-
Notifications
You must be signed in to change notification settings - Fork 0
/
youdao.py
26 lines (24 loc) · 969 Bytes
/
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
import random
import json
import sys
import requests
import hashlib
class Youdao(object):
def __init__(self):
# app key
self.appKey = "0b2326799bc74b6e"
#pass ward
self.pwd = "h8YekUjoOWwK15BhlpsXJ0bxkgXv4XQE"
def translate(self, fromLanguage, toLanguage, translateText):
try:
salt = random.randrange(10)
signString = self.appKey + translateText + str(salt) + self.pwd
sign = hashlib.md5(signString.encode())
url = "http://openapi.youdao.com/api?q=" + translateText + "&from=" + fromLanguage + "&to=" + toLanguage + "&appKey=" + self.appKey + "&salt=" + str(salt) + "&sign=" + sign.hexdigest()
receiveMsg = requests.post(url)
jsonObj = json.loads(receiveMsg.text)
result = jsonObj["translation"]
leng = len(result)
return result[0]
except Exception:
raise ValueError("youdao have a error")