Make rate limiting apply to all requests in MH (#6542)

* Make rate limit apply to images in MH.

* Update block message.
This commit is contained in:
Alessandro Jean 2021-04-14 10:19:58 -03:00 committed by GitHub
parent 42a1284781
commit 08e71f3454
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'Mangá Host'
pkgNameSuffix = 'pt.mangahost'
extClass = '.MangaHost'
extVersionCode = 22
extVersionCode = 23
libVersion = '1.2'
}

View File

@ -38,11 +38,10 @@ class MangaHost : ParsedHttpSource() {
override val supportsLatest = true
override val client: OkHttpClient = network.cloudflareClient.newBuilder()
.addInterceptor(::rateLimitIntercept)
.addInterceptor(RateLimitInterceptor(1, 3, TimeUnit.SECONDS))
.addInterceptor(::blockMessageIntercept)
.build()
private val rateLimitInterceptor = RateLimitInterceptor(1, 3, TimeUnit.SECONDS)
override fun headersBuilder(): Headers.Builder = Headers.Builder()
.add("Accept", ACCEPT)
.add("Accept-Language", ACCEPT_LANGUAGE)
@ -197,12 +196,15 @@ class MangaHost : ParsedHttpSource() {
return GET(page.imageUrl!!, newHeaders)
}
private fun rateLimitIntercept(chain: Interceptor.Chain): Response {
return if (chain.request().url().toString().contains(CDN_REGEX)) {
chain.proceed(chain.request())
} else {
rateLimitInterceptor.intercept(chain)
private fun blockMessageIntercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
if (!response.isSuccessful && response.code() == 403) {
response.close()
throw Exception(BLOCK_MESSAGE)
}
return response
}
private fun Call.asObservableIgnoreCode(code: Int): Observable<Response> {
@ -246,6 +248,8 @@ class MangaHost : ParsedHttpSource() {
private val IMAGE_REGEX = "_(small|medium|xmedium)\\.".toRegex()
private val CDN_REGEX = "/mangas_files/.*\\.jpg".toRegex()
private const val BLOCK_MESSAGE = "O site está bloqueando o Tachiyomi. Migre para outra fonte caso o problema persistir."
private val DATE_FORMAT by lazy {
SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH)
}