forked from pcrbot/ai_setu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.py
More file actions
108 lines (87 loc) · 3.11 KB
/
translate.py
File metadata and controls
108 lines (87 loc) · 3.11 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
from hoshino import aiorequests
import uuid
import hashlib
import time
import yaml
from os.path import dirname, join
curpath = dirname(__file__) #当前路径
config_path = join(curpath,"config.yaml")
with open(config_path,encoding="utf-8") as f: #初始化法典
config = yaml.safe_load(f)#读取配置文件
way = config['way2trans']
if way:
url = config['baidu_url']
app_id = config['baidu_app_id']
app_key = config['baidu_app_key']
else:
url = config['youdao_url']
app_id = config['youdao_app_id']
app_key = config['youdao_app_key']
async def youdaoTranslate(translate_text):
'''
:param translate_text: 待翻译的句子
:param flag: 1:原句子翻译成英文;0:原句子翻译成中文
:return: 返回翻译结果
'''
# 翻译文本生成sign前进行的处理
input_text = ""
# 当文本长度小于等于20时,取文本
if (len(translate_text) <= 20):
input_text = translate_text
# 当文本长度大于20时,进行特殊处理
elif (len(translate_text) > 20):
input_text = translate_text[:10] + str(len(translate_text)) + translate_text[-10:]
time_curtime = int(time.time()) # 秒级时间戳获取
uu_id = uuid.uuid4() # 随机生成的uuid数,为了每次都生成一个不重复的数。
sign = hashlib.sha256(
(app_id + input_text + str(uu_id) + str(time_curtime) + app_key).encode('utf-8')).hexdigest() # sign生成
data = {
'q': translate_text, # 翻译文本
'appKey': app_id, # 应用id
'salt': uu_id, # 随机生产的uuid码
'sign': sign, # 签名
'signType': "v3", # 签名类型,固定值
'curtime': time_curtime, # 秒级时间戳
}
data['from'] = "zh-CHS" # 译文语种
data['to'] = "en" # 译文语种
r = await aiorequests.get(url, params=data) # 获取返回的json()内容
r = await r.json()
# print("翻译后的结果:" + r["translation"][0]) # 获取翻译内容
return r["translation"][0]
async def baiduTranslate(translate_text:str) -> str:
# pre
from_lang = 'zh' # original language
to_lang = 'en' # target language
# get text to translate
input_text = '这里是需要翻译的内容。'
input_text = translate_text
# Generate salt and sign
uu_id = uuid.uuid4()
sign = hashlib.md5((app_id + input_text + str(uu_id) + app_key).encode('utf-8')).hexdigest()
# Build request
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
data = {
'appid': app_id,
'q': input_text,
'from': from_lang,
'to': to_lang,
'salt': uu_id,
'sign': sign
}
# Send request
r = await (await aiorequests.post(url, params=data, headers=headers)).json()
# Show response
return r["trans_result"][0]["dst"]
async def tag_trans(tags):
for c in tags:
if ('\u4e00' <= c <= '\u9fa5'):
isChinese = True
break
else:
isChinese = False
if(isChinese):
tags= (await baiduTranslate(tags)) if way else (await youdaoTranslate(tags))
return tags