mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-29 16:44:13 +08:00
baidu
This commit is contained in:
parent
a542f6d5dc
commit
1004bcfa79
@ -59,8 +59,16 @@ def settab2d(self):
|
|||||||
lixians,pre,mianfei,develop,shoufei=splittranslatortypes()
|
lixians,pre,mianfei,develop,shoufei=splittranslatortypes()
|
||||||
while True:
|
while True:
|
||||||
port=globalconfig['debugport']
|
port=globalconfig['debugport']
|
||||||
_path=globalconfig['chromepath']
|
_path=None
|
||||||
needstart=any([globalconfig['fanyi'][dev]['use'] for dev in develop]) and os.path.exists(_path)
|
for syspath in [
|
||||||
|
globalconfig['chromepath'],
|
||||||
|
r'C:\Program Files\Google\Chrome\Application\chrome.exe',
|
||||||
|
r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe',
|
||||||
|
]:
|
||||||
|
if os.path.exists(syspath) and os.path.isfile(syspath):
|
||||||
|
_path=syspath
|
||||||
|
break
|
||||||
|
needstart=any([globalconfig['fanyi'][dev]['use'] for dev in develop]) and _path
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if needstart :
|
if needstart :
|
||||||
|
73
LunaTranslator/LunaTranslator/translator/baidu_ai.py
Normal file
73
LunaTranslator/LunaTranslator/translator/baidu_ai.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
from translator.basetranslator import basetrans
|
||||||
|
import time,json
|
||||||
|
|
||||||
|
class TS(basetrans):
|
||||||
|
def inittranslator(self):
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'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"',
|
||||||
|
}
|
||||||
|
self.session.get('https://fanyi.baidu.com/mtpe-individual/multimodal#/',headers=headers)
|
||||||
|
def langmap(self):
|
||||||
|
return {"es":"spa","ko":"kor","fr":"fra","ja":"jp","cht":"cht","vi":"vie","uk":"ukr","ar":"ara"}
|
||||||
|
def translate(self,query):
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'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"',
|
||||||
|
}
|
||||||
|
|
||||||
|
json_data = {
|
||||||
|
'query': query,
|
||||||
|
'from': self.srclang,
|
||||||
|
'to': self.tgtlang,
|
||||||
|
'reference': '',
|
||||||
|
'corpusIds': [],
|
||||||
|
'qcSettings': [
|
||||||
|
'1',
|
||||||
|
'2',
|
||||||
|
'3',
|
||||||
|
'4',
|
||||||
|
'5',
|
||||||
|
'6',
|
||||||
|
'7',
|
||||||
|
'8',
|
||||||
|
'9',
|
||||||
|
'10',
|
||||||
|
'11',
|
||||||
|
],
|
||||||
|
'domain': 'common',
|
||||||
|
'milliTimestamp': int(time.time()*1000),
|
||||||
|
}
|
||||||
|
|
||||||
|
response = self.session.post('https://fanyi.baidu.com/ait/text/translate', headers=headers, json=json_data,stream=True)
|
||||||
|
|
||||||
|
for text in response.iter_lines():
|
||||||
|
#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'))
|
||||||
|
event=js['data']['event']
|
||||||
|
if event=='Translating':
|
||||||
|
trans='\n'.join([_['dst'] for _ in js['data']['list']])
|
||||||
|
return trans
|
@ -3,13 +3,13 @@ from translator.basetranslator_dev import basetransdev
|
|||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
import time
|
import time
|
||||||
class TS(basetransdev):
|
class TS(basetransdev):
|
||||||
target_url= 'https://fanyi.baidu.com'
|
target_url= 'https://fanyi.baidu.com/#'
|
||||||
def langmap(self):
|
def langmap(self):
|
||||||
return {"es":"spa","ko":"kor","fr":"fra","ja":"jp","cht":"cht","vi":"vie","uk":"ukr"}
|
return {"es":"spa","ko":"kor","fr":"fra","ja":"jp","cht":"cht","vi":"vie","uk":"ukr"}
|
||||||
def translate(self,content):
|
def translate(self,content):
|
||||||
self.Runtime_evaluate("document.getElementsByClassName('textarea-clear-btn')[0].click()")
|
self.Runtime_evaluate("document.getElementsByClassName('textarea-clear-btn')[0].click()")
|
||||||
|
|
||||||
self.Page_navigate('https://fanyi.baidu.com/#{}/{}/{}'.format(self.srclang,self.tgtlang,quote(content)))
|
self.Page_navigate('https://fanyi.baidu.com/#{}/{}/{}'.format(self.srclang,self.tgtlang,quote(content)))
|
||||||
res=self.wait_for_result("document.querySelector('div.output-bd')===null?'':document.querySelector('div.output-bd').innerText")
|
res=self.wait_for_result("document.querySelector('div.output-bd')===null?'':document.querySelector('div.output-bd').innerText").replace('\n\n','\n')
|
||||||
return (res)
|
return (res)
|
||||||
|
|
@ -25,8 +25,6 @@ class basetransdev(basetrans):
|
|||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
#########################################
|
#########################################
|
||||||
def _private_init(self):
|
def _private_init(self):
|
||||||
if os.path.exists(globalconfig['chromepath'])==False:
|
|
||||||
raise Exception("can't find chromium")
|
|
||||||
self._id=1
|
self._id=1
|
||||||
self._createtarget()
|
self._createtarget()
|
||||||
super()._private_init()
|
super()._private_init()
|
||||||
|
@ -769,6 +769,11 @@
|
|||||||
"color": "#ff65db",
|
"color": "#ff65db",
|
||||||
"name": "百度"
|
"name": "百度"
|
||||||
},
|
},
|
||||||
|
"baidu_ai": {
|
||||||
|
"use": false,
|
||||||
|
"color": "#ff65db",
|
||||||
|
"name": "百度_ai"
|
||||||
|
},
|
||||||
"bing": {
|
"bing": {
|
||||||
"use": false,
|
"use": false,
|
||||||
"color": "#000ff0",
|
"color": "#000ff0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user