mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2025-01-15 00:43:59 +08:00
auto
This commit is contained in:
parent
1257dfddf5
commit
0f791c8e57
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
set(VERSION_MAJOR 5)
|
set(VERSION_MAJOR 5)
|
||||||
set(VERSION_MINOR 58)
|
set(VERSION_MINOR 58)
|
||||||
set(VERSION_PATCH 1)
|
set(VERSION_PATCH 2)
|
||||||
set(VERSION_REVISION 0)
|
set(VERSION_REVISION 0)
|
||||||
add_definitions(-DVERSION_MAJOR=${VERSION_MAJOR})
|
add_definitions(-DVERSION_MAJOR=${VERSION_MAJOR})
|
||||||
add_definitions(-DVERSION_MINOR=${VERSION_MINOR})
|
add_definitions(-DVERSION_MINOR=${VERSION_MINOR})
|
||||||
|
@ -40,6 +40,29 @@ class TS(basetrans):
|
|||||||
"la": "lat",
|
"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):
|
def translate(self, query):
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
@ -55,7 +78,7 @@ class TS(basetrans):
|
|||||||
|
|
||||||
json_data = {
|
json_data = {
|
||||||
"query": query,
|
"query": query,
|
||||||
"from": self.parse_maybe_autolang(query),
|
"from": self.detectlang(query) if self.srclang == "auto" else self.srclang,
|
||||||
"to": self.tgtlang,
|
"to": self.tgtlang,
|
||||||
"reference": "",
|
"reference": "",
|
||||||
"corpusIds": [],
|
"corpusIds": [],
|
||||||
|
@ -14,7 +14,6 @@ from myutils.utils import (
|
|||||||
dynamicapiname,
|
dynamicapiname,
|
||||||
)
|
)
|
||||||
from myutils.commonbase import ArgsEmptyExc, commonbase
|
from myutils.commonbase import ArgsEmptyExc, commonbase
|
||||||
from myutils.languageguesser import guess
|
|
||||||
|
|
||||||
|
|
||||||
class Interrupted(Exception):
|
class Interrupted(Exception):
|
||||||
@ -84,12 +83,6 @@ class basetrans(commonbase):
|
|||||||
|
|
||||||
return alternatedict(translatorsetting[self.typename]["args"])
|
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"
|
_globalconfig_key = "fanyi"
|
||||||
_setting_dict = translatorsetting
|
_setting_dict = translatorsetting
|
||||||
|
@ -174,20 +174,21 @@ class Bing(Tse):
|
|||||||
|
|
||||||
class TS(basetrans):
|
class TS(basetrans):
|
||||||
def langmap(self):
|
def langmap(self):
|
||||||
return {"zh": "zh-Hans", "cht": "zh-Hant"}
|
return {"zh": "zh-Hans", "cht": "zh-Hant", "auto": "auto-detect"}
|
||||||
|
|
||||||
def inittranslator(self):
|
def inittranslator(self):
|
||||||
self.engine = Bing()
|
self.engine = Bing()
|
||||||
|
|
||||||
def translate(self, content):
|
def translate(self, content):
|
||||||
src = self.parse_maybe_autolang(content)
|
|
||||||
try:
|
try:
|
||||||
return self.engine.bing_api(
|
return self.engine.bing_api(
|
||||||
content,
|
content,
|
||||||
src,
|
self.srclang,
|
||||||
self.tgtlang,
|
self.tgtlang,
|
||||||
proxies=self.proxy,
|
proxies=self.proxy,
|
||||||
if_use_cn_host=True,
|
if_use_cn_host=True,
|
||||||
)
|
)
|
||||||
except:
|
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
|
||||||
|
)
|
||||||
|
@ -18,13 +18,43 @@ class TS(basetransdev):
|
|||||||
return self.tgtlang_1
|
return self.tgtlang_1
|
||||||
|
|
||||||
def translate(self, content):
|
def translate(self, content):
|
||||||
self.Page_navigate(
|
if self.srclang == "auto":
|
||||||
"https://www.deepl.com/en/translator#{}/{}/{}".format(
|
self.Runtime_evaluate(
|
||||||
self.parse_maybe_autolang(content), self.tgtlang, quote(content)
|
'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,
|
|
||||||
)
|
|
||||||
|
@ -66,7 +66,7 @@ class Reverso(Tse):
|
|||||||
self.host_url = "https://www.reverso.net/text-translation"
|
self.host_url = "https://www.reverso.net/text-translation"
|
||||||
self.api_url = "https://api.reverso.net/translate/v1/translation"
|
self.api_url = "https://api.reverso.net/translate/v1/translation"
|
||||||
self.language_url = None
|
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.host_headers = self.get_headers(self.host_url, if_api=False)
|
||||||
self.api_headers = self.get_headers(
|
self.api_headers = self.get_headers(
|
||||||
self.host_url, if_api=True, if_json_for_api=True
|
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"])
|
return data if is_detail_result else "".join(data["translation"])
|
||||||
|
|
||||||
|
|
||||||
|
from myutils.languageguesser import guess
|
||||||
from translator.basetranslator import basetrans
|
from translator.basetranslator import basetrans
|
||||||
|
|
||||||
|
|
||||||
@ -154,10 +155,14 @@ class TS(basetrans):
|
|||||||
self.engine._ = None
|
self.engine._ = None
|
||||||
|
|
||||||
def translate(self, content):
|
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(
|
return self.engine.reverso_api(
|
||||||
content,
|
content,
|
||||||
self.parse_maybe_autolang(content),
|
src,
|
||||||
self.tgtlang,
|
self.tgtlang,
|
||||||
proxies=self.proxy,
|
proxies=self.proxy,
|
||||||
)
|
)
|
||||||
|
@ -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,
|
|
||||||
)
|
|
@ -1585,12 +1585,12 @@
|
|||||||
"name": "Azure"
|
"name": "Azure"
|
||||||
},
|
},
|
||||||
"baidu_ai": {
|
"baidu_ai": {
|
||||||
"use": false,
|
"use": true,
|
||||||
"color": "#ff65db",
|
"color": "#ff65db",
|
||||||
"name": "百度_v2"
|
"name": "百度_v2"
|
||||||
},
|
},
|
||||||
"baidu": {
|
"baidu": {
|
||||||
"use": true,
|
"use": false,
|
||||||
"color": "#ff65db",
|
"color": "#ff65db",
|
||||||
"name": "百度_v1"
|
"name": "百度_v1"
|
||||||
},
|
},
|
||||||
@ -1841,11 +1841,6 @@
|
|||||||
"color": "blue",
|
"color": "blue",
|
||||||
"name": "reverso"
|
"name": "reverso"
|
||||||
},
|
},
|
||||||
"reverso_context": {
|
|
||||||
"use": false,
|
|
||||||
"color": "blue",
|
|
||||||
"name": "reverso_context"
|
|
||||||
},
|
|
||||||
"TranslateCom": {
|
"TranslateCom": {
|
||||||
"use": false,
|
"use": false,
|
||||||
"color": "blue",
|
"color": "blue",
|
||||||
|
@ -4,6 +4,21 @@
|
|||||||
"host": "translate.plausibility.cloud"
|
"host": "translate.plausibility.cloud"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"reverso": {
|
||||||
|
"args": {
|
||||||
|
"origin": 0
|
||||||
|
},
|
||||||
|
"argstype": {
|
||||||
|
"origin": {
|
||||||
|
"rank": 0,
|
||||||
|
"type": "combo",
|
||||||
|
"list": [
|
||||||
|
"translation.web",
|
||||||
|
"contextweb"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"deepl": {
|
"deepl": {
|
||||||
"args": {
|
"args": {
|
||||||
"usewhich": 0,
|
"usewhich": 0,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user