From c9a27a8b51e9e0b2f2372545e3f91bfb1ae06dd0 Mon Sep 17 00:00:00 2001 From: zhongfly <11155705+zhongfly@users.noreply.github.com> Date: Sun, 29 Dec 2024 14:34:32 +0800 Subject: [PATCH] zaimanhua: add ranking filters (#6792) --- src/zh/zaimanhua/build.gradle | 2 +- .../extension/zh/zaimanhua/Filter.kt | 42 ++++++++++++++++++ .../extension/zh/zaimanhua/Zaimanhua.kt | 43 ++++++++++++++----- 3 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Filter.kt diff --git a/src/zh/zaimanhua/build.gradle b/src/zh/zaimanhua/build.gradle index 552b19fbc..27e316df7 100644 --- a/src/zh/zaimanhua/build.gradle +++ b/src/zh/zaimanhua/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Zaimanhua' extClass = '.Zaimanhua' - extVersionCode = 4 + extVersionCode = 5 } apply from: "$rootDir/common.gradle" diff --git a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Filter.kt b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Filter.kt new file mode 100644 index 000000000..6a516f85f --- /dev/null +++ b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Filter.kt @@ -0,0 +1,42 @@ +package eu.kanade.tachiyomi.extension.zh.zaimanhua + +import eu.kanade.tachiyomi.source.model.Filter +import okhttp3.HttpUrl + +open class QueryFilter(name: String, private val key: String, private val params: Array>) : + Filter.Select(name, params.map { it.first }.toTypedArray()) { + fun addQuery(builder: HttpUrl.Builder) { + val param = params[state].second + if (param.isNotEmpty()) { + builder.addQueryParameter(key, param) + } + } +} +class RankingGroup : Filter.Group>( + "排行榜(搜索文本时无效)", + listOf>( + TimeFilter(), + SortFilter(), + ), +) { + private class TimeFilter : QueryFilter( + "榜单", + "by_time", + arrayOf( + Pair("日排行", "0"), + Pair("周排行", "1"), + Pair("月排行", "2"), + Pair("总排行", "3"), + ), + ) + + private class SortFilter : QueryFilter( + "排序", + "rank_type", + arrayOf( + Pair("人气", "0"), + Pair("吐槽", "1"), + Pair("订阅", "2"), + ), + ) +} diff --git a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt index 004d87b18..d3c7dc5bd 100644 --- a/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt +++ b/src/zh/zaimanhua/src/eu/kanade/tachiyomi/extension/zh/zaimanhua/Zaimanhua.kt @@ -197,8 +197,8 @@ class Zaimanhua : HttpSource(), ConfigurableSource { // Popular private fun rankApiUrl(): HttpUrl.Builder = - "$apiUrl/comic/rank/list".toHttpUrl().newBuilder().addQueryParameter("by_time", "3") - .addQueryParameter("tag_id", "0").addQueryParameter("rank_type", "0") + "$apiUrl/comic/rank/list".toHttpUrl().newBuilder() + .addQueryParameter("tag_id", "0") override fun popularMangaRequest(page: Int): Request = GET( rankApiUrl().apply { @@ -214,16 +214,30 @@ class Zaimanhua : HttpSource(), ConfigurableSource { "$apiUrl/search/index".toHttpUrl().newBuilder().addQueryParameter("source", "0") .addQueryParameter("size", "20") - override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request = GET( - searchApiUrl().apply { - addQueryParameter("keyword", query) - addQueryParameter("page", page.toString()) - }.build(), - apiHeaders, - ) + override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { + val ranking = filters.filterIsInstance().firstOrNull() + val url = if (query.isEmpty() && ranking != null) { + rankApiUrl().apply { + ranking.state.filterIsInstance().forEach { + it.addQuery(this) + } + addQueryParameter("page", page.toString()) + }.build() + } else { + searchApiUrl().apply { + addQueryParameter("keyword", query) + addQueryParameter("page", page.toString()) + }.build() + } + return GET(url, apiHeaders) + } override fun searchMangaParse(response: Response): MangasPage = - response.parseAs>().data.toMangasPage() + if (response.request.url.toString().startsWith("$apiUrl/comic/rank/list")) { + latestUpdatesParse(response) + } else { + response.parseAs>().data.toMangasPage() + } // Latest // "$apiUrl/comic/update/list/1/$page" is same content @@ -231,10 +245,17 @@ class Zaimanhua : HttpSource(), ConfigurableSource { GET("$apiUrl/comic/update/list/0/$page", apiHeaders) override fun latestUpdatesParse(response: Response): MangasPage { - val mangas = response.parseAs>>().data + val mangas = response.parseAs?>>().data + if (mangas.isNullOrEmpty()) { + throw Exception("没有更多结果了") + } return MangasPage(mangas.map { it.toSManga() }, true) } + override fun getFilterList() = FilterList( + RankingGroup(), + ) + companion object { val USE_CACHE = CacheControl.Builder().maxStale(170, TimeUnit.SECONDS).build() }