TM: Fix headers (#783)

* TM: Fix headers

* more proper header value regex
This commit is contained in:
beerpsi 2024-01-29 22:51:23 +07:00 committed by GitHub
parent 167e423d5b
commit 59d337d6a0
2 changed files with 30 additions and 2 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'Tsuki Mangás' extName = 'Tsuki Mangás'
extClass = '.TsukiMangas' extClass = '.TsukiMangas'
extVersionCode = 4 extVersionCode = 5
isNsfw = true isNsfw = true
} }

View File

@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.asJsoup
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.decodeFromStream
import okhttp3.HttpUrl import okhttp3.HttpUrl
@ -39,6 +40,7 @@ class TsukiMangas : HttpSource() {
override val client by lazy { override val client by lazy {
network.client.newBuilder() network.client.newBuilder()
.addInterceptor(::apiHeadersInterceptor)
.addInterceptor(::imageCdnSwapper) .addInterceptor(::imageCdnSwapper)
.rateLimitHost(baseUrl.toHttpUrl(), 2) .rateLimitHost(baseUrl.toHttpUrl(), 2)
.rateLimitHost(MAIN_CDN.toHttpUrl(), 1) .rateLimitHost(MAIN_CDN.toHttpUrl(), 1)
@ -48,7 +50,6 @@ class TsukiMangas : HttpSource() {
override fun headersBuilder() = super.headersBuilder() override fun headersBuilder() = super.headersBuilder()
.add("Referer", "$baseUrl/") .add("Referer", "$baseUrl/")
.add("X-Requested", "HttpsRequests")
private val json: Json by injectLazy() private val json: Json by injectLazy()
@ -261,6 +262,33 @@ class TsukiMangas : HttpSource() {
} }
} }
private val apiHeadersRegex = Regex("""headers\.common(?:\.(?<key>[0-9A-Za-z_]+)|\[['"](?<key2>[0-9A-Za-z-_]+)['"]])\s*=\s*['"](?<value>[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 { companion object {
const val PREFIX_SEARCH = "id:" const val PREFIX_SEARCH = "id:"