-
Notifications
You must be signed in to change notification settings - Fork 0
/
youdao.py
46 lines (37 loc) · 1.16 KB
/
youdao.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
# /usr/bin/env python
# coding=utf8
import httplib
import json
import md5
import urllib
import random
class translation:
@classmethod
def translate(self, q):
appKey = 'xxx'
secretKey = 'xxx'
httpClient = None
myurl = '/api'
q = q
fromLang = 'EN'
toLang = 'zh-CHS'
salt = random.randint(1, 65536)
sign = appKey + q + str(salt) + secretKey
m1 = md5.new()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl + '?appKey=' + appKey + '&q=' + urllib.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = httplib.HTTPConnection('openapi.youdao.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
response = response.read()
data = json.loads(response)
return data['web'][0]['value'][0]
except Exception, e:
return ' '
finally:
if httpClient:
httpClient.close()