From 68620cd05129fb00269541b6901b0e0585596bd3 Mon Sep 17 00:00:00 2001 From: Alessandro Jean Date: Fri, 10 Jul 2020 21:22:12 -0300 Subject: [PATCH] Fix chapters not loading at SuperHentais. (#3752) --- src/pt/supermangas/build.gradle | 2 +- .../pt/supermangas/SuperMangasGeneric.kt | 22 +++++---- .../pt/supermangas/source/SuperHentais.kt | 47 +++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/pt/supermangas/build.gradle b/src/pt/supermangas/build.gradle index 4f8a35b66..3262a2f2c 100644 --- a/src/pt/supermangas/build.gradle +++ b/src/pt/supermangas/build.gradle @@ -5,7 +5,7 @@ ext { extName = 'Super Mangás' pkgNameSuffix = 'pt.supermangas' extClass = '.SuperMangasFactory' - extVersionCode = 2 + extVersionCode = 3 libVersion = '1.2' } diff --git a/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/SuperMangasGeneric.kt b/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/SuperMangasGeneric.kt index b616af733..a11071a4e 100644 --- a/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/SuperMangasGeneric.kt +++ b/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/SuperMangasGeneric.kt @@ -68,6 +68,8 @@ abstract class SuperMangasGeneric( protected open val contentList: List = listOf() + protected open val chapterListOrder: String = "desc" + private fun genericPaginatedRequest( typeUrl: String, filterData: Map = defaultFilter, @@ -235,19 +237,23 @@ abstract class SuperMangasGeneric( override fun chapterListParse(response: Response): List { val document = response.asJsoup() - val totalPage = document.select("select.pageSelect option").last()!!.attr("value").toInt() - val idCategory = document.select("div#listaDeConteudo").first()!!.attr("data-id-cat").toInt() + val lastPage = document.select("select.pageSelect option").last()!! + .attr("value").toInt() + val idCategory = document.select("div#listaDeConteudo").first()!! + .attr("data-id-cat").toInt() val mangaUrl = response.request().url().toString() val chapters = mutableListOf() - for (page in 1..totalPage) { - val result = client.newCall(chapterListPaginatedRequest(idCategory, page, totalPage, mangaUrl)).execute() + for (page in 1..lastPage) { + val chapterListRequest = chapterListPaginatedRequest(idCategory, page, lastPage, mangaUrl) + val result = client.newCall(chapterListRequest).execute() val apiResponse = result.asJsonObject() if (apiResponse["codigo"].int == 0) break - chapters += Jsoup.parse(apiResponse["body"].asJsonArray.joinToString("") { it.string }) + val htmlBody = apiResponse["body"].array.joinToString("") { it.string } + chapters += Jsoup.parse(htmlBody) .select(chapterListSelector()) .map { chapterFromElement(it) } } @@ -261,11 +267,11 @@ abstract class SuperMangasGeneric( .add("page", page.toString()) .add("limit", "50") .add("total_page", totalPage.toString()) - .add("order_video", "desc") + .add("order_video", chapterListOrder) .add("type", "book") } - private fun chapterListPaginatedRequest(idCategory: Int, page: Int, totalPage: Int, mangaUrl: String): Request { + protected fun chapterListPaginatedRequest(idCategory: Int, page: Int, totalPage: Int, mangaUrl: String): Request { val form = chapterListPaginatedBody(idCategory, page, totalPage).build() val newHeaders = headersBuilder() @@ -338,7 +344,7 @@ abstract class SuperMangasGeneric( else -> SManga.UNKNOWN } - private fun Response.asJsonObject(): JsonObject = JSON_PARSER.parse(body()!!.string()).obj + protected fun Response.asJsonObject(): JsonObject = JSON_PARSER.parse(body()!!.string()).obj private fun Map.toUrlQueryParams(): String = map { (k, v) -> "$k=$v" }.joinToString("&") diff --git a/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/source/SuperHentais.kt b/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/source/SuperHentais.kt index bb253190a..655c9b94e 100644 --- a/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/source/SuperHentais.kt +++ b/src/pt/supermangas/src/eu/kanade/tachiyomi/extension/pt/supermangas/source/SuperHentais.kt @@ -1,11 +1,18 @@ package eu.kanade.tachiyomi.extension.pt.supermangas.source +import com.github.salomonbrys.kotson.array +import com.github.salomonbrys.kotson.int +import com.github.salomonbrys.kotson.string import eu.kanade.tachiyomi.extension.pt.supermangas.SuperMangasGeneric import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList +import eu.kanade.tachiyomi.source.model.SChapter +import eu.kanade.tachiyomi.util.asJsoup import okhttp3.HttpUrl import okhttp3.Request +import okhttp3.Response +import org.jsoup.Jsoup class SuperHentais : SuperMangasGeneric( "Super Hentais", @@ -27,6 +34,8 @@ class SuperHentais : SuperMangasGeneric( Triple("10", "manhwa-ero", "Manhwa") ) + override val chapterListOrder = "asc" + override fun searchMangaWithQueryRequest(query: String): Request { val searchUrl = HttpUrl.parse("$baseUrl/busca")!!.newBuilder() .addEncodedQueryParameter("parametro", query) @@ -38,6 +47,44 @@ class SuperHentais : SuperMangasGeneric( override fun searchMangaSelector(): String = "article.box_view.list div.grid_box:contains(Hentai Manga) div.grid_image.grid_image_vertical a" + override fun chapterListParse(response: Response): List { + val document = response.asJsoup() + + // Parse the first chapter list page available at the html. + val chapters = document.select(chapterListSelector()) + .map { chapterFromElement(it) } + .toMutableList() + + // Check if there is more pages. + val lastPage = document.select("select.pageSelect option").last()!! + .attr("value").toInt() + + if (lastPage > 1) { + val idCategory = document.select("div#listaDeConteudo").first()!! + .attr("data-id-cat").toInt() + val mangaUrl = response.request().url().toString() + + for (page in 2..lastPage) { + val chapterListRequest = chapterListPaginatedRequest(idCategory, page, lastPage, mangaUrl) + val result = client.newCall(chapterListRequest).execute() + val apiResponse = result.asJsonObject() + + // Check if for some reason the API returned an error. + if (apiResponse["codigo"].int == 0) break + + val htmlBody = apiResponse["body"].array.joinToString("") { it.string } + chapters += Jsoup.parse(htmlBody) + .select(chapterListSelector()) + .map { chapterFromElement(it) } + } + } + + // Reverse the chapters since the pagination is broken and there is no + // way to order some parts as desc in the API and unite with the first page + // that is ordered as asc. + return chapters.reversed() + } + override fun getFilterList() = FilterList( Filter.Header("Filtros abaixo são ignorados na busca!"), ContentFilter(contentList),