FoamGirl: Fix pages loading (#6146)

* Fix loading pages

* Use regex

* Cleanup

* Remove NUMB_REGEX

Co-authored-by: Vetle Ledaal <vetle.ledaal@gmail.com>

* Remove extra return

Co-authored-by: Vetle Ledaal <vetle.ledaal@gmail.com>

* Refactoring pageList

Co-authored-by: Vetle Ledaal <vetle.ledaal@gmail.com>

* Refactoring 'getPagesListByNumber'

---------

Co-authored-by: Vetle Ledaal <vetle.ledaal@gmail.com>
This commit is contained in:
Chopper 2024-11-20 06:16:35 -03:00 committed by Draff
parent 855a25518b
commit ea99590719
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
2 changed files with 39 additions and 13 deletions

View File

@ -1,7 +1,7 @@
ext {
extName = 'FoamGirl'
extClass = '.FoamGirl'
extVersionCode = 1
extVersionCode = 2
isNsfw = true
}

View File

@ -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<Page> {
val pages = mutableListOf<Page>()
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(
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<Page> {
val imagePrefix = baseIndex.toLong() / 10
return (0 until imageCount).map { index ->
Page(
i,
index,
imageUrl = imageUrl.newBuilder().apply {
removePathSegment(imageUrl.pathSize - 1)
addPathSegment("${imagePrefix}${i + 2}.jpg")
addPathSegment("${imagePrefix}${index + 2}.jpg")
}.build().toString(),
),
)
}
return pages
}
private fun getPageListByDocument(document: Document): List<Page> {
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)
}