-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaiduFanyi.py
More file actions
124 lines (96 loc) · 3.39 KB
/
Copy pathBaiduFanyi.py
File metadata and controls
124 lines (96 loc) · 3.39 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
# coding=utf-8
import sys
import zipfile
import shutil
import os
import os.path
import time
import datetime
import json
import requests
#include common.py
#
o_path = os.getcwd() # 返回当前工作目录
print(o_path)
# sys.path.append(o_path) # 添加自己指定的搜索路径
# 当前工作目录 Common/PythonCreator/ProjectConfig/Script
sys.path.append('../../')
# sys.path.append('./')
dir = os.path.abspath(__file__)
print(dir)
dir = os.path.dirname(dir)
print(dir)
# sys.path.append("..") #把上级目录加入到变量中
sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Common.Common import Common
# from Config import config
from Common import Source
# from Config import adconfig
from Common.File.FileUtil import FileUtil
from Common.Platform import Platform
# https://api.fanyi.baidu.com/product/113
# http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4
import http.client
import hashlib
import urllib
import random
import json
class BaiduFanyi():
driver: None
filepathSmail=""
#构造函数
def __init__(self):
name =""
def GetUrl(self, url):
r = requests.get(url)
return r.content.decode('utf-8',"ignore")
def RunFanyiEnToCN(self,text):
fromLang = 'en' #原文语种
toLang = 'zh' #译文语种
return self.Fanyi(text,fromLang,toLang)
def RunFanyiCnToEn(self,text):
fromLang = 'zh' #原文语种
toLang = 'en' #译文语种
return self.Fanyi(text,fromLang,toLang)
def Fanyi(self,text,fromLang,toLang):
# text = "apple"
# url = "http://api.fanyi.baidu.com/api/trans/vip/translate?q="+text+"&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4"
# print(url)
# str = self.GetUrl(url)
# print(str)
# rootJson = json.loads(str)
# return rootJson["trans_result"][0]["dst"]
# # {"from":"en","to":"zh","trans_result":[{"src":"apple","dst":"\u82f9\u679c"}]}
appid = '20210603000852273' # 填写你的appid
secretKey = 'ciTOLU1ECG4VXxKQtEsJ' # 填写你的密钥
httpClient = None
myurl = '/api/trans/vip/translate'
salt = random.randint(32768, 65536)
# q= 'apple'
q = text
sign = appid + q + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
result_all = response.read().decode("utf-8")
result = json.loads(result_all)
print('翻译::')
# print (result)
list = result["trans_result"]
ret = ""
for item in list:
ret = ret +item["dst"]+"\n"
return ret
except Exception as e:
print (e)
finally:
if httpClient:
httpClient.close()
return ""
mainBaiduFanyi = BaiduFanyi()