From d7c6574e876144ea4c4b626c7ad71bc78ca8b8d5 Mon Sep 17 00:00:00 2001 From: Jake Date: Sat, 15 Mar 2025 15:10:38 +0800 Subject: [PATCH] MangaHub (multisrc) - Fix "API Limit Exceeded" (#8073) * Fixed "API limit exceeded" gonna crash out if this build failed again bc of linting * review changes * Review changes --- lib-multisrc/mangahub/build.gradle.kts | 2 +- .../tachiyomi/multisrc/mangahub/MangaHub.kt | 50 ++++++++----------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/lib-multisrc/mangahub/build.gradle.kts b/lib-multisrc/mangahub/build.gradle.kts index aadb9d353..cc781e3e0 100644 --- a/lib-multisrc/mangahub/build.gradle.kts +++ b/lib-multisrc/mangahub/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("lib-multisrc") } -baseVersionCode = 29 +baseVersionCode = 30 dependencies { api(project(":lib:randomua")) diff --git a/lib-multisrc/mangahub/src/eu/kanade/tachiyomi/multisrc/mangahub/MangaHub.kt b/lib-multisrc/mangahub/src/eu/kanade/tachiyomi/multisrc/mangahub/MangaHub.kt index 856847862..75ded1cd8 100644 --- a/lib-multisrc/mangahub/src/eu/kanade/tachiyomi/multisrc/mangahub/MangaHub.kt +++ b/lib-multisrc/mangahub/src/eu/kanade/tachiyomi/multisrc/mangahub/MangaHub.kt @@ -17,7 +17,6 @@ import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.put -import kotlinx.serialization.json.putJsonObject import okhttp3.Cookie import okhttp3.Headers import okhttp3.HttpUrl.Companion.toHttpUrl @@ -31,7 +30,7 @@ import org.jsoup.nodes.Document import org.jsoup.nodes.Element import rx.Observable import uy.kohesive.injekt.injectLazy -import java.net.URLEncoder +import java.io.IOException import java.text.ParseException import java.text.SimpleDateFormat import java.util.Calendar @@ -49,6 +48,7 @@ abstract class MangaHub( private var baseApiUrl = "https://api.mghcdn.com" private var baseCdnUrl = "https://imgx.mghcdn.com" + private val regex = Regex("mhub_access=([^;]+)") override val client: OkHttpClient = network.cloudflareClient.newBuilder() .setRandomUserAgent( @@ -91,8 +91,6 @@ abstract class MangaHub( } private fun refreshApiKey(chapter: SChapter) { - val now = Calendar.getInstance().time.time - val slug = "$baseUrl${chapter.url}" .toHttpUrlOrNull() ?.pathSegments @@ -104,32 +102,28 @@ abstract class MangaHub( baseUrl.toHttpUrl() } - // Clear key cookie - val cookie = Cookie.parse(url, "mhub_access=; Max-Age=0; Path=/")!! - client.cookieJar.saveFromResponse(url, listOf(cookie)) + val oldKey = client.cookieJar + .loadForRequest(baseUrl.toHttpUrl()) + .firstOrNull { it.name == "mhub_access" && it.value.isNotEmpty() }?.value - // Set required cookie (for cache busting?) - val recently = buildJsonObject { - putJsonObject((now - (0..3600).random()).toString()) { - put("mangaID", (1..42_000).random()) - put("number", (1..20).random()) + for (i in 1..2) { + // Clear key cookie + val cookie = Cookie.parse(url, "mhub_access=; Max-Age=0; Path=/")!! + client.cookieJar.saveFromResponse(url, listOf(cookie)) + + // We try requesting again with param if the first one fails + val query = if (i == 2) "?reloadKey=1" else "" + + try { + val response = client.newCall(GET("$url$query", headers)).execute() + val returnedKey = response.headers["set-cookie"]?.let { regex.find(it)?.groupValues?.get(1) } + response.close() // Avoid potential resource leaks + + if (returnedKey != oldKey) break; // Break out of loop since we got an allegedly valid API key + } catch (_: IOException) { + throw IOException("An error occurred while obtaining a new API key") // Show error } - }.toString() - - client.cookieJar.saveFromResponse( - url, - listOf( - Cookie.Builder() - .domain(url.host) - .name("recently") - .value(URLEncoder.encode(recently, "utf-8")) - .expiresAt(now + 2 * 60 * 60 * 24 * 31) // +2 months - .build(), - ), - ) - - val request = GET("$url?reloadKey=1", headers) - client.newCall(request).execute() + } } data class SMangaDTO(