Skip to content

Commit

Permalink
fix: remove deepl translatioin
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Oct 19, 2024
1 parent 92ad451 commit a56c9b2
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
override fun translate(translationMode: TranslationMode) {
when (translationMode) {
TranslationMode.TRANSLATE_BY_PARAGRAPH -> translateByParagraph(TRANSLATE_API.GOOGLE)

TranslationMode.PAPAGO_TRANSLATE_BY_PARAGRAPH -> translateByParagraph(TRANSLATE_API.PAPAGO)
TranslationMode.DEEPL_BY_PARAGRAPH -> translateByParagraph(TRANSLATE_API.DEEPL)

TranslationMode.PAPAGO_TRANSLATE_BY_SCREEN -> translateWebView()
TranslationMode.GOOGLE_IN_PLACE -> ebWebView.addGoogleTranslation()
Expand All @@ -674,8 +674,10 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
LanguageSettingDialogFragment(translateApi, translationViewModel) {
if (translateApi == TRANSLATE_API.GOOGLE) {
translateByParagraph(TRANSLATE_API.GOOGLE)
} else {
} else if (translateApi == TRANSLATE_API.PAPAGO) {
translateByParagraph(TRANSLATE_API.PAPAGO)
} else if (translateApi == TRANSLATE_API.DEEPL) {
translateByParagraph(TRANSLATE_API.DEEPL)
}
}
.show(supportFragmentManager, "LanguageSettingDialog")
Expand Down Expand Up @@ -1232,10 +1234,6 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
webView: EBWebView = ebWebView,
) {
translateByParagraphInPlace(translateApi, webView)
// if (config.enableInplaceParagraphTranslate) {
// } else {
// translateByParagraphInReaderMode(translateApi)
// }
}

private fun translateByParagraphInPlace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import info.plateaukao.einkbro.viewmodel.TRANSLATE_API
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.core.component.KoinComponent
Expand All @@ -23,6 +24,9 @@ class JsWebInterface(private val webView: EBWebView) :
// to control the translation request threshold
private val semaphoreForTranslate = Semaphore(4)

// deepL has a limit of 5 requests per second
private val semaphoreForDeepL = Semaphore(1)

@JavascriptInterface
fun getAnchorPosition(left: Float, top: Float, right: Float, bottom: Float) {
Log.d("touch", "rect: $left, $top, $right, $bottom")
Expand All @@ -35,19 +39,32 @@ class JsWebInterface(private val webView: EBWebView) :
val translateApi = webView.translateApi

GlobalScope.launch(Dispatchers.IO) {
semaphoreForTranslate.acquire()
if (translateApi == TRANSLATE_API.DEEPL) {
semaphoreForDeepL.acquire()
} else {
semaphoreForTranslate.acquire()
}

Log.d("JsWebInterface", "getTranslation: $originalText")
val translatedString = if (translateApi == TRANSLATE_API.PAPAGO) {
translateRepository.ppTranslate(
translateRepository.pTranslate(
originalText,
configManager.translationLanguage.value,
).orEmpty()
} else {
} else if (translateApi == TRANSLATE_API.GOOGLE) {
translateRepository.gTranslateWithApi(
originalText,
configManager.translationLanguage.value
)
}.orEmpty()
).orEmpty()
} else if (translateApi == TRANSLATE_API.DEEPL) {
translateRepository.deepLTranslate(
originalText,
configManager.translationLanguage
).orEmpty()
} else {
""
}

withContext(Dispatchers.Main) {
if (webView.isAttachedToWindow && translatedString.isNotEmpty()) {
webView.evaluateJavascript(
Expand All @@ -56,7 +73,12 @@ class JsWebInterface(private val webView: EBWebView) :
)
}
}
semaphoreForTranslate.release()
if (translateApi == TRANSLATE_API.DEEPL) {
delay(300)
semaphoreForDeepL.release()
} else {
semaphoreForTranslate.release()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ class ConfigManager(
K_GPT_USER_PROMPT_WEB_PAGE,
"Summarize in 300 words:"
)
var papagoApiSecret by StringPreference(sp, K_PAPAGO_API_SECRET, "")
var imageApiKey by StringPreference(sp, K_IMAGE_API_KEY, "")
var gptModel by StringPreference(sp, K_GPT_MODEL, "gpt-3.5-turbo")
var alternativeModel by StringPreference(sp, K_ALTERNATIVE_MODEL, gptModel)
Expand Down Expand Up @@ -1082,7 +1081,8 @@ enum class TranslationMode(val labelResId: Int) {
GOOGLE_IN_PLACE(R.string.google_in_place),
TRANSLATE_BY_PARAGRAPH(R.string.translate_by_paragraph),
PAPAGO_TRANSLATE_BY_PARAGRAPH(R.string.papago_translate_by_paragraph),
PAPAGO_TRANSLATE_BY_SCREEN(R.string.papago_translate_by_screen)
PAPAGO_TRANSLATE_BY_SCREEN(R.string.papago_translate_by_screen),
DEEPL_BY_PARAGRAPH(R.string.deepl_translate_by_paragraph),
}

enum class FontType(val resId: Int) {
Expand Down
128 changes: 13 additions & 115 deletions app/src/main/java/info/plateaukao/einkbro/service/TranslateRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Bitmap
import android.util.Base64
import android.util.Log
import info.plateaukao.einkbro.preference.ConfigManager
import info.plateaukao.einkbro.util.TranslationLanguage
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import okhttp3.FormBody
Expand Down Expand Up @@ -37,34 +38,6 @@ class TranslateRepository : KoinComponent {
private val client = OkHttpClient()
private val config: ConfigManager by inject()

suspend fun gTranslateWithWeb(
text: String,
targetLanguage: String = "en",
sourceLanguage: String = "auto",
): String? {
return withContext(IO) {
val url = HttpUrl.Builder()
.scheme("https")
.host("translate.google.com")
.addPathSegment("m")
.addQueryParameter("tl", targetLanguage)
.addQueryParameter("sl", sourceLanguage)
.addQueryParameter("q", text)
.build()

val request = Request.Builder().url(url).build()

client.newCall(request).execute().use { response ->
val body = response.body?.string() ?: return@use null

Jsoup.parse(body)
.body()
.getElementsByClass("result-container")
.first()?.text()
}
}
}

private fun getICount(translateText: String): Int {
return translateText.split("i").size - 1
}
Expand Down Expand Up @@ -98,9 +71,16 @@ class TranslateRepository : KoinComponent {

suspend fun deepLTranslate(
text: String,
targetLanguage: String = "zh",
sourceLanguage: String = "auto",
targetLanguage: TranslationLanguage,
): String? {
val target = when (targetLanguage) {
TranslationLanguage.ZH_TW,
TranslationLanguage.ZH_CN,
-> "zh"

else -> config.translationLanguage.value
}

val headers = Headers.Builder()
.add("content-type", "application/json")
.build()
Expand All @@ -113,8 +93,8 @@ class TranslateRepository : KoinComponent {
put("params", JSONObject().apply {
put("splitting", "newlines")
put("lang", JSONObject().apply {
put("source_lang_user_selected", sourceLanguage.uppercase(Locale.getDefault()))
put("target_lang", targetLanguage.uppercase(Locale.getDefault()))
put("source_lang_user_selected", "AUTO")
put("target_lang", target.uppercase(Locale.getDefault()))
})
put("texts", JSONArray().apply {
put(JSONObject().apply {
Expand Down Expand Up @@ -195,42 +175,6 @@ class TranslateRepository : KoinComponent {
}
}

suspend fun pTranslate(
text: String,
targetLanguage: String = "en",
sourceLanguage: String = "auto",
): String? {
return withContext(IO) {
val request = Request.Builder()
.url("https://openapi.naver.com/v1/papago/n2mt")
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("X-Naver-Client-Id", "wCrZZHNa1x_wi8tkGERB")
.addHeader("X-Naver-Client-Secret", config.papagoApiSecret)
.post(
FormBody.Builder()
.add("source", sourceLanguage)
.add("target", targetLanguage)
.add("text", text)
.build()
)
.build()

try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) return@use null

val body = JSONObject(response.body?.string() ?: return@use null)
body.getJSONObject("message")
.getJSONObject("result")
.getString("translatedText")
}
} catch (e: Exception) {
Log.e("TranslateRepository", "pTranslate: $e")
null
}
}
}

private fun getAuthKey(): String? {
val url = "https://papago.naver.com"
val response = client.newCall(Request.Builder().url(url).build()).execute()
Expand All @@ -255,7 +199,7 @@ class TranslateRepository : KoinComponent {
}

private var authKey: String? = null
suspend fun ppTranslate(
suspend fun pTranslate(
text: String,
targetLanguage: String = "en",
sourceLanguage: String = "auto",
Expand Down Expand Up @@ -313,52 +257,6 @@ class TranslateRepository : KoinComponent {
}
}

suspend fun pDetectLanguage(text: String): String? {
if (authKey == null) {
authKey = getAuthKey()
}
val key = authKey?.toByteArray(Charsets.UTF_8) ?: return ""

val guid = UUID.randomUUID()
val timestamp = System.currentTimeMillis()
val code = "$guid\n$DETECT_LANGUAGE_URL\n$timestamp".toByteArray(Charsets.UTF_8)
val hmac = Mac.getInstance("HmacMD5")
val secretKeySpec = SecretKeySpec(key, "HmacMD5")
hmac.init(secretKeySpec)
val token = Base64.encodeToString(hmac.doFinal(code), Base64.DEFAULT)

return withContext(IO) {
val request = Request.Builder()
.url(DETECT_LANGUAGE_URL)
.addHeader("device-type", "pc")
.addHeader("x-apigw-partnerid", "papago")
.addHeader("Origin", "https://papago.naver.com")
.addHeader("Referer", "https://papago.naver.com")
.addHeader("Authorization", "PPG $guid:$token".replace("\n", ""))
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Timestamp", timestamp.toString())
.post(
FormBody.Builder()
.add("query", text)
.build()
)
.build()

try {
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) return@use null

val body = JSONObject(response.body?.string() ?: return@use null)
body.getString("langCode")
}
} catch (e: Exception) {
Log.d("TranslateRepository", "pDetectLanguage: $e")
null
}
}
}


private val sid: String by lazy { "${P_IMAGE_API_VERSION}${UUID.randomUUID()}" }

private fun signUrl(url: String): Signature {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ private fun ContextMenuItems(
ContextMenuItem(R.string.text_select, showIcons, Icons.AutoMirrored.Outlined.Segment) {
onClicked(SelectText)
}
if (shouldShowTranslateImage && (url.endsWith(".jpg") || url.endsWith(".png"))) {
val lowerCaseUrl = url.lowercase()
if (shouldShowTranslateImage && (lowerCaseUrl.contains("jpg") || lowerCaseUrl.contains("png"))) {
ContextMenuItem(R.string.translate, showIcons, iconResId = R.drawable.ic_papago) {
onClicked(TranslateImage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ToolbarConfigDialogFragment : ComposeDialogFragment() {
.filter { it.isAddable }
.filterNot { config.toolbarActions.contains(it) }
// hide papago action if papago api key is not set
.filterNot { config.papagoApiSecret.isBlank() && it == ToolbarAction.PapagoByParagraph }
.filterNot { config.imageApiKey.isBlank() && it == ToolbarAction.PapagoByParagraph }
.map { ToolbarActionItemInfo(it, false) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TranslateDialogFragment(
MyTheme {
TranslateResponse(
translationViewModel,
showExtraIcons = config.papagoApiSecret.isNotBlank(),
showExtraIcons = config.imageApiKey.isNotBlank(),
this::changeTranslationLanguage,
this::getTranslationWebView,
closeAction ?: { dismiss() }
Expand Down Expand Up @@ -163,9 +163,9 @@ private fun TranslateResponse(
if (ViewUnit.isTablet(LocalContext.current)) {
GptRow(viewModel)
}
DeepLButton(iconSize, iconPadding, translateDeepL, onTargetLanguageClick)
GoogleButton(iconSize, iconPadding, translateGoogle, onTargetLanguageClick)
if (showExtraIcons) {
DeepLButton(iconSize, iconPadding, translateDeepL, onTargetLanguageClick)
PapagoButton(iconSize, iconPadding, translatePapago, onTargetLanguageClick)
NaverButton(iconSize, iconPadding, translateNaver)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ fun TranslationConfigScreen(
expanded = actionExpanded,
onDismissRequest = { actionExpanded = false }
) {
TranslationMode.entries.forEach { type ->
val text = context.getString(type.labelResId)
DropdownMenuItem(onClick = {
translationModeChanged(type)
actionExpanded = false
}) {
Text(text = text)
TranslationMode.entries.toMutableList().apply { remove(TranslationMode.DEEPL_BY_PARAGRAPH) }
.forEach { type ->
val text = context.getString(type.labelResId)
DropdownMenuItem(onClick = {
translationModeChanged(type)
actionExpanded = false
}) {
Text(text = text)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ class ToolbarActionHandler(
ToolbarAction.BoldFont -> browserController.showFontBoldnessDialog()
ToolbarAction.Bookmark -> browserController.saveBookmark()
ToolbarAction.Font -> browserController.toggleReaderMode()
ToolbarAction.InputUrl -> {
// toggle papago translate
if (config.papagoApiSecret.isBlank()) {
config.papagoApiSecret = "123"
} else {
config.papagoApiSecret = ""
}
}
ToolbarAction.NewTab -> IntentUnit.launchNewBrowser(activity, config.favoriteUrl)
ToolbarAction.PageDown -> browserController.jumpToBottom()
ToolbarAction.PageInfo -> browserController.summarizeContent()
Expand Down
Loading

0 comments on commit a56c9b2

Please sign in to comment.