forked from Animenosekai/translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing.py
218 lines (167 loc) · 7.56 KB
/
bing.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
This implementation was made specifically for translatepy from 'Zhymabek Roman', based on 'Anime no Sekai' version.
"""
import re
import json
import requests
import pyuseragents
from translatepy.translators.base import BaseTranslator
from translatepy.exceptions import UnsupportedMethod
from translatepy.utils.annotations import Tuple
from icecream import ic
HEADERS = {
# "Host": "www.bing.com",
"User-Agent": pyuseragents.random(),
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate",
# "Referer": "https://www.bing.com/",
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "keep-alive"
}
# TODO: read documentation: https://docs.microsoft.com/ru-ru/azure/cognitive-services/translator/language-support
class Example():
class SourceExample():
"""The source for an example"""
def __init__(self, data) -> None:
self._data = data
self.prefix = self._data.get("sourcePrefix", "")
self.term = self._data.get("sourceTerm", "")
self.suffix = self._data.get("sourceSuffix", "")
self.example = self.prefix + self.term + self.suffix
def __repr__(self) -> str:
return str(self.example)
class DestinationExample():
"""The target language example"""
def __init__(self, data) -> None:
self._data = data
self.prefix = self._data.get("targetPrefix", "")
self.term = self._data.get("targetTerm", "")
self.suffix = self._data.get("targetSuffix", "")
self.example = self.prefix + self.term + self.suffix
def __repr__(self) -> str:
return str(self.example)
def __init__(self, data) -> None:
self._data = data
self.source = self.SourceExample(self._data)
self.destination = self.DestinationExample(self._data)
def __repr__(self) -> str:
return str(self.source)
class BingSessionManager():
def __init__(self):
self._parse_authorization_data()
def _parse_authorization_data(self):
_page = requests.get("https://www.bing.com/translator").text
_parsed_IG = re.findall('IG:"(.*?)"', _page)
_parsed_IID = re.findall('data-iid="(.*?)"', _page)
_parsed_helper_info = re.findall("params_RichTranslateHelper = (.*?);", _page)
_normalized_key = json.loads(_parsed_helper_info[0])[0]
_normalized_token = json.loads(_parsed_helper_info[0])[1]
self.ig = _parsed_IG[0]
self.iid = _parsed_IID[0]
self.key = _normalized_key
self.token = _normalized_token
def send(self, url, data):
while True:
_params = {'IG': self.ig, 'IID': "self.iid.{}".format(1)}
_data = {'token': self.token, 'key': self.key}
_data.update(data)
request = requests.post(url, params=_params, data=_data, headers=HEADERS)
response = request.json()
if isinstance(response, dict):
if response.get("statusCode", 200) == 400:
self._parse_authorization_data()
continue
return response
class BingTranslate(BaseTranslator):
"""
A Python implementation of Microsoft Bing Translation's APIs
"""
def __init__(self):
self.session_manager = BingSessionManager()
def _translate(self, text: str, destination_language: str, source_language: str) -> str:
"""
Translates the given text to the given language
Args:
text: param destination_language:
source_language: Default value = "auto-detect")
destination_language:
Returns:
Tuple(str, str) --> tuple with source_lang, translation
"""
response = self.session_manager.send("https://www.bing.com/ttranslatev3", data={'text': text, 'fromLang': source_language, 'to': destination_language})
return response[0]["translations"][0]["text"]
def _example(self, text, destination_language, source_language, translation) -> str:
"""
Gives examples for the given text
Args:
text: param destination_language:
source_language: Default value = "auto-detect")
destination_language:
Returns:
Tuple(str, list[str]) --> tuple with source_lang, [examples]
"""
if translation is None:
source_language, translation = self.translate(text, destination_language, source_language)
if source_language == "auto-detect":
source_language = self._language(text)
response = self.session_manager.send("https://www.bing.com/texamplev3", data={'text': text.lower(), 'from': source_language, 'to': destination_language, 'translation': translation.lower()})
return [Example(example) for example in response[0]["examples"]]
def _spellcheck(self, text: str, source_language: str) -> str:
"""
Checks the spelling of the given text
Args:
text: param source_language: (Default value = None)
source_language: (Default value = None)
Returns:
Tuple(str, str) --> tuple with source_lang, spellchecked_text
"""
if source_language == "auto-detect":
source_language = self._language(text)
response = self.session_manager.send("https://www.bing.com/tspellcheckv3", data={'text': text, 'fromLang': source_language})
result = response["correctedText"]
if result == "":
return text
return result
def _language(self, text: str) -> str:
"""
Gives back the language of the given text
Args:
text:
Returns:
str --> the language code
"""
response = self.session_manager.send("https://www.bing.com/ttranslatev3", data={'text': text, 'fromLang': "auto-detect", 'to': "en"})
return response[0]["detectedLanguage"]["language"]
def _transliterate(self, text: str, destination_language: str, source_language: str):
# TODO: alternative implementation, won't work
# request = requests.post("https://www.bing.com/ttransliteratev3", data={'text': text, 'language': source_language, 'toScript': destination_language})
response = self.session_manager.send("https://www.bing.com/ttranslatev3", data={'text': text, 'fromLang': source_language, 'to': destination_language})
# XXX: Not a predictable response from Bing Translate
try:
return response[1]["inputTransliteration"]
except IndexError:
return response[0]["translations"][0]["transliteration"]["text"]
def _dictionary(self, text: str, destination_language: str, source_language: str):
# TODO: Implement
raise UnsupportedMethod("Bing Translate doesn't support this method")
def _text_to_speech(self, text: str, source_language: str):
# TODO: Implement
raise UnsupportedMethod("Bing Translate doesn't support this method")
def _language_normalize(self, language):
# TODO
_normalized_language_code = language.alpha2
if _normalized_language_code == "auto":
return "auto-detect"
elif _normalized_language_code == "no":
return "nb"
elif _normalized_language_code == "pt":
return "pt-pt"
elif _normalized_language_code == "zh-cn":
return "zh-Hans"
elif _normalized_language_code == "zh-tw":
return "zh-Hant"
else:
return _normalized_language_code
def __repr__(self) -> str:
return "Microsoft Bing Translator"