diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index d7a8c1c28..a10f7a7df 100644 --- a/src/all/mangadex/build.gradle +++ b/src/all/mangadex/build.gradle @@ -6,12 +6,8 @@ ext { extName = 'MangaDex' pkgNameSuffix = 'all.mangadex' extClass = '.MangaDexFactory' - extVersionCode = 166 + extVersionCode = 167 isNsfw = true } -dependencies { - compileOnly(libs.okhttp) -} - -apply from: "$rootDir/common.gradle" \ No newline at end of file +apply from: "$rootDir/common.gradle" diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt index 4d0679bf4..11841693d 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt @@ -18,6 +18,7 @@ import eu.kanade.tachiyomi.extension.all.mangadex.dto.MangaListDto import eu.kanade.tachiyomi.extension.all.mangadex.dto.RelationshipDto import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.asObservableSuccess +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.MangasPage @@ -59,9 +60,8 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .add("User-Agent", "Tachiyomi " + System.getProperty("http.agent")) override val client = network.client.newBuilder() - .addNetworkInterceptor(mdRateLimitInterceptor) - .addInterceptor(coverInterceptor) - .addInterceptor(MdAtHomeReportInterceptor(network.client, headersBuilder().build())) + .rateLimit(3) + .addInterceptor(MdAtHomeReportInterceptor(network.client, headers)) .build() // POPULAR Manga Section diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexInterceptors.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt similarity index 69% rename from src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexInterceptors.kt rename to src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt index d32d90cfc..1aa2dfc8e 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDexInterceptors.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdAtHomeReportInterceptor.kt @@ -13,18 +13,6 @@ import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.Response import uy.kohesive.injekt.injectLazy -/** - * Rate limit requests ignore covers though - */ -private val coverRegex = Regex("""/images/.*\.jpg""") -private val baseInterceptor = RateLimitInterceptor(3) -val mdRateLimitInterceptor = Interceptor { chain -> - return@Interceptor when (chain.request().url.toString().contains(coverRegex)) { - true -> chain.proceed(chain.request()) - false -> baseInterceptor.intercept(chain) - } -} - /** * Interceptor to post to md@home for MangaDex Stats */ @@ -74,21 +62,3 @@ class MdAtHomeReportInterceptor( } } } - -val coverInterceptor = Interceptor { chain -> - val originalRequest = chain.request() - return@Interceptor chain.proceed(chain.request()).let { response -> - if (response.code == 404 && originalRequest.url.toString() - .contains(coverRegex) - ) { - response.close() - chain.proceed( - originalRequest.newBuilder().url( - originalRequest.url.toString().substringBeforeLast(".") + ".thumb.jpg" - ).build() - ) - } else { - response - } - } -} diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/RateLimitInterceptor.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/RateLimitInterceptor.kt deleted file mode 100644 index 3d2ca34ae..000000000 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/RateLimitInterceptor.kt +++ /dev/null @@ -1,58 +0,0 @@ -package eu.kanade.tachiyomi.extension.all.mangadex - -import android.os.SystemClock -import okhttp3.Interceptor -import okhttp3.Response -import java.util.concurrent.TimeUnit - -/** - * An OkHttp interceptor that handles rate limiting. - * - * Examples: - * - * permits = 5, period = 1, unit = seconds => 5 requests per second - * permits = 10, period = 2, unit = minutes => 10 requests per 2 minutes - * - * @param permits {Int} Number of requests allowed within a period of units. - * @param period {Long} The limiting duration. Defaults to 1. - * @param unit {TimeUnit} The unit of time for the period. Defaults to seconds. - */ -class RateLimitInterceptor( - private val permits: Int, - private val period: Long = 1, - private val unit: TimeUnit = TimeUnit.SECONDS -) : Interceptor { - - private val requestQueue = ArrayList(permits) - private val rateLimitMillis = unit.toMillis(period) - - override fun intercept(chain: Interceptor.Chain): Response { - synchronized(requestQueue) { - val now = SystemClock.elapsedRealtime() - val waitTime = if (requestQueue.size < permits) { - 0 - } else { - val oldestReq = requestQueue[0] - val newestReq = requestQueue[permits - 1] - - if (newestReq - oldestReq > rateLimitMillis) { - 0 - } else { - oldestReq + rateLimitMillis - now // Remaining time - } - } - - if (requestQueue.size == permits) { - requestQueue.removeAt(0) - } - if (waitTime > 0) { - requestQueue.add(now + waitTime) - Thread.sleep(waitTime) // Sleep inside synchronized to pause queued requests - } else { - requestQueue.add(now) - } - } - - return chain.proceed(chain.request()) - } -} diff --git a/src/zh/boylove/build.gradle b/src/zh/boylove/build.gradle index 3b15c9efc..b5ed97a48 100644 --- a/src/zh/boylove/build.gradle +++ b/src/zh/boylove/build.gradle @@ -6,7 +6,7 @@ ext { extName = 'BoyLove' pkgNameSuffix = 'zh.boylove' extClass = '.BoyLove' - extVersionCode = 1 + extVersionCode = 2 isNsfw = true } diff --git a/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/BoyLove.kt b/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/BoyLove.kt index 044696d7b..8d7c55045 100644 --- a/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/BoyLove.kt +++ b/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/BoyLove.kt @@ -7,6 +7,7 @@ import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.asObservableSuccess +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.source.ConfigurableSource import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList @@ -41,7 +42,7 @@ class BoyLove : HttpSource(), ConfigurableSource { .coerceIn(0, MIRRORS.size - 1).let { MIRRORS[it] } override val client = network.client.newBuilder() - .addInterceptor(NonblockingRateLimiter(2)) + .rateLimit(2) .build() override fun popularMangaRequest(page: Int): Request = diff --git a/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/NonblockingRateLimiter.kt b/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/NonblockingRateLimiter.kt deleted file mode 100644 index 3e6715d25..000000000 --- a/src/zh/boylove/src/eu/kanade/tachiyomi/extension/zh/boylove/NonblockingRateLimiter.kt +++ /dev/null @@ -1,58 +0,0 @@ -package eu.kanade.tachiyomi.extension.zh.boylove - -import android.os.SystemClock -import okhttp3.Interceptor -import okhttp3.Response -import java.io.IOException -import java.util.concurrent.TimeUnit - -// See https://github.com/tachiyomiorg/tachiyomi/pull/7389 -internal class NonblockingRateLimiter( - private val permits: Int, - period: Long = 1, - unit: TimeUnit = TimeUnit.SECONDS, -) : Interceptor { - - private val requestQueue = ArrayList(permits) - private val rateLimitMillis = unit.toMillis(period) - - override fun intercept(chain: Interceptor.Chain): Response { - // Ignore canceled calls, otherwise they would jam the queue - if (chain.call().isCanceled()) { - throw IOException() - } - - synchronized(requestQueue) { - val now = SystemClock.elapsedRealtime() - val waitTime = if (requestQueue.size < permits) { - 0 - } else { - val oldestReq = requestQueue[0] - val newestReq = requestQueue[permits - 1] - - if (newestReq - oldestReq > rateLimitMillis) { - 0 - } else { - oldestReq + rateLimitMillis - now // Remaining time - } - } - - // Final check - if (chain.call().isCanceled()) { - throw IOException() - } - - if (requestQueue.size == permits) { - requestQueue.removeAt(0) - } - if (waitTime > 0) { - requestQueue.add(now + waitTime) - Thread.sleep(waitTime) // Sleep inside synchronized to pause queued requests - } else { - requestQueue.add(now) - } - } - - return chain.proceed(chain.request()) - } -} diff --git a/src/zh/sixmh/build.gradle b/src/zh/sixmh/build.gradle index f614151dd..b2784603d 100644 --- a/src/zh/sixmh/build.gradle +++ b/src/zh/sixmh/build.gradle @@ -6,7 +6,7 @@ ext { extName = '6Manhua' pkgNameSuffix = 'zh.sixmh' extClass = '.SixMH' - extVersionCode = 2 + extVersionCode = 3 } apply from: "$rootDir/common.gradle" diff --git a/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/NonblockingRateLimiter.kt b/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/NonblockingRateLimiter.kt deleted file mode 100644 index abcc45e6d..000000000 --- a/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/NonblockingRateLimiter.kt +++ /dev/null @@ -1,58 +0,0 @@ -package eu.kanade.tachiyomi.extension.zh.sixmh - -import android.os.SystemClock -import okhttp3.Interceptor -import okhttp3.Response -import java.io.IOException -import java.util.concurrent.TimeUnit - -// See https://github.com/tachiyomiorg/tachiyomi/pull/7389 -internal class NonblockingRateLimiter( - private val permits: Int, - period: Long = 1, - unit: TimeUnit = TimeUnit.SECONDS, -) : Interceptor { - - private val requestQueue = ArrayList(permits) - private val rateLimitMillis = unit.toMillis(period) - - override fun intercept(chain: Interceptor.Chain): Response { - // Ignore canceled calls, otherwise they would jam the queue - if (chain.call().isCanceled()) { - throw IOException() - } - - synchronized(requestQueue) { - val now = SystemClock.elapsedRealtime() - val waitTime = if (requestQueue.size < permits) { - 0 - } else { - val oldestReq = requestQueue[0] - val newestReq = requestQueue[permits - 1] - - if (newestReq - oldestReq > rateLimitMillis) { - 0 - } else { - oldestReq + rateLimitMillis - now // Remaining time - } - } - - // Final check - if (chain.call().isCanceled()) { - throw IOException() - } - - if (requestQueue.size == permits) { - requestQueue.removeAt(0) - } - if (waitTime > 0) { - requestQueue.add(now + waitTime) - Thread.sleep(waitTime) // Sleep inside synchronized to pause queued requests - } else { - requestQueue.add(now) - } - } - - return chain.proceed(chain.request()) - } -} diff --git a/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/SixMH.kt b/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/SixMH.kt index dcbfd7910..c774c467c 100644 --- a/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/SixMH.kt +++ b/src/zh/sixmh/src/eu/kanade/tachiyomi/extension/zh/sixmh/SixMH.kt @@ -4,6 +4,7 @@ import com.github.stevenyomi.unpacker.Unpacker import eu.kanade.tachiyomi.AppInfo import eu.kanade.tachiyomi.network.GET import eu.kanade.tachiyomi.network.POST +import eu.kanade.tachiyomi.network.interceptor.rateLimit import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.SChapter @@ -33,7 +34,7 @@ class SixMH : ParsedHttpSource() { private val json: Json by injectLazy() override val client = network.client.newBuilder() - .addInterceptor(NonblockingRateLimiter(2)) + .rateLimit(2) .build() override fun popularMangaRequest(page: Int) = GET("$PC_URL/rank/1-$page.html", headers)