2024-04-02 15:36:52 +08:00
|
|
|
from translator.basetranslator import basetrans
|
|
|
|
import time, json
|
2025-01-04 17:59:00 +08:00
|
|
|
from language import Languages
|
2024-03-05 17:04:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TS(basetrans):
|
|
|
|
def inittranslator(self):
|
2024-04-02 15:36:52 +08:00
|
|
|
|
2024-03-05 17:04:16 +08:00
|
|
|
headers = {
|
2024-04-02 15:36:52 +08:00
|
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
|
|
"Accept-Language": "zh-CN,zh;q=0.9,ar;q=0.8,sq;q=0.7",
|
|
|
|
"Cache-Control": "max-age=0",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"If-Modified-Since": "Mon, 04 Mar 2024 09:40:16 GMT",
|
|
|
|
"If-None-Match": '"65e59700-e9b"',
|
|
|
|
"Sec-Fetch-Dest": "document",
|
|
|
|
"Sec-Fetch-Mode": "navigate",
|
|
|
|
"Sec-Fetch-Site": "same-origin",
|
|
|
|
"Sec-Fetch-User": "?1",
|
|
|
|
"Upgrade-Insecure-Requests": "1",
|
|
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
|
|
|
"sec-ch-ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
|
|
|
|
"sec-ch-ua-mobile": "?0",
|
|
|
|
"sec-ch-ua-platform": '"Windows"',
|
2024-03-05 17:04:16 +08:00
|
|
|
}
|
2024-06-07 19:03:52 +08:00
|
|
|
self.proxysession.get(
|
2024-04-02 15:36:52 +08:00
|
|
|
"https://fanyi.baidu.com/mtpe-individual/multimodal#/", headers=headers
|
|
|
|
)
|
|
|
|
|
2024-03-05 17:04:16 +08:00
|
|
|
def langmap(self):
|
2024-04-02 15:36:52 +08:00
|
|
|
return {
|
2025-01-04 17:59:00 +08:00
|
|
|
Languages.Spanish: "spa",
|
|
|
|
Languages.Korean: "kor",
|
|
|
|
Languages.French: "fra",
|
|
|
|
Languages.Japanese: "jp",
|
|
|
|
Languages.TradChinese: "cht",
|
|
|
|
Languages.Vietnamese: "vie",
|
|
|
|
Languages.Ukrainian: "ukr",
|
|
|
|
Languages.Arabic: "ara",
|
|
|
|
Languages.Swedish : "swe",
|
|
|
|
Languages.Latin: "lat",
|
2024-04-02 15:36:52 +08:00
|
|
|
}
|
|
|
|
|
2024-11-12 18:08:22 +08:00
|
|
|
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"',
|
|
|
|
"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:
|
2024-11-15 10:36:57 +08:00
|
|
|
raise Exception(response)
|
2024-11-12 18:08:22 +08:00
|
|
|
|
2024-04-02 15:36:52 +08:00
|
|
|
def translate(self, query):
|
2024-03-05 17:04:16 +08:00
|
|
|
|
|
|
|
headers = {
|
2024-04-02 15:36:52 +08:00
|
|
|
"sec-ch-ua": '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
|
|
|
|
"sec-ch-ua-mobile": "?0",
|
|
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"accept": "text/event-stream",
|
|
|
|
#'Acs-Token':
|
|
|
|
"Referer": "https://fanyi.baidu.com/mtpe-individual/multimodal",
|
|
|
|
"sec-ch-ua-platform": '"Windows"',
|
2024-03-05 17:04:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
json_data = {
|
2024-04-02 15:36:52 +08:00
|
|
|
"query": query,
|
2024-11-12 18:08:22 +08:00
|
|
|
"from": self.detectlang(query) if self.srclang == "auto" else self.srclang,
|
2024-04-02 15:36:52 +08:00
|
|
|
"to": self.tgtlang,
|
|
|
|
"reference": "",
|
|
|
|
"corpusIds": [],
|
|
|
|
"qcSettings": [
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
"7",
|
|
|
|
"8",
|
|
|
|
"9",
|
|
|
|
"10",
|
|
|
|
"11",
|
2024-03-05 17:04:16 +08:00
|
|
|
],
|
2024-04-02 15:36:52 +08:00
|
|
|
"domain": "common",
|
|
|
|
"milliTimestamp": int(time.time() * 1000),
|
2024-03-05 17:04:16 +08:00
|
|
|
}
|
|
|
|
|
2024-06-07 19:03:52 +08:00
|
|
|
response = self.proxysession.post(
|
2024-04-02 15:36:52 +08:00
|
|
|
"https://fanyi.baidu.com/ait/text/translate",
|
|
|
|
headers=headers,
|
|
|
|
json=json_data,
|
|
|
|
stream=True,
|
|
|
|
)
|
2024-03-05 17:04:16 +08:00
|
|
|
|
|
|
|
for text in response.iter_lines():
|
2024-04-02 15:36:52 +08:00
|
|
|
# print(text,text[:5]!=b'data:',text[:5],b'data:')
|
|
|
|
if len(text) == 0 or text[:5] != b"data:":
|
|
|
|
continue
|
|
|
|
js = json.loads(text[5:].decode("utf8"))
|
2024-06-20 14:33:23 +08:00
|
|
|
if js["data"] is None:
|
|
|
|
continue
|
2024-04-02 15:36:52 +08:00
|
|
|
event = js["data"]["event"]
|
|
|
|
if event == "Translating":
|
|
|
|
trans = "\n".join([_["dst"] for _ in js["data"]["list"]])
|
|
|
|
yield trans
|