-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaiduTrans.py
47 lines (42 loc) · 1.3 KB
/
BaiduTrans.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
43
44
45
46
47
# coding=utf8
import httplib
import md5
import urllib
import random
import json
def transByHttpRequest(fromLanguage, toLanguage, queto):
appid = '20170512000047153'
secretKey = 'BlWllKkDwU5Wigp07MlR'
httpClient = None
myurl = '/api/trans/vip/translate'
salt = random.randint(32768, 65536)
sign = appid + queto + str(salt) + secretKey
m1 = md5.new()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + urllib.quote(
queto) + '&from=' + getLanguage(fromLanguage) + '&to=' + getLanguage(toLanguage) + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = httplib.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
response = httpClient.getresponse()
string = response.read().decode('utf-8')
json_obj = json.loads(string)
resultArray = json_obj['trans_result']
mylists = []
for item in resultArray:
dst = item['dst']
mylists.append(dst)
return mylists
except Exception, e:
print e
finally:
if httpClient:
httpClient.close()
def getLanguage(index):
if index==0:
return 'zh'
elif index==1:
return 'en'
return 'zh'