-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrosoft.py
154 lines (137 loc) · 5.19 KB
/
microsoft.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: utf-8 -*-
import os
import time
import requests
import config
from utili import set_proxy
shound_del=False
def update_proxy(type='set'):
global shound_del
if type=='del' and shound_del:
del os.environ['http_proxy']
del os.environ['https_proxy']
del os.environ['all_proxy']
shound_del=False
elif type=='set':
raw_proxy=os.environ.get('http_proxy')
if not raw_proxy:
proxy=set_proxy()
if proxy:
shound_del=True
os.environ['http_proxy'] = proxy
os.environ['https_proxy'] = proxy
os.environ['all_proxy'] = proxy
def cleartext(text):
return text.replace('"','').replace("'",'').replace(''','').replace('"',"").strip()
def trans(text_list, target_language="en", *, inst=None,stop=0,source_code=""):
"""
text_list:
target_language:
"""
target_text = []
index = -1
iter_num = 0
err = ""
proxies=None
pro=update_proxy(type='set')
if pro:
proxies={"https":pro,"http":pro}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
}
def get_content(data,auth):
url = f"https://api-edge.cognitive.microsofttranslator.com/translate?from=&to={data['target_language']}&api-version=3.0&includeSentenceLength=true"
headers['Authorization'] = f"Bearer {auth.text}"
config.logger.info(f'[Mircosoft]请求数据:{url=},{auth.text=}')
response = requests.post(url, json=[{"Text": data['text']}], headers=headers, timeout=300)
config.logger.info(f'[Mircosoft]返回:{response.text=}')
if response.status_code != 200:
raise Exception(f'{response.status_code=}')
try:
re_result = response.json()
except Exception:
raise Exception('notjson' + response.text)
if len(re_result) == 0 or len(re_result[0]['translations']) == 0:
raise Exception(f'{re_result}')
return re_result[0]['translations'][0]['text']
print("to translate")
while 1:
iter_num += 1
if iter_num > 3:
print("max time reached")
break
if iter_num > 1:
time.sleep(5)
#print("to zli")
if isinstance(text_list, str):
source_text = text_list.strip().split("\n")
else:
source_text = [f"{t['text']}" for t in text_list]
#print("to split")
split_size = int(config.settings['trans_thread'])
split_source_text = [source_text[i:i + split_size] for i in range(0, len(source_text), split_size)]
try:
auth=requests.get('https://edge.microsoft.com/translate/auth',headers=headers,proxies=proxies)
except:
err='连接微软翻译失败' if config.defaulelang=='zh' else 'Failed to connect to Microsoft Translate'
continue
#print(" to proceds:", split_source_text)
for i,it in enumerate(split_source_text):
print("i:", i, "it:", it)
if i <= index:
print("i", i, "ind:", index)
continue
if stop>0:
print("to sleep stop:", stop)
time.sleep(stop)
print("to get")
try:
source_length=len(it)
text = "\n".join(it)
data={
"text":text,
"target_language":target_language
}
res_trans=get_content(data,auth)
print("trans return result:", res_trans)
result= cleartext(res_trans).split("\n")
result_length = len(result)
# not match split again
if result_length < source_length:
print(f'翻译前后数量不一致,需要重新切割')
result=[]
for line_res in it:
data['text']=line_res
result.append(get_content(data,auth))
if inst and inst.precent < 75:
inst.precent += round((i + 1) * 5 / len(split_source_text), 2)
result_length=len(result)
while result_length<source_length:
result.append("")
result_length+=1
result=result[:source_length]
target_text.extend(result)
except Exception as e:
err=f'{str(e)}'
break
else:
err=''
index= i
iter_num=0
else:
break
update_proxy(type='del')
if err:
config.logger.error(f'[Mircosoft]翻译请求失败:{err=}')
raise Exception(f'Mircosoft:{err}')
if isinstance(text_list, str):
return "\n".join(target_text)
max_i = len(target_text)
if max_i < len(text_list)/2:
raise Exception(f'Mircosoft:{"translate errr"}')
for i, it in enumerate(text_list):
if i < max_i:
text_list[i]['text'] = target_text[i]
else:
text_list[i]['text'] = ""
return text_list