From 59d337d6a0709d25b45cbdfec720d4807c320b0a Mon Sep 17 00:00:00 2001 From: beerpsi <92439990+beerpiss@users.noreply.github.com> Date: Mon, 29 Jan 2024 22:51:23 +0700 Subject: [PATCH] TM: Fix headers (#783) * TM: Fix headers * more proper header value regex --- src/pt/tsukimangas/build.gradle | 2 +- .../extension/pt/tsukimangas/TsukiMangas.kt | 30 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/pt/tsukimangas/build.gradle b/src/pt/tsukimangas/build.gradle index f086e0169..648d31133 100644 --- a/src/pt/tsukimangas/build.gradle +++ b/src/pt/tsukimangas/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Tsuki Mangás' extClass = '.TsukiMangas' - extVersionCode = 4 + extVersionCode = 5 isNsfw = true } diff --git a/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt b/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt index 580ec8013..08bcba934 100644 --- a/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt +++ b/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt @@ -13,6 +13,7 @@ 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.HttpSource +import eu.kanade.tachiyomi.util.asJsoup import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream import okhttp3.HttpUrl @@ -39,6 +40,7 @@ class TsukiMangas : HttpSource() { override val client by lazy { network.client.newBuilder() + .addInterceptor(::apiHeadersInterceptor) .addInterceptor(::imageCdnSwapper) .rateLimitHost(baseUrl.toHttpUrl(), 2) .rateLimitHost(MAIN_CDN.toHttpUrl(), 1) @@ -48,7 +50,6 @@ class TsukiMangas : HttpSource() { override fun headersBuilder() = super.headersBuilder() .add("Referer", "$baseUrl/") - .add("X-Requested", "HttpsRequests") private val json: Json by injectLazy() @@ -261,6 +262,33 @@ class TsukiMangas : HttpSource() { } } + private val apiHeadersRegex = Regex("""headers\.common(?:\.(?[0-9A-Za-z_]+)|\[['"](?[0-9A-Za-z-_]+)['"]])\s*=\s*['"](?[a-zA-Z0-9_ :;.,\\/"'?!(){}\[\]@<>=\-+*#$&`|~^%]+)['"]""") + + private val apiHeaders by lazy { + val document = client.newCall(GET(baseUrl, headers)).execute().asJsoup() + val scriptUrl = document.selectFirst("script[src*=index-]")!!.absUrl("src") + val script = client.newCall(GET(scriptUrl, headers)).execute().body.string() + val matches = apiHeadersRegex.findAll(script) + + matches.associate { + (it.groups["key"] ?: it.groups["key2"]!!).value to it.groups["value"]!!.value + } + } + + private fun apiHeadersInterceptor(chain: Interceptor.Chain): Response { + val request = chain.request() + + if (!request.url.encodedPath.startsWith(API_PATH)) { + return chain.proceed(request) + } + + val newRequest = request.newBuilder().apply { + apiHeaders.entries.forEach { addHeader(it.key, it.value) } + }.build() + + return chain.proceed(newRequest) + } + companion object { const val PREFIX_SEARCH = "id:"