From 420c7d9cd5bcadf2a5f3e6410856e2735b199797 Mon Sep 17 00:00:00 2001 From: Rama Bondan Prakoso Date: Thu, 29 Feb 2024 23:20:23 +0700 Subject: [PATCH] Anchira: Add trending filter (#1522) Anchira: Add trending filters Signed-off-by: Rama Bondan Prakoso --- src/en/anchira/build.gradle | 2 +- .../tachiyomi/extension/en/anchira/Anchira.kt | 129 +++++++++++------- 2 files changed, 83 insertions(+), 48 deletions(-) diff --git a/src/en/anchira/build.gradle b/src/en/anchira/build.gradle index c924fecb0..b040a87d8 100644 --- a/src/en/anchira/build.gradle +++ b/src/en/anchira/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Anchira' extClass = '.Anchira' - extVersionCode = 8 + extVersionCode = 9 isNsfw = true } diff --git a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt index 8efe53e65..ff26a17ff 100644 --- a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt +++ b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt @@ -117,59 +117,76 @@ class Anchira : HttpSource(), ConfigurableSource { } } override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { + val filterList = if (filters.isEmpty()) getFilterList() else filters + val trendingFilter = filterList.findInstance() + val sortTrendingFilter = filters.findInstance() var url = libraryUrl.toHttpUrl().newBuilder() url.addQueryParameter("page", page.toString()) - if (query.isNotBlank()) { - url.addQueryParameter("s", query) - } - - filters.forEach { filter -> - when (filter) { - is CategoryGroup -> { - var sum = 0 - - filter.state.forEach { category -> - when (category.name) { - "Manga" -> if (category.state) sum = sum or 1 - "Doujinshi" -> if (category.state) sum = sum or 2 - "Illustration" -> if (category.state) sum = sum or 4 - } - } - - if (sum > 0) url.addQueryParameter("cat", sum.toString()) - } - - is SortFilter -> { - val sort = when (filter.state?.index) { - 0 -> "1" - 1 -> "2" - 2 -> "4" - 4 -> "32" - else -> "" - } - - if (sort.isNotEmpty()) url.addQueryParameter("sort", sort) - if (filter.state?.ascending == true) url.addQueryParameter("order", "1") - } - - is FavoritesFilter -> { - if (filter.state) { - if (!isLoggedIn()) { - throw IOException("No login cookie found") - } - - url = url.toString().replace("library", "user/favorites").toHttpUrl() - .newBuilder() - } - } - - else -> {} + if (trendingFilter?.state == true) { + val interval = when (sortTrendingFilter?.state) { + 1 -> "3" + else -> "" } - } - return GET(url.build(), headers) + if (interval.isNotBlank()) url.addQueryParameter("interval", interval) + + url = url.toString().replace("library", "trending").toHttpUrl() + .newBuilder() + + return GET(url.build(), headers) + } else { + if (query.isNotBlank()) { + url.addQueryParameter("s", query) + } + + filters.forEach { filter -> + when (filter) { + is CategoryGroup -> { + var sum = 0 + + filter.state.forEach { category -> + when (category.name) { + "Manga" -> if (category.state) sum = sum or 1 + "Doujinshi" -> if (category.state) sum = sum or 2 + "Illustration" -> if (category.state) sum = sum or 4 + } + } + + if (sum > 0) url.addQueryParameter("cat", sum.toString()) + } + + is SortFilter -> { + val sort = when (filter.state?.index) { + 0 -> "1" + 1 -> "2" + 2 -> "4" + 4 -> "32" + else -> "" + } + + if (sort.isNotEmpty()) url.addQueryParameter("sort", sort) + if (filter.state?.ascending == true) url.addQueryParameter("order", "1") + } + + is FavoritesFilter -> { + if (filter.state) { + if (!isLoggedIn()) { + throw IOException("No login cookie found") + } + + url = url.toString().replace("library", "user/favorites").toHttpUrl() + .newBuilder() + } + } + + else -> {} + } + } + + return GET(url.build(), headers) + } } override fun searchMangaParse(response: Response) = latestUpdatesParse(response) @@ -298,6 +315,10 @@ class Anchira : HttpSource(), ConfigurableSource { CategoryGroup(), SortFilter(), FavoritesFilter(), + Filter.Separator(), + Filter.Header("Others are ignored if trending only"), + TrendingFilter(), + SortTrendingFilter(), ) private class CategoryFilter(name: String) : Filter.CheckBox(name, false) @@ -317,6 +338,18 @@ class Anchira : HttpSource(), ConfigurableSource { Selection(2, false), ) + private class TrendingFilter : Filter.CheckBox( + "Show only trending", + ) + + private class SortTrendingFilter : PartFilter( + "Sort By", + arrayOf("Trending: Weekly", "Trending: Monthly"), + ) + + private open class PartFilter(displayName: String, value: Array) : + Filter.Select(displayName, value) + private val SharedPreferences.imageQuality get() = getString(IMAGE_QUALITY_PREF, "b")!! @@ -362,6 +395,8 @@ class Anchira : HttpSource(), ConfigurableSource { .use { json.decodeFromStream>(it.body.byteStream()) } } + private inline fun Iterable<*>.findInstance() = find { it is T } as? T + companion object { const val SLUG_SEARCH_PREFIX = "id:" private const val IMAGE_QUALITY_PREF = "image_quality"