From 589f46de0becdc7d70eaa9773e22e543c6e75d6d Mon Sep 17 00:00:00 2001 From: beerpsi <92439990+beerpiss@users.noreply.github.com> Date: Sun, 22 Jan 2023 19:31:05 +0700 Subject: [PATCH] [Otaku Sanctuary] Move back to main endpoint for fetching chapter pages (#15062) * [Otaku Sanctuary] Move back to main endpoint for fetching chapter pages * tfw you write code at 2AM --- .../multisrc/otakusanctuary/OtakuSanctuary.kt | 106 ++++++++++++++---- .../otakusanctuary/OtakuSanctuaryGenerator.kt | 2 +- .../otakusanctuary/OtakuSanctuaryHelper.kt | 4 +- 3 files changed, 85 insertions(+), 27 deletions(-) diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuary.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuary.kt index e275e712c..4a6cb181f 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuary.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuary.kt @@ -9,8 +9,8 @@ import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.util.asJsoup -import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import kotlinx.serialization.json.jsonArray import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import okhttp3.FormBody @@ -92,9 +92,9 @@ open class OtakuSanctuary( return MangasPage(parseMangaCollection(collection), collection.size >= 24) } - override fun latestUpdatesRequest(page: Int) = throw Exception("Unused") + override fun latestUpdatesRequest(page: Int) = throw UnsupportedOperationException("Not used") - override fun latestUpdatesParse(response: Response) = throw Exception("Unused") + override fun latestUpdatesParse(response: Response) = throw UnsupportedOperationException("Not used") override fun searchMangaRequest(page: Int, query: String, filters: FilterList): Request = GET( @@ -181,8 +181,45 @@ open class OtakuSanctuary( val vi = document.select("#dataip").attr("value") val numericId = document.select("#inpit-c").attr("data-chapter-id") - val rawPagesArray = try { - val data = json.parseToJsonElement( + val data = json.parseToJsonElement( + client.newCall( + POST( + "$baseUrl/Manga/UpdateView", + headers, + FormBody.Builder().add("chapId", numericId).build() + ) + ).execute().body!!.string() + ).jsonObject + + if (data["view"] != null) { + val usingservers = mutableListOf(0, 0, 0) + + val isSuccess = data["isSuccess"]!!.jsonArray.map { it.jsonPrimitive.content } + return json.parseToJsonElement(data["view"]!!.jsonPrimitive.content).jsonArray.mapIndexed { idx, it -> + var url = helper.processUrl(it.jsonPrimitive.content).removePrefix("image:") + val indexServer = getIndexLessServer(usingservers) + + if (url.contains("ImageSyncing") || url.contains("FetchService") || url.contains("otakusan.net_") && (url.contains("extendContent") || url.contains("/Extend")) && !url.contains("fetcher.otakusan.net") && !url.contains("image3.otakusan.net") && !url.contains("image3.otakuscan.net") && !url.contains("[GDP]") && !url.contains("[GDT]")) { + if (url.startsWith("/api/Value/")) { + val serverUrl = if (helper.otakusanLang() == "us" && indexServer == 1) { + US_SERVERS[0] + } else { + SERVERS[indexServer] + } + url = "$serverUrl$url" + } + + if (url.contains("otakusan.net_") && !url.contains("fetcher.otakuscan.net")) { + url += "#${isSuccess[idx]}" + } + + usingservers[indexServer] += 1 + } + + Page(idx, imageUrl = url) + } + } else { + val alternate = json.parseToJsonElement( client.newCall( POST( "$baseUrl/Manga/CheckingAlternate", @@ -190,25 +227,46 @@ open class OtakuSanctuary( FormBody.Builder().add("chapId", numericId).build() ) ).execute().body!!.string() - ) - - data.jsonObject["Content"]!!.jsonPrimitive.content - } catch (_: Exception) { - val data = json.parseToJsonElement( - client.newCall( - POST( - "$baseUrl/Manga/UpdateView", - headers, - FormBody.Builder().add("chapId", numericId).build() - ) - ).execute().body!!.string() - ) - - data.jsonObject["view"]!!.jsonPrimitive.content - } - - return json.decodeFromString>(rawPagesArray).mapIndexed { idx, it -> - Page(idx, imageUrl = helper.processUrl(it, vi)) + ).jsonObject + val content = alternate["Content"]?.jsonPrimitive?.content + ?: throw Exception("No pages found") + return json.parseToJsonElement(content).jsonArray.mapIndexed { idx, it -> + Page(idx, imageUrl = helper.processUrl(it.jsonPrimitive.content, vi)) + } } } + + override fun imageRequest(page: Page): Request { + val request = super.imageRequest(page) + val url = request.url.toString() + + val newRequest = request.newBuilder() + + if (url.contains("ImageSyncing") || url.contains("FetchService") || url.contains("otakusan.net_") && (url.contains("extendContent") || url.contains("/Extend")) && !url.contains("fetcher.otakusan.net") && !url.contains("image3.otakusan.net") && !url.contains("image3.otakuscan.net") && !url.contains("[GDP]") && !url.contains("[GDT]")) { + if (url.contains("otakusan.net_") && !url.contains("fetcher.otakuscan.net")) { + newRequest.header("page-sign", request.url.fragment!!) + } else { + newRequest.header("page-lang", "vn-lang") + } + } + + return newRequest.build() + } + + private fun getIndexLessServer(usingservers: List): Int { + var minIndex = usingservers[0] + var minNumber = usingservers[0] + for (i in 1 until 3) { + if (usingservers[i] <= minNumber) { + minIndex = i + minNumber = usingservers[i] + } + } + return minIndex + } + + companion object { + val SERVERS = listOf("https://image2.otakuscan.net", "https://shopotaku.net", "https://image.otakuscan.net") + val US_SERVERS = listOf("https://image3.shopotaku.net", "https://image2.otakuscan.net") + } } diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryGenerator.kt index 86736e298..5a7081a1a 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryGenerator.kt @@ -9,7 +9,7 @@ class OtakuSanctuaryGenerator : ThemeSourceGenerator { override val themeClass = "OtakuSanctuary" - override val baseVersionCode: Int = 1 + override val baseVersionCode: Int = 2 override val sources = listOf( MultiLang( diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryHelper.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryHelper.kt index c9bd13d14..ec410cebc 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryHelper.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/otakusanctuary/OtakuSanctuaryHelper.kt @@ -10,7 +10,7 @@ class OtakuSanctuaryHelper(private val lang: String) { else -> lang } - fun processUrl(url: String, vi: String): String { + fun processUrl(url: String, vi: String = ""): String { var url = url.replace("_h_", "http") .replace("_e_", "/extendContent/Manga") .replace("_r_", "/extendContent/MangaRaw") @@ -31,7 +31,7 @@ class OtakuSanctuaryHelper(private val lang: String) { url } "[IS1]" -> { - var url = url.replace("[IS1]", "https://imagepi.otakuscan.net/") + val url = url.replace("[IS1]", "https://imagepi.otakuscan.net/") if (url.contains("vi") && url.contains("otakusan.net_")) { url } else {