From 1e3332d8052833e98566fa80caa0f5237d9f894c Mon Sep 17 00:00:00 2001 From: jopejoe1 <34899572+jopejoe1@users.noreply.github.com> Date: Wed, 12 Jan 2022 01:53:49 +1300 Subject: [PATCH] Bato.to reworked fetchSearchManga class (#10427) * Update BatoTo.kt * Update build.gradle --- src/all/batoto/build.gradle | 2 +- .../tachiyomi/extension/all/batoto/BatoTo.kt | 149 +++++++++--------- 2 files changed, 76 insertions(+), 75 deletions(-) diff --git a/src/all/batoto/build.gradle b/src/all/batoto/build.gradle index 7c520e39a..ffd2d0af4 100644 --- a/src/all/batoto/build.gradle +++ b/src/all/batoto/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'Bato.to' pkgNameSuffix = 'all.batoto' extClass = '.BatoToFactory' - extVersionCode = 19 + extVersionCode = 20 isNsfw = true } diff --git a/src/all/batoto/src/eu/kanade/tachiyomi/extension/all/batoto/BatoTo.kt b/src/all/batoto/src/eu/kanade/tachiyomi/extension/all/batoto/BatoTo.kt index b6d7dbe33..c1d51648d 100644 --- a/src/all/batoto/src/eu/kanade/tachiyomi/extension/all/batoto/BatoTo.kt +++ b/src/all/batoto/src/eu/kanade/tachiyomi/extension/all/batoto/BatoTo.kt @@ -25,6 +25,7 @@ import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import okhttp3.FormBody +import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.OkHttpClient import okhttp3.Request @@ -143,9 +144,6 @@ open class BatoTo( override fun popularMangaNextPageSelector() = latestUpdatesNextPageSelector() override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable { - val utilsFilter = filters.findInstance()!! - val letterFilter = filters.findInstance()!! - val historyFilter = filters.findInstance()!! return when { query.startsWith("ID:") -> { val id = query.substringAfter("ID:") @@ -154,79 +152,87 @@ open class BatoTo( queryIDParse(response, id) } } - query.isNotBlank() && letterFilter.state != 0 -> { - val url = "$baseUrl/search?word=$query&page=$page&mode=letter" - client.newCall(GET(url, headers)).asObservableSuccess() - .map { response -> - queryParse(response) - } - } query.isNotBlank() -> { - val url = "$baseUrl/search?word=$query&page=$page" - client.newCall(GET(url, headers)).asObservableSuccess() + val url = "$baseUrl/search".toHttpUrl().newBuilder() + .addQueryParameter("word", query) + .addQueryParameter("page", page.toString()) + filters.forEach { filter -> + when (filter) { + is LetterFilter -> { + if (filter.state == 1) { + url.addQueryParameter("mode", "letter") + } + } + } + } + client.newCall(GET(url.build().toString(), headers)).asObservableSuccess() .map { response -> queryParse(response) } } - utilsFilter.state != 0 -> { - val url = "$baseUrl/_utils/comic-list?type=${utilsFilter.selected}" - client.newCall(GET(url, headers)).asObservableSuccess() - .map { response -> - queryUtilsParse(response) - } - } - historyFilter.state != 0 -> { - val url = "$baseUrl/ajax.my.${historyFilter.selected}.paging" - client.newCall(POST(url, headers, formBuilder().build())).asObservableSuccess() - .map { response -> - queryHistoryParse(response) - } - } else -> { - val sortFilter = filters.findInstance()!! - val reverseSortFilter = filters.findInstance()!! - val statusFilter = filters.findInstance()!! - val langFilter = filters.findInstance()!! - val originFilter = filters.findInstance()!! - val genreFilter = filters.findInstance()!! - val minChapterFilter = filters.findInstance()!! - val maxChapterFilter = filters.findInstance()!! val url = "$baseUrl/browse".toHttpUrlOrNull()!!.newBuilder() + var min = "" + var max= "" + filters.forEach { filter -> + when (filter) { + is UtilsFilter -> { + if (filter.state != 0) { + val filterUrl = "$baseUrl/_utils/comic-list?type=${filter.selected}" + return client.newCall(GET(filterUrl, headers)).asObservableSuccess() + .map { response -> + queryUtilsParse(response) + } + } + } + is HistoryFilter -> { + if (filter.state != 0) { + val filterUrl = "$baseUrl/ajax.my.${filter.selected}.paging" + return client.newCall(POST(filterUrl, headers, formBuilder().build())).asObservableSuccess() + .map { response -> + queryHistoryParse(response) + } + } + } + is LangGroupFilter -> { + if (filter.selected.isEmpty()) { + url.addQueryParameter("langs", siteLang) + } else { + val selection = "${filter.selected.joinToString(",")},$siteLang" + url.addQueryParameter("langs", selection) + } + } + is GenreGroupFilter -> { + with(filter) { + url.addQueryParameter( + "genres", included.joinToString(",") + "|" + excluded.joinToString(",") + ) + } + } + is StatusFilter -> url.addQueryParameter("release", filter.selected) + is SortFilter -> { + if (filter.state != null) { + val sort = getSortFilter()[filter.state!!.index].value + val value = when (filter.state!!.ascending) { + true -> "az" + false -> "za" + } + url.addQueryParameter("sort", "$sort.$value") + } + } + is OriginGroupFilter -> { + if (filter.selected.isNotEmpty()) { + url.addQueryParameter("origs", filter.selected.joinToString(",")) + } + } + is MinChapterTextFilter -> min = filter.state + is MaxChapterTextFilter -> max = filter.state + } + } url.addQueryParameter("page", page.toString()) - with(langFilter) { - if (this.selected.isEmpty()) { - url.addQueryParameter("langs", siteLang) - } else { - val selection = "${this.selected.joinToString(",")},$siteLang" - url.addQueryParameter("langs", selection) - } - } - - with(genreFilter) { - url.addQueryParameter( - "genres", included.joinToString(",") + "|" + excluded.joinToString(",") - ) - } - - with(statusFilter) { - url.addQueryParameter("release", this.selected) - } - - with(sortFilter) { - if (reverseSortFilter.state) { - url.addQueryParameter("sort", "${this.selected}.az") - } else { - url.addQueryParameter("sort", "${this.selected}.za") - } - } - - if (originFilter.selected.isNotEmpty()) { - url.addQueryParameter("origs", originFilter.selected.joinToString(",")) - } - - if (maxChapterFilter.state.isNotEmpty() or minChapterFilter.state.isNotEmpty()) { - url.addQueryParameter("chapters", minChapterFilter.state + "-" + maxChapterFilter.state) + if (max.isNotEmpty() or min.isNotEmpty()) { + url.addQueryParameter("chapters", "$min-$max") } client.newCall(GET(url.build().toString(), headers)).asObservableSuccess() @@ -500,14 +506,13 @@ open class BatoTo( Filter.Separator(), Filter.Header("NOTE: Ignored if using text search!"), Filter.Separator(), - SortFilter(getSortFilter(), 5), + SortFilter(getSortFilter().map { it.name }.toTypedArray()), StatusFilter(getStatusFilter(), 0), GenreGroupFilter(getGenreFilter()), OriginGroupFilter(getOrginFilter()), LangGroupFilter(getLangFilter()), MinChapterTextFilter(), MaxChapterTextFilter(), - ReverseSortFilter(), Filter.Separator(), Filter.Header("NOTE: Filters below are incompatible with any other filters!"), Filter.Header("NOTE: Login Required!"), @@ -515,7 +520,6 @@ open class BatoTo( UtilsFilter(getUtilsFilter(), 0), HistoryFilter(getHistoryFilter(), 0), ) - class SelectFilterOption(val name: String, val value: String) class CheckboxFilterOption(val value: String, name: String, default: Boolean = false) : Filter.CheckBox(name, default) class TriStateFilterOption(val value: String, name: String, default: Int = 0) : Filter.TriState(name, default) @@ -540,8 +544,7 @@ open class BatoTo( abstract class TextFilter(name: String) : Filter.Text(name) - class SortFilter(options: List, default: Int) : SelectFilter("Sort By", options, default) - class ReverseSortFilter(default: Boolean = false) : Filter.CheckBox("Revers Sort", default) + class SortFilter(sortables: Array) : Filter.Sort("Sort", sortables, Selection(5, false)) class StatusFilter(options: List, default: Int) : SelectFilter("Status", options, default) class OriginGroupFilter(options: List) : CheckboxGroupFilter("Origin", options) class GenreGroupFilter(options: List) : TriStateGroupFilter("Genre", options) @@ -927,6 +930,4 @@ open class BatoTo( CheckboxFilterOption("eu", "Basque"), CheckboxFilterOption("pt-PT", "Portuguese (Portugal)"), ).filterNot { it.value == siteLang } - - private inline fun Iterable<*>.findInstance() = find { it is T } as? T }