OnePieceEx: Fix Brotli issue (#206)

This commit is contained in:
beerpsi 2024-01-13 13:37:57 +07:00 committed by GitHub
parent 389b6313b4
commit 1fe8cf0bcf
2 changed files with 8 additions and 31 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'One Piece Ex'
pkgNameSuffix = 'pt.opex'
extClass = '.OnePieceEx'
extVersionCode = 6
extVersionCode = 7
}
apply from: "$rootDir/common.gradle"

View File

@ -14,11 +14,9 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import rx.Observable
@ -37,8 +35,14 @@ class OnePieceEx : ParsedHttpSource() {
override val supportsLatest = false
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.addInterceptor(::bypassHttp103Intercept)
.rateLimit(1, 2, TimeUnit.SECONDS)
.apply {
val interceptors = interceptors()
val index = interceptors.indexOfFirst { "Brotli" in it.javaClass.simpleName }
if (index >= 0) {
interceptors.add(interceptors.removeAt(index))
}
}
.build()
override fun headersBuilder(): Headers.Builder = Headers.Builder()
@ -230,33 +234,6 @@ class OnePieceEx : ParsedHttpSource() {
override fun latestUpdatesNextPageSelector() = throw UnsupportedOperationException("Not used")
private fun bypassHttp103Intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
if (request.url.pathSegments[0] != "mangas") {
return chain.proceed(request)
}
val bypasserUrl = "https://translate.google.com/translate".toHttpUrl().newBuilder()
.addQueryParameter("pto", "op")
.addQueryParameter("u", request.url.toString())
.build()
val bypasserRequest = request.newBuilder()
.url(bypasserUrl)
.build()
val bypasserResponse = chain.proceed(bypasserRequest)
val fixedBody = bypasserResponse.body.string()
.replace("onepieceex-net.translate.goog", baseUrl.removePrefix("https://"))
.toResponseBody(bypasserResponse.body.contentType())
return bypasserResponse.newBuilder()
.body(fixedBody)
.request(request)
.build()
}
companion object {
private const val ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9," +
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"