From 0f791c8e57b3b5f62b02b84d60377c53a1bb215b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Tue, 12 Nov 2024 18:08:22 +0800 Subject: [PATCH] auto --- cpp/version.cmake | 2 +- py/LunaTranslator/translator/baidu_ai.py | 25 ++- .../translator/basetranslator.py | 7 - py/LunaTranslator/translator/bing.py | 9 +- py/LunaTranslator/translator/deepl_dev.py | 48 +++++- py/LunaTranslator/translator/reverso.py | 11 +- .../translator/reverso_context.py | 161 ------------------ py/files/defaultconfig/config.json | 9 +- py/files/defaultconfig/translatorsetting.json | 15 ++ 9 files changed, 94 insertions(+), 193 deletions(-) delete mode 100644 py/LunaTranslator/translator/reverso_context.py diff --git a/cpp/version.cmake b/cpp/version.cmake index 60f6eed7..baa10b41 100644 --- a/cpp/version.cmake +++ b/cpp/version.cmake @@ -1,7 +1,7 @@ set(VERSION_MAJOR 5) set(VERSION_MINOR 58) -set(VERSION_PATCH 1) +set(VERSION_PATCH 2) set(VERSION_REVISION 0) add_definitions(-DVERSION_MAJOR=${VERSION_MAJOR}) add_definitions(-DVERSION_MINOR=${VERSION_MINOR}) diff --git a/py/LunaTranslator/translator/baidu_ai.py b/py/LunaTranslator/translator/baidu_ai.py index e1e88f6a..92285970 100644 --- a/py/LunaTranslator/translator/baidu_ai.py +++ b/py/LunaTranslator/translator/baidu_ai.py @@ -40,6 +40,29 @@ class TS(basetrans): "la": "lat", } + def detectlang(self, query): + + headers = { + "sec-ch-ua-platform": '"Windows"', + "Referer": "https://fanyi.baidu.com/mtpe-individual/multimodal", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", + "sec-ch-ua": '"Google Chrome";v="129", "Not=A?Brand";v="8", "Chromium";v="129"', + "Content-Type": "application/x-www-form-urlencoded", + "sec-ch-ua-mobile": "?0", + } + + data = { + "query": query, + } + + response = self.proxysession.post( + "https://fanyi.baidu.com/langdetect", headers=headers, data=data + ) + try: + return response.json()["lan"] + except: + raise Exception(response.maybejson) + def translate(self, query): headers = { @@ -55,7 +78,7 @@ class TS(basetrans): json_data = { "query": query, - "from": self.parse_maybe_autolang(query), + "from": self.detectlang(query) if self.srclang == "auto" else self.srclang, "to": self.tgtlang, "reference": "", "corpusIds": [], diff --git a/py/LunaTranslator/translator/basetranslator.py b/py/LunaTranslator/translator/basetranslator.py index 77d87680..fd2e1046 100644 --- a/py/LunaTranslator/translator/basetranslator.py +++ b/py/LunaTranslator/translator/basetranslator.py @@ -14,7 +14,6 @@ from myutils.utils import ( dynamicapiname, ) from myutils.commonbase import ArgsEmptyExc, commonbase -from myutils.languageguesser import guess class Interrupted(Exception): @@ -84,12 +83,6 @@ class basetrans(commonbase): return alternatedict(translatorsetting[self.typename]["args"]) - def parse_maybe_autolang(self, content): - if self.srclang != "auto": - return self.srclang - gs = guess(content) - return self.langmap_.get(gs, gs) - ############################################################ _globalconfig_key = "fanyi" _setting_dict = translatorsetting diff --git a/py/LunaTranslator/translator/bing.py b/py/LunaTranslator/translator/bing.py index c5a5392c..6089769e 100644 --- a/py/LunaTranslator/translator/bing.py +++ b/py/LunaTranslator/translator/bing.py @@ -174,20 +174,21 @@ class Bing(Tse): class TS(basetrans): def langmap(self): - return {"zh": "zh-Hans", "cht": "zh-Hant"} + return {"zh": "zh-Hans", "cht": "zh-Hant", "auto": "auto-detect"} def inittranslator(self): self.engine = Bing() def translate(self, content): - src = self.parse_maybe_autolang(content) try: return self.engine.bing_api( content, - src, + self.srclang, self.tgtlang, proxies=self.proxy, if_use_cn_host=True, ) except: - return self.engine.bing_api(content, src, self.tgtlang, proxies=self.proxy) + return self.engine.bing_api( + content, self.srclang, self.tgtlang, proxies=self.proxy + ) diff --git a/py/LunaTranslator/translator/deepl_dev.py b/py/LunaTranslator/translator/deepl_dev.py index 9f23634e..c900eb69 100644 --- a/py/LunaTranslator/translator/deepl_dev.py +++ b/py/LunaTranslator/translator/deepl_dev.py @@ -18,13 +18,43 @@ class TS(basetransdev): return self.tgtlang_1 def translate(self, content): - self.Page_navigate( - "https://www.deepl.com/en/translator#{}/{}/{}".format( - self.parse_maybe_autolang(content), self.tgtlang, quote(content) + if self.srclang == "auto": + self.Runtime_evaluate( + 'document.querySelector("#translator-source-clear-button").click()' + ) + self.Runtime_evaluate( + "document.getElementsByTagName('d-textarea')[0].focus()" + ) + self.send_keys(content) + result = self.wait_for_result( + 'document.getElementsByTagName("d-textarea")[1].textContent', + ("complete", ""), + multi=True, + ) + href: str = self.wait_for_result("window.location.href") + src, tgt = href.split("#")[1].split("/")[:2] + if tgt != self.tgtlang: + self.Page_navigate( + "https://www.deepl.com/en/translator#{}/{}/{}".format( + src, self.tgtlang, quote(content) + ) + ) + return self.wait_for_result( + 'document.getElementsByTagName("d-textarea")[1].textContent', + ("complete", ""), + multi=True, + ) + else: + return result + + else: + self.Page_navigate( + "https://www.deepl.com/en/translator#{}/{}/{}".format( + self.srclang, self.tgtlang, quote(content) + ) + ) + return self.wait_for_result( + 'document.getElementsByTagName("d-textarea")[1].textContent', + ("complete", ""), + multi=True, ) - ) - return self.wait_for_result( - 'document.getElementsByTagName("d-textarea")[1].textContent', - ("complete", ""), - multi=True, - ) diff --git a/py/LunaTranslator/translator/reverso.py b/py/LunaTranslator/translator/reverso.py index 4b1294d6..c33e7eff 100644 --- a/py/LunaTranslator/translator/reverso.py +++ b/py/LunaTranslator/translator/reverso.py @@ -66,7 +66,7 @@ class Reverso(Tse): self.host_url = "https://www.reverso.net/text-translation" self.api_url = "https://api.reverso.net/translate/v1/translation" self.language_url = None - self.language_pattern = "https://cdn.reverso.net/trans/v(\d).(\d).(\d)/main.js" + self.language_pattern = r"https://cdn.reverso.net/trans/v(\d).(\d).(\d)/main.js" self.host_headers = self.get_headers(self.host_url, if_api=False) self.api_headers = self.get_headers( self.host_url, if_api=True, if_json_for_api=True @@ -133,6 +133,7 @@ class Reverso(Tse): return data if is_detail_result else "".join(data["translation"]) +from myutils.languageguesser import guess from translator.basetranslator import basetrans @@ -154,10 +155,14 @@ class TS(basetrans): self.engine._ = None def translate(self, content): - + if self.srclang != "auto": + src = self.srclang + else: + gs = guess(content) + src = self.langmap_.get(gs, gs) return self.engine.reverso_api( content, - self.parse_maybe_autolang(content), + src, self.tgtlang, proxies=self.proxy, ) diff --git a/py/LunaTranslator/translator/reverso_context.py b/py/LunaTranslator/translator/reverso_context.py deleted file mode 100644 index a9be8677..00000000 --- a/py/LunaTranslator/translator/reverso_context.py +++ /dev/null @@ -1,161 +0,0 @@ -import requests -import urllib -import random -import time - - -class Tse: - def __init__(self): - self.author = "Ulion.Tse" - self.begin_time = time.time() - self.default_session_seconds = 1.5e3 - self.transform_en_translator_pool = ( - "Itranslate", - "Lingvanex", - ) - self.auto_pool = ( - "auto", - "auto-detect", - ) - self.zh_pool = ( - "zh", - "zh-CN", - "zh-CHS", - "zh-Hans", - "zh-Hans_CN", - "cn", - "chi", - ) - - @staticmethod - def get_headers( - host_url: str, - if_api: bool = False, - if_referer_for_host: bool = True, - if_ajax_for_api: bool = True, - if_json_for_api: bool = False, - if_multipart_for_api: bool = False, - ) -> dict: - - user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36" - url_path = urllib.parse.urlparse(host_url).path - host_headers = { - "Referer" if if_referer_for_host else "Host": host_url, - "User-Agent": user_agent, - } - api_headers = { - "Origin": host_url.split(url_path)[0] if url_path else host_url, - "Referer": host_url, - "X-Requested-With": "XMLHttpRequest", - "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", - "User-Agent": user_agent, - } - if if_api and not if_ajax_for_api: - api_headers.pop("X-Requested-With") - api_headers.update({"Content-Type": "text/plain"}) - if if_api and if_json_for_api: - api_headers.update({"Content-Type": "application/json"}) - if if_api and if_multipart_for_api: - api_headers.pop("Content-Type") - return host_headers if not if_api else api_headers - - -class Reverso(Tse): - def __init__(self): - super().__init__() - self.host_url = "https://www.reverso.net/text-translation" - self.api_url = "https://api.reverso.net/translate/v1/translation" - self.language_url = None - self.language_pattern = "https://cdn.reverso.net/trans/v(\d).(\d).(\d)/main.js" - self.host_headers = self.get_headers(self.host_url, if_api=False) - self.api_headers = self.get_headers( - self.host_url, if_api=True, if_json_for_api=True - ) - self.session = None - self.language_map = None - self.decrypt_language_map = None - self.query_count = 0 - self.output_zh = "zh" # 'chi', because there are self.language_tran - self.input_limit = 2000 - - def reverso_api( - self, - query_text: str, - from_language: str = "auto", - to_language: str = "en", - **kwargs - ): - proxies = kwargs.get("proxies", None) - is_detail_result = kwargs.get("is_detail_result", False) - sleep_seconds = kwargs.get("sleep_seconds", random.random()) - update_session_after_seconds = kwargs.get( - "update_session_after_seconds", self.default_session_seconds - ) - - not_update_cond_time = ( - 1 if time.time() - self.begin_time < update_session_after_seconds else 0 - ) - if not ( - self.session - and not_update_cond_time - and self.language_map - and self.decrypt_language_map - ): - self.session = requests.Session() - host_html = self.session.get( - self.host_url, - headers=self.host_headers, - proxies=proxies, - ).text - - form_data = { - "format": "text", - "from": from_language, - "to": to_language, - "input": query_text, - "options": { - "contextResults": "true", - "languageDetection": "true", - "sentenceSplitter": "true", - "origin": "contextweb", - }, - } - r = self.session.post( - self.api_url, - json=form_data, - headers=self.api_headers, - proxies=proxies, - ) - r.raise_for_status() - data = r.json() - time.sleep(sleep_seconds) - self.query_count += 1 - return data if is_detail_result else "".join(data["translation"]) - - -from translator.basetranslator import basetrans - - -class TS(basetrans): - def langmap(self): - return { - "zh": "chi", - "en": "eng", - "es": "spa", - "fr": "fra", - "ko": "kor", - "ru": "rus", - "ja": "jpn", - } - - def inittranslator(self): - self.engine = Reverso() - self.engine._ = None - - def translate(self, content): - return self.engine.reverso_api( - content, - self.parse_maybe_autolang(content), - self.tgtlang, - proxies=self.proxy, - ) diff --git a/py/files/defaultconfig/config.json b/py/files/defaultconfig/config.json index 55f35841..7a84c669 100644 --- a/py/files/defaultconfig/config.json +++ b/py/files/defaultconfig/config.json @@ -1585,12 +1585,12 @@ "name": "Azure" }, "baidu_ai": { - "use": false, + "use": true, "color": "#ff65db", "name": "百度_v2" }, "baidu": { - "use": true, + "use": false, "color": "#ff65db", "name": "百度_v1" }, @@ -1841,11 +1841,6 @@ "color": "blue", "name": "reverso" }, - "reverso_context": { - "use": false, - "color": "blue", - "name": "reverso_context" - }, "TranslateCom": { "use": false, "color": "blue", diff --git a/py/files/defaultconfig/translatorsetting.json b/py/files/defaultconfig/translatorsetting.json index a79edbd4..98c48d99 100644 --- a/py/files/defaultconfig/translatorsetting.json +++ b/py/files/defaultconfig/translatorsetting.json @@ -4,6 +4,21 @@ "host": "translate.plausibility.cloud" } }, + "reverso": { + "args": { + "origin": 0 + }, + "argstype": { + "origin": { + "rank": 0, + "type": "combo", + "list": [ + "translation.web", + "contextweb" + ] + } + } + }, "deepl": { "args": { "usewhich": 0,