Fix google cn translator

This commit is contained in:
世界 2020-09-23 16:13:09 +00:00
parent f55065cb80
commit 25ca374340
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
5 changed files with 54 additions and 16 deletions

View File

@ -26,7 +26,7 @@ val String.code2Locale: Locale by receiveLazy<String, Locale> {
val Locale.locale2code by receiveLazy<Locale, String> { val Locale.locale2code by receiveLazy<Locale, String> {
if (StrUtil.isBlank(getCountry())) { if (StrUtil.isBlank(country)) {
language language
} else { } else {
"$language-$country" "$language-$country"

View File

@ -31,15 +31,29 @@ object GoogleWebTranslator : Translator {
if (!GoogleWebTranslator::tkk.isInitialized) { 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") 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) { tkk = if (matcher.find() && matcher.group(1) != null && matcher.group(2) != null) {
longArrayOf(matcher.group(1).toLong(), matcher.group(2).toLong()) 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", "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", "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") "hy", "ig", "it", "yi", "hi", "su", "id", "jw", "en", "yo", "vi", "zh-TW", "zh-CN", "zh")
} }

View File

@ -1,5 +1,6 @@
package tw.nekomimi.nekogram.utils package tw.nekomimi.nekogram.utils
import okhttp3.Dns
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import org.telegram.messenger.SharedConfig import org.telegram.messenger.SharedConfig
@ -19,16 +20,14 @@ fun Request.Builder.applyUserAgent(): Request.Builder {
object HttpUtil { object HttpUtil {
@JvmField @JvmField
val okHttpClient = OkHttpClient().newBuilder() val okHttpClient = OkHttpClient.Builder()
.dns(DnsFactory) .dns(DnsFactory)
.connectTimeout(2, TimeUnit.SECONDS) .connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(2, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS)
.build() .build()
val okHttpClientNoDoh = OkHttpClient() val okHttpClientNoDoh = okHttpClient.newBuilder()
.newBuilder() .dns(Dns.SYSTEM)
.connectTimeout(2, TimeUnit.SECONDS)
.readTimeout(2, TimeUnit.SECONDS)
.build() .build()
@JvmStatic @JvmStatic
@ -50,7 +49,8 @@ object HttpUtil {
} }
@JvmStatic @JvmStatic
fun get(url: String): String { @JvmOverloads
fun get(url: String, client: OkHttpClient = okHttpClient): String {
val request = Request.Builder().url(url) val request = Request.Builder().url(url)
.applyUserAgent() .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 @JvmStatic
fun getByteArray(url: String): ByteArray { fun getByteArray(url: String): ByteArray {

View File

@ -2,8 +2,8 @@
function assemble() { function assemble() {
./gradlew TMessagesProj:assembleRelease ./gradlew TMessagesProj:assembleRelease \
./gradlew TMessagesProj:assembleReleaseNoGcm TMessagesProj:assembleReleaseNoGcm
return $? return $?

View File

@ -22,4 +22,13 @@ upload
outPath="TMessagesProj/build/outputs/apk/full/releaseNoGcm" outPath="TMessagesProj/build/outputs/apk/full/releaseNoGcm"
upload
outPath="TMessagesProj/build/outputs/apk/mini/release"
upload
outPath="TMessagesProj/build/outputs/apk/mini/releaseNoGcm"
upload upload