-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
39 lines (34 loc) · 1.2 KB
/
translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import hashlib
import random
import time
import requests
from slugify import slugify
def translate(content):
salt = str(round(time.time() * 1000)) + str(random.randint(0, 9))
data = "fanyideskweb" + content + salt + "Tbh5E8=q6U3EXe+&L[4c@"
sign = hashlib.md5()
sign.update(data.encode("utf-8"))
sign = sign.hexdigest()
url = 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'
headers = {
'Cookie': '[email protected];',
'Host': 'fanyi.youdao.com',
'Origin': 'http://fanyi.youdao.com',
'Referer': 'http://fanyi.youdao.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36',
}
data = {
'i': str(content),
'from': 'AUTO',
'to': 'en',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': str(salt),
'sign': str(sign),
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}
res = requests.post(url=url, headers=headers, data=data).json()
res = res['translateResult'][0][0]['tgt']
return slugify(res)