From efbc6ad6e239ebd6a3788c7b61c47d17373929cb Mon Sep 17 00:00:00 2001 From: Chopper <156493704+choppeh@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:52:17 -0300 Subject: [PATCH] Fix image loading timeout and expired translator token --- .../extension/all/snowmtl/SnowmltFactory.kt | 2 ++ .../extension/all/snowmtl/Snowmtl.kt | 1 + .../interceptors/TranslationInterceptor.kt | 17 ++++++++----- .../all/snowmtl/translator/BingTranslator.kt | 24 +++++++++++++------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/SnowmltFactory.kt b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/SnowmltFactory.kt index ec6da7b53c6..8310b61d12a 100644 --- a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/SnowmltFactory.kt +++ b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/SnowmltFactory.kt @@ -15,5 +15,7 @@ data class Source(val lang: String, val target: String = lang, val origin: Strin private val languageList = listOf( Source("en"), Source("es"), + Source("id"), + Source("it"), Source("pt-BR", "pt"), ) diff --git a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/Snowmtl.kt b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/Snowmtl.kt index c859491949d..e6c460d641c 100644 --- a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/Snowmtl.kt +++ b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/Snowmtl.kt @@ -52,6 +52,7 @@ class Snowmtl( override val client = network.cloudflareClient.newBuilder() .rateLimit(2) + .readTimeout(2, TimeUnit.MINUTES) .addInterceptor(TranslationInterceptor(source, translator)) .addInterceptor(ComposedImageInterceptor(baseUrl, super.client)) .build() diff --git a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/interceptors/TranslationInterceptor.kt b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/interceptors/TranslationInterceptor.kt index 7ee5acb3c6a..a5a8387e42a 100644 --- a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/interceptors/TranslationInterceptor.kt +++ b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/interceptors/TranslationInterceptor.kt @@ -55,13 +55,18 @@ class TranslationInterceptor( translator.translate(source.origin, source.target, token).split(delimiter) } - return tokens.mapNotNull { token -> - val list = token.parseAs>() - val key = list.first() - val text = list.last() + return replaceDialoguesWithTranslations(tokens, mapping) + } - mapping[key]?.second?.dialog?.copy(text = text) - } + private fun replaceDialoguesWithTranslations( + tokens: List, + mapping: Map>, + ) = tokens.mapNotNull { token -> + val list = token.parseAs>() + val key = list.first() + val text = list.last() + + mapping[key]?.second?.dialog?.copy(text = text) } /** diff --git a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/translator/BingTranslator.kt b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/translator/BingTranslator.kt index 735946b61eb..fc347609384 100644 --- a/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/translator/BingTranslator.kt +++ b/src/all/snowmtl/src/eu/kanade/tachiyomi/extension/all/snowmtl/translator/BingTranslator.kt @@ -23,7 +23,7 @@ class BingTranslator(private val client: OkHttpClient, private val headers: Head private val json: Json by injectLazy() - private val tokens: TokenGroup by lazy { loadTokens() } + private var tokens = loadTokens() override val capacity: Int = MAX_CHARS_ALLOW @@ -36,12 +36,22 @@ class BingTranslator(private val client: OkHttpClient, private val headers: Head return text } - val dto = client - .newCall(translatorRequest(from, to, text)) - .execute() - .parseAs>() - - return dto.firstOrNull()?.text ?: text + var attempts = 0 + do { + try { + val dto = client + .newCall(translatorRequest(from, to, text)) + .execute() + .parseAs>() + + return dto.firstOrNull()?.text ?: text + } catch (e: Exception) { + tokens = loadTokens() + attempts++ + } + } while (attempts < 3) + + return text } private fun translatorRequest(from: String, to: String, text: String): Request {