Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Madtheme: fix chapters fetching #7270

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib-multisrc/madtheme/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ plugins {
id("lib-multisrc")
}

baseVersionCode = 15
baseVersionCode = 16
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.source.online.ParsedHttpSource
import eu.kanade.tachiyomi.util.asJsoup
import kotlinx.serialization.json.Json
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -183,18 +184,19 @@ abstract class MadTheme(
val bookId = script.data().substringAfter("bookId = ").substringBefore(";")
val bookSlug = script.data().substringAfter("bookSlug = \"").substringBefore("\";")

// Find by bookId, if no result then try with slug
var chapterRequest =
client.newCall(GET("$baseUrl/api/manga/$bookId/chapters?source=detail", headers))
.execute()
// At this moment we can not decide which endpoint has the chapters, so we call both.
val idRequest = client.newCall(GET(buildChapterUrl(bookId), headers)).execute()
val slugRequest = client.newCall(GET(buildChapterUrl(bookSlug), headers)).execute()

if (chapterRequest.code !in 200..299) {
chapterRequest =
client.newCall(GET("$baseUrl/api/manga/$bookSlug/chapters?source=detail", headers))
.execute()
// By default the id request will be the final, due to some extension don't even has slug fetch.
var finalDocument = idRequest.asJsoup().select(chapterListSelector())
val slugDocument = slugRequest.asJsoup().select(chapterListSelector())

if (finalDocument.size < slugDocument.size) {
finalDocument = slugDocument
}

return chapterRequest.asJsoup().select("#chapter-list > li").map {
return finalDocument.map {
SChapter.create().apply {
url = it.selectFirst("a")!!.absUrl("href").removePrefix(baseUrl)
name = it.selectFirst(".chapter-title")!!.text()
Expand All @@ -203,6 +205,16 @@ abstract class MadTheme(
}
}

private fun buildChapterUrl(fetchByParam: String): HttpUrl {
return baseUrl.toHttpUrl().newBuilder().apply {
addPathSegment("api")
addPathSegment("manga")
addPathSegment(fetchByParam)
addPathSegment("chapters")
addQueryParameter("source", "detail")
}.build()
}

override fun chapterListRequest(manga: SManga): Request =
MANGA_ID_REGEX.find(manga.url)?.groupValues?.get(1)?.let { mangaId ->
val url = "$baseUrl/service/backend/chaplist/".toHttpUrl().newBuilder()
Expand Down