Anchira: Add trending filter (#1522)

Anchira: Add trending filters

Signed-off-by: Rama Bondan Prakoso <ramanarubp@gmail.com>
This commit is contained in:
Rama Bondan Prakoso 2024-02-29 23:20:23 +07:00 committed by Draff
parent 48ca6558ed
commit 420c7d9cd5
2 changed files with 83 additions and 48 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'Anchira' extName = 'Anchira'
extClass = '.Anchira' extClass = '.Anchira'
extVersionCode = 8 extVersionCode = 9
isNsfw = true isNsfw = true
} }

View File

@ -117,59 +117,76 @@ class Anchira : HttpSource(), ConfigurableSource {
} }
} }
override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request { override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request {
val filterList = if (filters.isEmpty()) getFilterList() else filters
val trendingFilter = filterList.findInstance<TrendingFilter>()
val sortTrendingFilter = filters.findInstance<SortTrendingFilter>()
var url = libraryUrl.toHttpUrl().newBuilder() var url = libraryUrl.toHttpUrl().newBuilder()
url.addQueryParameter("page", page.toString()) url.addQueryParameter("page", page.toString())
if (query.isNotBlank()) { if (trendingFilter?.state == true) {
url.addQueryParameter("s", query) val interval = when (sortTrendingFilter?.state) {
} 1 -> "3"
else -> ""
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) 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) override fun searchMangaParse(response: Response) = latestUpdatesParse(response)
@ -298,6 +315,10 @@ class Anchira : HttpSource(), ConfigurableSource {
CategoryGroup(), CategoryGroup(),
SortFilter(), SortFilter(),
FavoritesFilter(), FavoritesFilter(),
Filter.Separator(),
Filter.Header("Others are ignored if trending only"),
TrendingFilter(),
SortTrendingFilter(),
) )
private class CategoryFilter(name: String) : Filter.CheckBox(name, false) private class CategoryFilter(name: String) : Filter.CheckBox(name, false)
@ -317,6 +338,18 @@ class Anchira : HttpSource(), ConfigurableSource {
Selection(2, false), 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<String>) :
Filter.Select<String>(displayName, value)
private val SharedPreferences.imageQuality private val SharedPreferences.imageQuality
get() = getString(IMAGE_QUALITY_PREF, "b")!! get() = getString(IMAGE_QUALITY_PREF, "b")!!
@ -362,6 +395,8 @@ class Anchira : HttpSource(), ConfigurableSource {
.use { json.decodeFromStream<List<EntryKey>>(it.body.byteStream()) } .use { json.decodeFromStream<List<EntryKey>>(it.body.byteStream()) }
} }
private inline fun <reified T> Iterable<*>.findInstance() = find { it is T } as? T
companion object { companion object {
const val SLUG_SEARCH_PREFIX = "id:" const val SLUG_SEARCH_PREFIX = "id:"
private const val IMAGE_QUALITY_PREF = "image_quality" private const val IMAGE_QUALITY_PREF = "image_quality"