diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt index f32555b3c..800565938 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/Translator.kt @@ -26,7 +26,7 @@ val String.code2Locale: Locale by receiveLazy { val Locale.locale2code by receiveLazy { - if (StrUtil.isBlank(getCountry())) { + if (StrUtil.isBlank(country)) { language } else { "$language-$country" diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleWebTranslator.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleWebTranslator.kt index 889802e17..031a1a239 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleWebTranslator.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/transtale/source/GoogleWebTranslator.kt @@ -31,15 +31,29 @@ object GoogleWebTranslator : Translator { if (!GoogleWebTranslator::tkk.isInitialized) { - val response = HttpUtil.get("https://translate.google." + if (NekoConfig.translationProvider == 2) "cn" else "com") + val url = "https://translate.google." + if (NekoConfig.translationProvider == 2) "cn" else "com" - if (TextUtils.isEmpty(response)) { + val response = runCatching { + (if (NekoConfig.translationProvider == 2) HttpUtil.okHttpClientNoDoh else HttpUtil.okHttpClient).newCall(Request.Builder().url(url).applyUserAgent().build()).execute() + }.recoverCatching { + HttpUtil.okHttpClientWithCurrProxy.newCall(Request.Builder().url(url).applyUserAgent().build()).execute() + }.getOrThrow() + + if (response.code != 200) { + + error("HTTP ${response.code} : ${response.body?.string()}") + + } + + val html = response.body?.string() + + if (html.isNullOrBlank()) { error("Tkk init failed") } - val matcher = Pattern.compile("tkk\\s*[:=]\\s*['\"]([0-9]+)\\.([0-9]+)['\"]", Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE).matcher(response) + val matcher = Pattern.compile("tkk\\s*[:=]\\s*['\"]([0-9]+)\\.([0-9]+)['\"]", Pattern.CASE_INSENSITIVE or Pattern.UNICODE_CASE).matcher(html) tkk = if (matcher.find() && matcher.group(1) != null && matcher.group(2) != null) { longArrayOf(matcher.group(1).toLong(), matcher.group(2).toLong()) @@ -85,6 +99,4 @@ object GoogleWebTranslator : Translator { "sr", "st", "si", "eo", "sk", "sl", "sw", "gd", "ceb", "so", "tg", "te", "ta", "th", "tr", "cy", "ur", "uk", "uz", "es", "iw", "el", "haw", "sd", "hu", "sn", "hy", "ig", "it", "yi", "hi", "su", "id", "jw", "en", "yo", "vi", "zh-TW", "zh-CN", "zh") - - } diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/HttpUtil.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/HttpUtil.kt index 7473758d2..9b7163af6 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/HttpUtil.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/HttpUtil.kt @@ -1,5 +1,6 @@ package tw.nekomimi.nekogram.utils +import okhttp3.Dns import okhttp3.OkHttpClient import okhttp3.Request import org.telegram.messenger.SharedConfig @@ -19,16 +20,14 @@ fun Request.Builder.applyUserAgent(): Request.Builder { object HttpUtil { @JvmField - val okHttpClient = OkHttpClient().newBuilder() + val okHttpClient = OkHttpClient.Builder() .dns(DnsFactory) - .connectTimeout(2, TimeUnit.SECONDS) - .readTimeout(2, TimeUnit.SECONDS) + .connectTimeout(5, TimeUnit.SECONDS) + .readTimeout(5, TimeUnit.SECONDS) .build() - val okHttpClientNoDoh = OkHttpClient() - .newBuilder() - .connectTimeout(2, TimeUnit.SECONDS) - .readTimeout(2, TimeUnit.SECONDS) + val okHttpClientNoDoh = okHttpClient.newBuilder() + .dns(Dns.SYSTEM) .build() @JvmStatic @@ -50,7 +49,8 @@ object HttpUtil { } @JvmStatic - fun get(url: String): String { + @JvmOverloads + fun get(url: String, client: OkHttpClient = okHttpClient): String { val request = Request.Builder().url(url) .applyUserAgent() @@ -66,6 +66,23 @@ object HttpUtil { } + @JvmStatic + fun getNoDoh(url: String): String { + + val request = Request.Builder().url(url) + .applyUserAgent() + .build() + + okHttpClientNoDoh.newCall(request).execute().apply { + + val body = body + + return body?.string() ?: error("HTTP ERROR $code") + + } + + } + @JvmStatic fun getByteArray(url: String): ByteArray { diff --git a/bin/init_release.sh b/bin/init_release.sh index 0e045e10e..a2d71628f 100755 --- a/bin/init_release.sh +++ b/bin/init_release.sh @@ -2,8 +2,8 @@ function assemble() { - ./gradlew TMessagesProj:assembleRelease - ./gradlew TMessagesProj:assembleReleaseNoGcm + ./gradlew TMessagesProj:assembleRelease \ + TMessagesProj:assembleReleaseNoGcm return $? diff --git a/bin/upload_release.sh b/bin/upload_release.sh index d3ad7ccfa..675062c8c 100755 --- a/bin/upload_release.sh +++ b/bin/upload_release.sh @@ -22,4 +22,13 @@ upload outPath="TMessagesProj/build/outputs/apk/full/releaseNoGcm" +upload + +outPath="TMessagesProj/build/outputs/apk/mini/release" + +upload + + +outPath="TMessagesProj/build/outputs/apk/mini/releaseNoGcm" + upload \ No newline at end of file