From 871c4d7a222e204bde782a9fb9cdbf2569542aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Cirella?= Date: Sat, 18 May 2024 12:06:19 -0300 Subject: [PATCH] Anchira: Fix API changes (#3089) * Anchira: Fix API changes * Anchira: Update versin code * Anchira: Add interceptor for resampled images, update data URL * Apply suggestions from code review --- src/en/anchira/build.gradle | 2 +- .../tachiyomi/extension/en/anchira/Anchira.kt | 39 ++++++++----------- .../extension/en/anchira/AnchiraHelper.kt | 2 + 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/en/anchira/build.gradle b/src/en/anchira/build.gradle index 73350b955..18ccc6457 100644 --- a/src/en/anchira/build.gradle +++ b/src/en/anchira/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Anchira' extClass = '.Anchira' - extVersionCode = 13 + extVersionCode = 14 isNsfw = true } diff --git a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt index 419714ee8..785203e6e 100644 --- a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt +++ b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/Anchira.kt @@ -7,6 +7,7 @@ import androidx.preference.ListPreference import androidx.preference.PreferenceScreen import androidx.preference.SwitchPreferenceCompat import eu.kanade.tachiyomi.extension.en.anchira.AnchiraHelper.createChapter +import eu.kanade.tachiyomi.extension.en.anchira.AnchiraHelper.getCdn import eu.kanade.tachiyomi.extension.en.anchira.AnchiraHelper.getPathFromUrl import eu.kanade.tachiyomi.extension.en.anchira.AnchiraHelper.prepareTags import eu.kanade.tachiyomi.network.GET @@ -43,7 +44,7 @@ class Anchira : HttpSource(), ConfigurableSource { override val baseUrl = "https://anchira.to" - private val apiUrl = "$baseUrl/api/v1" + private val apiUrl = baseUrl.replace("://", "://api.") private val libraryUrl = "$apiUrl/library" @@ -55,7 +56,7 @@ class Anchira : HttpSource(), ConfigurableSource { override val client: OkHttpClient = network.cloudflareClient.newBuilder() .rateLimit(3, 1, TimeUnit.SECONDS) - .addInterceptor { apiInterceptor(it) } + .addInterceptor { resampledInterceptor(it) } .build() private val json = Json { ignoreUnknownKeys = true } @@ -64,7 +65,9 @@ class Anchira : HttpSource(), ConfigurableSource { Injekt.get().getSharedPreferences("source_$id", 0x0000) } - override fun headersBuilder() = super.headersBuilder().add("X-Requested-With", "XMLHttpRequest") + override fun headersBuilder() = super.headersBuilder() + .add("Referer", "$baseUrl/") + .add("Origin", baseUrl) // Latest @@ -319,7 +322,7 @@ class Anchira : HttpSource(), ConfigurableSource { return data.images.mapIndexed { i, image -> Page( i, - imageUrl = "$cdnUrl/${imageData.id}/${imageData.key}/${imageData.hash}/b/${image.name}", + imageUrl = "${getCdn(i)}/${imageData.id}/${imageData.key}/${imageData.hash}/b/${image.name}", ) } } @@ -429,28 +432,20 @@ class Anchira : HttpSource(), ConfigurableSource { private val SharedPreferences.useTagGrouping get() = getBoolean(USE_TAG_GROUPING, false) - private fun apiInterceptor(chain: Interceptor.Chain): Response { + private fun resampledInterceptor(chain: Interceptor.Chain): Response { val request = chain.request() - val requestUrl = request.url.toString() + val url = request.url.toString() - return if (requestUrl.contains("/api/v1")) { - val newRequestBuilder = request.newBuilder() + return if (url.contains("sexo.xyz")) { + val response = chain.proceed(request) - if (requestUrl.contains(Regex("/\\d+/\\S+"))) { - newRequestBuilder.header( - "Referer", - requestUrl.replace(libraryUrl, "$baseUrl/g"), - ) - } else if (requestUrl.contains("user/favorites")) { - newRequestBuilder.header( - "Referer", - requestUrl.replace("$apiUrl/user/favorites", "$baseUrl/favorites"), - ) - } else { - newRequestBuilder.header("Referer", requestUrl.replace(libraryUrl, baseUrl)) + if (response.isSuccessful) { + return response + } else if (url.contains("/b/")) { + return chain.proceed(request.newBuilder().url(url.replace("/b/", "/a/")).build()) } - chain.proceed(newRequestBuilder.build()) + throw IOException("An error occurred while loading the image - ${response.code}") } else { chain.proceed(request) } @@ -474,7 +469,7 @@ class Anchira : HttpSource(), ConfigurableSource { private const val OPEN_SOURCE_PREF = "use_manga_source" private const val USE_TAG_GROUPING = "use_tag_grouping" private const val DATA_JSON = - "https://gist.githubusercontent.com/LetrixZ/2b559cc5829d1c221c701e02ecd81411/raw/data-v5.json" + "https://raw.githubusercontent.com/LetrixZ/gallery-data/main/extension_data.min.json" } } diff --git a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/AnchiraHelper.kt b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/AnchiraHelper.kt index 1e49cf7f2..4f83dbe5e 100644 --- a/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/AnchiraHelper.kt +++ b/src/en/anchira/src/eu/kanade/tachiyomi/extension/en/anchira/AnchiraHelper.kt @@ -50,6 +50,8 @@ object AnchiraHelper { } } + fun getCdn(page: Int) = if (page % 2 == 0) "https://kisakisexo.xyz" else "https://aronasexo.xyz" + private fun String.titleCase() = replaceFirstChar { if (it.isLowerCase()) { it.titlecase(Locale.getDefault())