2021-02-03 05:05:09 +08:00
|
|
|
"""
|
|
|
|
Yandex translator API
|
|
|
|
"""
|
2024-03-04 01:15:23 +08:00
|
|
|
|
|
|
|
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"
|
|
|
|
|
|
|
|
import os
|
2022-05-01 20:00:20 +08:00
|
|
|
from typing import List, Optional
|
|
|
|
|
2021-02-03 05:05:09 +08:00
|
|
|
import requests
|
2022-05-01 20:00:20 +08:00
|
|
|
|
|
|
|
from deep_translator.base import BaseTranslator
|
2024-03-04 01:15:23 +08:00
|
|
|
from deep_translator.constants import BASE_URLS, YANDEX_ENV_VAR
|
2022-05-01 20:00:20 +08:00
|
|
|
from deep_translator.exceptions import (
|
2024-03-04 01:15:23 +08:00
|
|
|
ApiKeyException,
|
2022-05-01 20:00:20 +08:00
|
|
|
RequestError,
|
|
|
|
ServerException,
|
|
|
|
TooManyRequests,
|
|
|
|
TranslationNotFound,
|
|
|
|
)
|
2024-03-04 01:15:23 +08:00
|
|
|
from deep_translator.validate import is_input_valid, request_failed
|
2021-02-03 05:05:09 +08:00
|
|
|
|
|
|
|
|
2022-05-01 20:00:20 +08:00
|
|
|
class YandexTranslator(BaseTranslator):
|
2021-02-03 05:05:09 +08:00
|
|
|
"""
|
2024-03-04 01:15:23 +08:00
|
|
|
class that wraps functions, which use the yandex translator
|
|
|
|
under the hood to translate word(s)
|
2021-02-03 05:05:09 +08:00
|
|
|
"""
|
|
|
|
|
2022-05-01 20:00:20 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
source: str = "en",
|
|
|
|
target: str = "de",
|
2024-03-04 01:15:23 +08:00
|
|
|
api_key: Optional[str] = os.getenv(YANDEX_ENV_VAR, None),
|
2022-05-01 20:00:20 +08:00
|
|
|
**kwargs
|
|
|
|
):
|
2021-02-03 05:05:09 +08:00
|
|
|
"""
|
|
|
|
@param api_key: your yandex api key
|
|
|
|
"""
|
|
|
|
if not api_key:
|
2024-03-04 01:15:23 +08:00
|
|
|
raise ApiKeyException(YANDEX_ENV_VAR)
|
2021-02-03 05:05:09 +08:00
|
|
|
self.api_key = api_key
|
|
|
|
self.api_version = "v1.5"
|
|
|
|
self.api_endpoints = {
|
|
|
|
"langs": "getLangs",
|
|
|
|
"detect": "detect",
|
|
|
|
"translate": "translate",
|
|
|
|
}
|
2022-05-01 20:00:20 +08:00
|
|
|
super().__init__(
|
2024-03-04 01:15:23 +08:00
|
|
|
base_url=BASE_URLS.get("YANDEX"),
|
|
|
|
source=source,
|
|
|
|
target=target,
|
|
|
|
**kwargs
|
2022-05-01 20:00:20 +08:00
|
|
|
)
|
2021-11-30 03:28:30 +08:00
|
|
|
|
|
|
|
def _get_supported_languages(self):
|
2021-02-03 05:05:09 +08:00
|
|
|
return set(x.split("-")[0] for x in self.dirs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def languages(self):
|
|
|
|
return self.get_supported_languages()
|
|
|
|
|
|
|
|
@property
|
2022-05-01 20:00:20 +08:00
|
|
|
def dirs(self, proxies: Optional[dict] = None):
|
2021-02-03 05:05:09 +08:00
|
|
|
try:
|
2024-03-04 01:15:23 +08:00
|
|
|
url = self._base_url.format(
|
|
|
|
version=self.api_version, endpoint="getLangs"
|
|
|
|
)
|
2021-02-03 05:05:09 +08:00
|
|
|
print("url: ", url)
|
2024-03-04 01:15:23 +08:00
|
|
|
response = requests.get(
|
|
|
|
url, params={"key": self.api_key}, proxies=proxies
|
|
|
|
)
|
2021-02-03 05:05:09 +08:00
|
|
|
except requests.exceptions.ConnectionError:
|
|
|
|
raise ServerException(503)
|
|
|
|
else:
|
|
|
|
data = response.json()
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
if request_failed(status_code=response.status_code):
|
2021-02-03 05:05:09 +08:00
|
|
|
raise ServerException(response.status_code)
|
|
|
|
return data.get("dirs")
|
|
|
|
|
2022-05-01 20:00:20 +08:00
|
|
|
def detect(self, text: str, proxies: Optional[dict] = None):
|
2021-02-03 05:05:09 +08:00
|
|
|
response = None
|
|
|
|
params = {
|
|
|
|
"text": text,
|
|
|
|
"format": "plain",
|
|
|
|
"key": self.api_key,
|
|
|
|
}
|
|
|
|
try:
|
2024-03-04 01:15:23 +08:00
|
|
|
url = self._base_url.format(
|
|
|
|
version=self.api_version, endpoint="detect"
|
|
|
|
)
|
2021-02-03 05:05:09 +08:00
|
|
|
response = requests.post(url, data=params, proxies=proxies)
|
|
|
|
|
|
|
|
except RequestError:
|
|
|
|
raise
|
|
|
|
except ConnectionError:
|
|
|
|
raise ServerException(503)
|
|
|
|
except ValueError:
|
|
|
|
raise ServerException(response.status_code)
|
|
|
|
else:
|
|
|
|
response = response.json()
|
2022-05-01 20:00:20 +08:00
|
|
|
language = response["lang"]
|
|
|
|
status_code = response["code"]
|
2021-02-03 05:05:09 +08:00
|
|
|
if status_code != 200:
|
|
|
|
raise RequestError()
|
|
|
|
elif not language:
|
|
|
|
raise ServerException(501)
|
|
|
|
return language
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def translate(
|
|
|
|
self, text: str, proxies: Optional[dict] = None, **kwargs
|
|
|
|
) -> str:
|
2022-05-01 20:00:20 +08:00
|
|
|
if is_input_valid(text):
|
|
|
|
params = {
|
|
|
|
"text": text,
|
|
|
|
"format": "plain",
|
|
|
|
"lang": self._target
|
|
|
|
if self._source == "auto"
|
|
|
|
else "{}-{}".format(self._source, self._target),
|
|
|
|
"key": self.api_key,
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
url = self._base_url.format(
|
|
|
|
version=self.api_version, endpoint="translate"
|
|
|
|
)
|
|
|
|
response = requests.post(url, data=params, proxies=proxies)
|
|
|
|
except ConnectionError:
|
|
|
|
raise ServerException(503)
|
|
|
|
else:
|
|
|
|
response = response.json()
|
|
|
|
|
|
|
|
if response["code"] == 429:
|
|
|
|
raise TooManyRequests()
|
|
|
|
|
|
|
|
if response["code"] != 200:
|
|
|
|
raise ServerException(response["code"])
|
|
|
|
|
|
|
|
if not response["text"]:
|
|
|
|
raise TranslationNotFound()
|
|
|
|
|
|
|
|
return response["text"]
|
|
|
|
|
|
|
|
def translate_file(self, path: str, **kwargs) -> str:
|
2021-02-03 05:05:09 +08:00
|
|
|
"""
|
|
|
|
translate from a file
|
|
|
|
@param path: path to file
|
|
|
|
@return: translated text
|
|
|
|
"""
|
2022-05-01 20:00:20 +08:00
|
|
|
return self._translate_file(path, **kwargs)
|
2021-02-03 05:05:09 +08:00
|
|
|
|
2022-05-01 20:00:20 +08:00
|
|
|
def translate_batch(self, batch: List[str], **kwargs) -> List[str]:
|
2021-02-03 05:05:09 +08:00
|
|
|
"""
|
|
|
|
translate a batch of texts
|
|
|
|
@param batch: list of texts to translate
|
|
|
|
@return: list of translations
|
|
|
|
"""
|
2022-05-01 20:00:20 +08:00
|
|
|
return self._translate_batch(batch, **kwargs)
|