From ea995907197bbf3d35c837b1f44b1100f669d694 Mon Sep 17 00:00:00 2001 From: Chopper <156493704+choppeh@users.noreply.github.com> Date: Wed, 20 Nov 2024 06:16:35 -0300 Subject: [PATCH] FoamGirl: Fix pages loading (#6146) * Fix loading pages * Use regex * Cleanup * Remove NUMB_REGEX Co-authored-by: Vetle Ledaal * Remove extra return Co-authored-by: Vetle Ledaal * Refactoring pageList Co-authored-by: Vetle Ledaal * Refactoring 'getPagesListByNumber' --------- Co-authored-by: Vetle Ledaal --- src/all/foamgirl/build.gradle | 2 +- .../extension/all/foamgirl/FoamGirl.kt | 50 ++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/all/foamgirl/build.gradle b/src/all/foamgirl/build.gradle index 34056cc00..182826551 100644 --- a/src/all/foamgirl/build.gradle +++ b/src/all/foamgirl/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'FoamGirl' extClass = '.FoamGirl' - extVersionCode = 1 + extVersionCode = 2 isNsfw = true } diff --git a/src/all/foamgirl/src/eu/kanade/tachiyomi/extension/all/foamgirl/FoamGirl.kt b/src/all/foamgirl/src/eu/kanade/tachiyomi/extension/all/foamgirl/FoamGirl.kt index 022a2e68c..742ecefde 100644 --- a/src/all/foamgirl/src/eu/kanade/tachiyomi/extension/all/foamgirl/FoamGirl.kt +++ b/src/all/foamgirl/src/eu/kanade/tachiyomi/extension/all/foamgirl/FoamGirl.kt @@ -6,6 +6,8 @@ import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.ParsedHttpSource +import eu.kanade.tachiyomi.util.asJsoup +import okhttp3.HttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.Request import org.jsoup.nodes.Document @@ -65,22 +67,43 @@ class FoamGirl() : ParsedHttpSource() { override fun searchMangaSelector() = popularMangaSelector() override fun pageListParse(document: Document): List { - val pages = mutableListOf() val imageCount = document.select(".post_title_topimg").text().substringAfter("(").substringBefore("P").toInt() val imageUrl = document.select(".imageclick-imgbox").attr("href").toHttpUrl() - val imagePrefix = imageUrl.pathSegments.last().substringBefore(".").toLong() / 10 - for (i in 0 until imageCount) { - pages.add( - Page( - i, - imageUrl = imageUrl.newBuilder().apply { - removePathSegment(imageUrl.pathSize - 1) - addPathSegment("${imagePrefix}${i + 2}.jpg") - }.build().toString(), - ), + val baseIndex = imageUrl.pathSegments.last().substringBefore(".") + + return if (baseIndex.isNumber()) { + getPagesListByNumber(imageCount, imageUrl, baseIndex) + } else { + getPageListByDocument(document) + } + } + + private fun getPagesListByNumber(imageCount: Int, imageUrl: HttpUrl, baseIndex: String): List { + val imagePrefix = baseIndex.toLong() / 10 + return (0 until imageCount).map { index -> + Page( + index, + imageUrl = imageUrl.newBuilder().apply { + removePathSegment(imageUrl.pathSize - 1) + addPathSegment("${imagePrefix}${index + 2}.jpg") + }.build().toString(), ) } - return pages + } + + private fun getPageListByDocument(document: Document): List { + val pages = document.select("#image_div img").mapIndexed { index, element -> + Page(index, imageUrl = element.absUrl("src")) + }.toList() + + val nextPageUrl = document.selectFirst(".page-numbers[title=Next]") + ?.absUrl("href") + ?.takeIf { HAS_NEXT_PAGE_REGEX in it } + ?: return pages + + return client.newCall(GET(nextPageUrl, headers)).execute().asJsoup().let { + pages + getPageListByDocument(it) + } } override fun chapterFromElement(element: Element) = SChapter.create().apply { @@ -119,7 +142,10 @@ class FoamGirl() : ParsedHttpSource() { } } + private fun String.isNumber() = isNotEmpty() && all { it.isDigit() } + companion object { + val HAS_NEXT_PAGE_REGEX = """(\d+_\d+)""".toRegex() private val DATE_FORMAT by lazy { SimpleDateFormat("yyyy.M.d", Locale.ENGLISH) }