From 67b94602e660a87f0940dd45544c36b480e58ab3 Mon Sep 17 00:00:00 2001 From: Alessandro Jean Date: Sun, 28 Nov 2021 13:15:50 -0300 Subject: [PATCH] Automatically check which endpoint to use in Madara. (#9948) --- .../tachiyomi/multisrc/madara/Madara.kt | 23 +++++++++++++++++-- .../multisrc/madara/MadaraGenerator.kt | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt index 90eb72f3c..99adb80ed 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/Madara.kt @@ -422,6 +422,14 @@ abstract class Madara( */ protected open val useNewChapterEndpoint: Boolean = false + /** + * Internal attribute to control if it should always use the + * new chapter endpoint after a first check if useNewChapterEndpoint is + * set to false. Using a separate variable to still allow the other + * one to be overridable manually in each source. + */ + private var oldChapterEndpointDisabled: Boolean = false + protected open fun oldXhrChaptersRequest(mangaId: String): Request { val form = FormBody.Builder() .add("action", "manga_get_chapters") @@ -457,8 +465,19 @@ abstract class Madara( val mangaUrl = document.location().removeSuffix("/") val mangaId = chaptersWrapper.attr("data-id") - val xhrRequest = if (useNewChapterEndpoint) xhrChaptersRequest(mangaUrl) else oldXhrChaptersRequest(mangaId) - val xhrResponse = client.newCall(xhrRequest).execute() + var xhrRequest = if (useNewChapterEndpoint || oldChapterEndpointDisabled) + xhrChaptersRequest(mangaUrl) else oldXhrChaptersRequest(mangaId) + var xhrResponse = client.newCall(xhrRequest).execute() + + // Newer Madara versions throws HTTP 400 when using the old endpoint. + if (!useNewChapterEndpoint && xhrResponse.code == 400) { + xhrResponse.close() + // Set it to true so following calls will be made directly to the new endpoint. + oldChapterEndpointDisabled = true + + xhrRequest = xhrChaptersRequest(mangaUrl) + xhrResponse = client.newCall(xhrRequest).execute() + } chapterElements = xhrResponse.asJsoup().select(chapterListSelector()) xhrResponse.close() diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/MadaraGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/MadaraGenerator.kt index 618826df0..ceeeaf0c5 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/MadaraGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madara/MadaraGenerator.kt @@ -10,7 +10,7 @@ class MadaraGenerator : ThemeSourceGenerator { override val themeClass = "Madara" - override val baseVersionCode: Int = 12 + override val baseVersionCode: Int = 13 override val sources = listOf( MultiLang("Leviatan Scans", "https://leviatanscans.com", listOf("en", "es"), className = "LeviatanScansFactory", overrideVersionCode = 9),