-
Notifications
You must be signed in to change notification settings - Fork 61
/
yandex.py
189 lines (141 loc) · 7.44 KB
/
yandex.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
"""
Yandex Translate
This implementation was made specifically for translatepy from 'Zhymabek Roman', based on 'Anime no Sekai' version.
"""
import uuid
from translatepy.exceptions import UnsupportedMethod
from translatepy.language import Language
from translatepy.translators.base import BaseTranslateException, BaseTranslator
from translatepy.utils.request import Request
class YandexTranslateException(BaseTranslateException):
"""
Default Yandex Translate exception
"""
error_codes = {
401: "ERR_KEY_INVALID",
402: "ERR_KEY_BLOCKED",
403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
408: "ERR_MONTHLY_CHAR_LIMIT_EXCEEDED",
413: "ERR_TEXT_TOO_LONG",
422: "ERR_UNPROCESSABLE_TEXT",
501: "ERR_LANG_NOT_SUPPORTED",
503: "ERR_SERVICE_NOT_AVAIBLE",
}
class YandexTranslate(BaseTranslator):
"""
Yandex Translation Implementation
"""
_api_url = "http://translate.yandex.net/api/v1/tr.json/{endpoint}"
_supported_languages = {'auto', 'af', 'sq', 'am', 'ar', 'hy', 'az', 'ba', 'eu', 'be', 'bn', 'bs', 'bg', 'my', 'ca', 'ca', 'ceb', 'zh', 'cv', 'cs', 'da', 'nl', 'nl', 'en', 'eo', 'et', 'fi', 'fr', 'ka', 'de', 'gd', 'gd', 'ga', 'gl', 'el', 'gu', 'ht', 'ht', 'he', 'hi', 'hr', 'hu', 'is', 'id', 'it', 'jv', 'ja', 'kn', 'kk', 'km', 'ky', 'ky', 'ko', 'lo', 'la', 'lv', 'lt', 'lb', 'lb', 'mk', 'ml', 'mi', 'mr', 'ms', 'mg', 'mt', 'mn', 'mrj', 'mhr', 'ne', 'no', 'pa', 'pa', 'pap', 'fa', 'pl', 'pt', 'ro', 'ro', 'ro', 'ru', 'sah', 'si', 'si', 'sk', 'sl', 'es', 'es', 'sr', 'sjn', 'su', 'sw', 'sv', 'ta', 'tt', 'te', 'tg', 'tl', 'th', 'tr', 'udm', 'uk', 'ur', 'uz', 'vi', 'cy', 'xh', 'yi', 'zu', 'kazlat', 'uzbcyr', 'emj'}
def __init__(self, request: Request = Request()):
self.session = request
self.session.header = {"User-Agent": "ru.yandex.translate/22.11.8.22364114 (samsung SM-A505GM; Android 12)"} # TODO: generate random telephone model
uuid_v4 = str(uuid.uuid4())
self.session_ucid = uuid_v4.replace("-", "")
self.session_request_id = 0
def _ucid(self, session_state: bool = False) -> str:
"""
Generates UUID (UCID / (U)SID) for Yandex Translate API requests
Args:
Returns:
str --> Yandex UUID value
"""
if session_state:
request_id = self.session_request_id
self.session_request_id += 1
return "{ucid}-{request_id}-0".format(ucid=self.session_ucid, request_id=request_id)
return self.session_ucid
def _translate(self, text: str, destination_language: str, source_language: str) -> str:
if source_language == "auto":
source_language = self._language(text)
url = self._api_url.format(endpoint="translate")
params = {"sid": self._ucid(session_state=True), "srv": "android", "format": "text"}
data = {"text": text, "lang": source_language + "-" + destination_language}
request = self.session.post(url, params=params, data=data)
response = request.json()
if request.status_code != 200 and response["code"] != 200:
raise YandexTranslateException(response["code"])
try:
_detected_language = str(data["lang"]).split("-")[0]
except Exception:
_detected_language = source_language
return _detected_language, response["text"][0]
def _transliterate(self, text: str, destination_language: str, source_language: str) -> str:
if source_language == "auto":
source_language = self._language(text)
url = "https://translate.yandex.net/translit/translit"
data = {'text': text, 'lang': source_language + "-" + destination_language}
request = self.session.post(url, data=data)
if request.status_code != 200:
raise YandexTranslateException(request.status_code)
return source_language, request.text[1:-1]
def _spellcheck(self, text: str, source_language: str) -> str:
if source_language == "auto":
source_language = self._language(text)
url = "https://speller.yandex.net/services/spellservice.json/checkText"
params = {"sid": self._ucid(), "srv": "android"}
data = {"text": text, "lang": source_language, "options": 8 + 4}
request = self.session.post(url, params=params, data=data)
response = request.json()
if request.status_code != 200:
raise YandexTranslateException(request.status_code)
for correction in response:
if correction["s"]:
word = correction['word']
suggestion = correction['s'][0]
text = text.replace(word, suggestion)
return source_language, text
def _language(self, text: str):
url = self._api_url.format(endpoint="detect")
params = {"sid": self._ucid(), "srv": "android"}
data = {'text': text, 'hint': "en"}
request = self.session.get(url, params=params, data=data)
response = request.json()
if request.status_code != 200 and response["code"] != 200:
raise YandexTranslateException(response["code"])
return response["lang"]
def _example(self, text: str, destination_language: str, source_language: str):
if source_language == "auto":
source_language = self._language(text)
url = "https://dictionary.yandex.net/dicservice.json/queryCorpus"
params = {"sid": self._ucid(), "srv": "android", "src": text, "ui": "en", "lang": source_language + "-" + destination_language, "flags": 7}
request = self.session.get(url, params=params)
if request.status_code != 200:
raise YandexTranslateException(request.status_code)
response = request.json()
_result = []
for examples_group in response["result"]:
for sentense in examples_group["examples"]:
_sentense_result = sentense["dst"]
_sentense_result = _sentense_result.replace("<", "").replace(">", "")
_result.append(_sentense_result)
return source_language, _result
def _dictionary(self, text: str, destination_language: str, source_language: str):
if source_language == "auto":
source_language = self._language(text)
url = "https://dictionary.yandex.net/dicservice.json/lookupMultiple"
params = {"sid": self._ucid(), "srv": "android", "text": text, "ui": "en", "dict": source_language + "-" + destination_language, "flags": 7, "dict_type": "regular"}
request = self.session.get(url, params=params)
if request.status_code != 200:
raise YandexTranslateException(request.status_code)
response = request.json()
_result = []
for word in response["{}-{}".format(source_language, destination_language)]["regular"]:
_word_result = word["tr"][0]["text"]
_result.append(_word_result)
return source_language, _result
def _language_normalize(self, language):
if language.id == "zho":
return "zh"
elif language.id == "srd":
return "sjn"
return language.alpha2
def _language_denormalize(self, language_code):
if str(language_code).lower() in {"zh", "zh-cn"}:
return Language("zho")
elif str(language_code).lower() == "sjn":
return Language("srd")
return Language(language_code)
def __str__(self) -> str:
return "Yandex"