Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
18 changes: 16 additions & 2 deletions Baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Baidu(object):

def __init__(self):

self.url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
#self.url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
self.url = 'https://fanyi.baidu.com/v2transapi?from={}&to={}'
self.header = {
'content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
'origin': 'https://fanyi.baidu.com',
Expand Down Expand Up @@ -106,8 +107,21 @@ def get_gtk(self):
#print('gtk '+gtk)
return gtk

def langdetect(self, text):
s = requests.session()
data = { 'query': text }
response = requests.post('https://fanyi.baidu.com/langdetect', headers=self.header, data=data)
res = response.json()
if res['error'] == 0:
return res['lan']
else:
return 'en'

def get_data(self, from_lan , to_lan, text):
data = {}
if from_lan == 'auto':
from_lan = self.langdetect(text)
print("detected from_lan={}".format(from_lan))
data['from'] = from_lan
data['to'] = to_lan
data['query'] = text
Expand All @@ -120,7 +134,7 @@ def get_data(self, from_lan , to_lan, text):
def translate(self, from_lan, to_lan, text):
self.data = self.get_data(from_lan, to_lan, text)
s = requests.session()
response = requests.post(self.url, headers=self.header, data=self.data )
response = requests.post(self.url.format(from_lan, to_lan), headers=self.header, data=self.data )
return response.json()['trans_result']['data'][0]['dst']


Expand Down
6 changes: 5 additions & 1 deletion Bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def __init__(self):

def translate(self, from_lan, to_lan,content,):
data = {}
data['from'] = '"' + from_lan + '"'
if from_lan != 'auto':
data['from'] = '"' + from_lan + '"'
data['to'] = '"' + to_lan + '"'
data['texts'] = '["'
data['texts'] += content
Expand All @@ -20,6 +21,9 @@ def translate(self, from_lan, to_lan,content,):
strUrl = self.url + data.decode() + "&appId=%223DAEE5B978BA031557E739EE1E2A68CB1FAD5909%22"
response = urllib.request.urlopen(strUrl)
str_data = response.read().decode('utf-8')
tmp, from_lang = str_data.split('"From":')
from_lang = from_lang[1:from_lang.find('"', 1)]
print("detected from_lang={}".format(from_lang))
tmp, str_data = str_data.split('"TranslatedText":')
translate_data = str_data[1:str_data.find('"', 1)]
return translate_data
Expand Down
8 changes: 4 additions & 4 deletions Tencent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ def get_filter(text):
return text

def get_qtv_qtk():
api_url = 'https://fanyi.qq.com/'
api_url = 'https://fanyi.qq.com/api/reauth12f'

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, '
'like Gecko) Chrome/73.0.3683.86 Safari/537.36', }

res = requests.get(api_url, headers=headers)
res = requests.post(api_url, headers=headers)
data = res.text
fy_guid = res.cookies.get('fy_guid')
reg = re.compile(r'var qtv = "(.*?)"')
reg = re.compile(r'"qtv":"(.*?)"')
qtv = reg.search(data).group(1)
reg = re.compile(r'var qtk = "(.*?)"')
reg = re.compile(r'"qtk":"(.*?)"')
qtk = reg.search(data).group(1)

return fy_guid, qtv, qtk
Expand Down
3 changes: 2 additions & 1 deletion Youdao.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def translate(self,from_lan,to_lan,text):
self.headers['Content-Length'] = str(233 + len(text))
ts = str(int(time.time() * 1000))
salf = ts + str(random.randint(0, 9))
n = 'fanyideskweb' + text + salf + "n%A-rKaT5fb[Gy?;N5@Tj"
n = 'fanyideskweb' + text + salf + "Tbh5E8=q6U3EXe+&L[4c@"
self.m = hashlib.md5()
self.m.update(n.encode('utf-8'))
sign = self.m.hexdigest()
Expand All @@ -53,6 +53,7 @@ def translate(self,from_lan,to_lan,text):
}
try:
result = self.s.post(self.url, headers=self.headers, data=data, timeout=5).json()
print(result)
if result != None:
ans = result['translateResult'][0][0]['tgt']
return ans
Expand Down