-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.py
42 lines (35 loc) · 935 Bytes
/
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
40
41
42
#!/usr/bin/python
# - * -coding: UTF - 8 - * -
import requests
import uuid
import time
import hashlib
import json
id = 'x235aa342437a4675'
key = 'xS8Yv9sToyjB5Hs93uOcMdAIO5ikTDvUf'
url = "https://openapi.youdao.com/api"
def translate(text):
if len(text) > 20:
input = text[0:10] + str(len(text)) + text[-10:]
else:
input = text
salt = uuid.uuid4().hex
curtime = str(int(time.time()))
sha = hashlib.sha256()
sha.update((id+input+salt+curtime+key).encode('utf-8'))
sign = sha.hexdigest()
querystring = {
"q": text,
"from": "auto",
"to": "zh-CHS",
"appKey": id,
"salt": salt,
"sign": sign,
"signType": "v3",
"curtime": curtime,
"input": input
}
response = requests.request(
"GET", url, params=querystring)
json_data = json.loads(response.text)
return json_data['translation'][0]