-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation_api.py
151 lines (136 loc) · 4.6 KB
/
translation_api.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
# coding: utf-8
import requests
import requests.exceptions
class YandexTranslateException(Exception):
"""
Default YandexTranslate exception
"""
error_codes = {
401: "ERR_KEY_INVALID",
402: "ERR_KEY_BLOCKED",
403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
413: "ERR_TEXT_TOO_LONG",
422: "ERR_UNPROCESSABLE_TEXT",
501: "ERR_LANG_NOT_SUPPORTED",
503: "ERR_SERVICE_NOT_AVAIBLE",
}
def __init__(self, status_code, *args, **kwargs):
message = self.error_codes.get(status_code)
super(YandexTranslateException, self).__init__(message, *args, **kwargs)
class YandexTranslate(object):
api_url = "https://translate.yandex.net/api/{version}/tr.json/{endpoint}"
api_version = "v1.5"
api_endpoints = {
"langs": "getLangs",
"detect": "detect",
"translate": "translate",
}
def __init__(self, key=None):
"""
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> len(translate.api_endpoints)
3
"""
if not key:
raise YandexTranslateException(401)
self.api_key = key
def url(self, endpoint):
"""
Returns full URL for specified API endpoint
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> translate.url("langs")
'https://translate.yandex.net/api/v1.5/tr.json/getLangs'
>>> translate.url("detect")
'https://translate.yandex.net/api/v1.5/tr.json/detect'
>>> translate.url("translate")
'https://translate.yandex.net/api/v1.5/tr.json/translate'
"""
return self.api_url.format(version=self.api_version,
endpoint=self.api_endpoints[endpoint])
@property
def directions(self, proxies=None):
"""
Returns list with translate directions
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> directions = translate.directions
>>> len(directions) > 0
True
"""
try:
response = requests.get(self.url("langs"), params={"key": self.api_key}, proxies=proxies)
except requests.exceptions.ConnectionError:
raise YandexTranslateException(self.error_codes[503])
else:
response = response.json()
status_code = response.get("code", 200)
if status_code != 200:
raise YandexTranslateException(status_code)
return response.get("dirs")
@property
def langs(self):
"""
Returns list with supported languages
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> languages = translate.langs
>>> len(languages) > 0
True
"""
return set(x.split("-")[0] for x in self.directions)
def detect(self, text, proxies=None, format="plain"):
"""
Specifies language of text
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> result = translate.detect(text="Hello world!")
>>> result == "en"
True
"""
data = {
"text": text,
"format": format,
"key": self.api_key,
}
try:
response = requests.post(self.url("detect"), data=data, proxies=proxies)
except ConnectionError:
raise YandexTranslateException(self.error_codes[503])
except ValueError:
raise YandexTranslateException(response)
else:
response = response.json()
language = response.get("lang", None)
status_code = response.get("code", 200)
if status_code != 200:
raise YandexTranslateException(status_code)
elif not language:
raise YandexTranslateException(501)
return language
def translate(self, text, lang, proxies=None, format="plain"):
"""
Translates text to passed language
>>> translate = YandexTranslate("trnsl.1.1.20130421T140201Z.323e508a33e9d84b.f1e0d9ca9bcd0a00b0ef71d82e6cf4158183d09e")
>>> result = translate.translate(lang="ru", text="Hello, world!")
>>> result["code"] == 200
True
>>> result["lang"] == "en-ru"
True
"""
data = {
"text": text,
"format": format,
"lang": lang,
"key": self.api_key
}
try:
response = requests.post(self.url("translate"), data=data, proxies=proxies)
except ConnectionError:
raise YandexTranslateException(503)
else:
response = response.json()
status_code = response.get("code", 200)
if status_code != 200:
raise YandexTranslateException(status_code)
return response
if __name__ == "__main__":
import doctest
doctest.testmod()