-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslateBaidu.py
More file actions
38 lines (28 loc) · 1009 Bytes
/
translateBaidu.py
File metadata and controls
38 lines (28 loc) · 1009 Bytes
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
import requests
import random
import json
from hashlib import md5
from config import APPID, APPKEY
# Set your own appid/appkey.
appid = APPID
appkey = APPKEY
def translate(txt, to_lang='jp'):
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
query = txt
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
dstText = result['trans_result'][0]['dst']
return dstText