def translate(self, text, proxies=None, **kwargs): 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(self, source, target, text): # Create the request parameters. translate_endpoint = 'translate' params = { "auth_key": self.api_key, "target_lang": self._map_language_to_code(target), "source_lang": self._map_language_to_code(source), "text": text } # Do the request and check the connection. try: response = requests.get(self.__base_url + translate_endpoint, params=params) except ConnectionError: raise ServerException(503) # If the answer is not success, raise server exception. if response.status_code == 403: raise AuthorizationException(self.api_key) elif response.status_code != 200: raise ServerException(response.status_code) # Get the response and check is not empty. res = response.json() if not res: raise TranslationNotFound(text) # Process and return the response. return res['translations'][0]['text']
def detect(self, text, proxies=None): response = None params = { "text": text, "format": "plain", "key": self.api_key, } try: url = self.__base_url.format(version=self.api_version, endpoint="detect") 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() language = response['lang'] status_code = response['code'] if status_code != 200: raise RequestError() elif not language: raise ServerException(501) return language
def dirs(self, proxies=None): try: url = self.__base_url.format(version=self.api_version, endpoint="getLangs") print("url: ", url) response = requests.get(url, params={"key": self.api_key}, proxies=proxies) except requests.exceptions.ConnectionError: raise ServerException(503) else: data = response.json() if response.status_code != 200: raise ServerException(response.status_code) return data.get("dirs")
def __init__(self, api_key=None, region=None, source=None, target=None, proxies=None, **kwargs): """ @params api_key and target are the required params @param api_key: your Microsoft API key @param region: your Microsoft Location """ if not api_key: raise ServerException(401) else: self.api_key = api_key self.proxies = proxies self.headers = { "Ocp-Apim-Subscription-Key": self.api_key, "Content-type": "application/json", } # region is not required but very common and goes to headers if passed if region: self.region = region self.headers["Ocp-Apim-Subscription-Region"] = self.region if not target: raise ServerException(401) else: if type(target) is str: self.target = target.lower() else: self.target = [i.lower() for i in target] if self.is_language_supported(self.target): self.target = self._map_language_to_code(self.target) self.url_params = {'to': self.target, **kwargs} if source: self.source = source.lower() if self.is_language_supported(self.source): self.source = self._map_language_to_code(self.source) self.url_params['from'] = self.source self.__base_url = BASE_URLS.get("MICROSOFT_TRANSLATE")
def __init__(self, api_key=None): """ @param api_key: your DeepL api key. Get one here: https://www.deepl.com/docs-api/accessing-the-api/ """ if not api_key: raise ServerException(401) self.version = 'v2' self.api_key = api_key self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
def translate(self, source, target, text): params = { "auth_key": self.api_key, "target_lang": target, "source_lang": source, "text": text } try: response = requests.get(self.__base_url, params=params) except ConnectionError: raise ServerException(503) else: if response.status_code != 200: ServerException(response.status_code) else: res = response.json() if not res: raise TranslationNotFound(text) return res
def translate(self, text, domain, **kwargs): params = { "key": self.api_key, "langpair": "{}-{}".format(self.source, self.target), "domain": domain, "text": text } try: response = self._get("translate", params=params, return_text=False) except ConnectionError: raise ServerException(503) else: if response.status_code != 200: ServerException(response.status_code) else: res = response.json() translation = res.get("translatedText") if not translation: raise TranslationNotFound(text) return translation
def __init__(self, api_key=None, source="en", target="en"): """ @param api_key: your DeepL api key. Get one here: https://www.deepl.com/docs-api/accessing-the-api/ @param source: source language @param target: target language """ if not api_key: raise ServerException(401) self.version = 'v2' self.api_key = api_key self.source = self._map_language_to_code(source) self.target = self._map_language_to_code(target) self.__base_url = BASE_URLS.get("DEEPL").format(version=self.version)
def __init__(self, api_key=None): """ @param api_key: your yandex api key """ if not api_key: raise ServerException(401) self.__base_url = BASE_URLS.get("YANDEX") self.api_key = api_key self.api_version = "v1.5" self.api_endpoints = { "langs": "getLangs", "detect": "detect", "translate": "translate", }
def __init__(self, api_key=None, source="en", target="en", **kwargs): """ @param api_key: your qrci api key. Get one for free here https://mt.qcri.org/api/v1/ref """ if not api_key: raise ServerException(401) self.__base_url = BASE_URLS.get("QCRI") self.source = source self.target = target self.api_key = api_key self.api_endpoints = { "get_languages": "getLanguagePairs", "get_domains": "getDomains", "translate": "translate", } self.params = {"key": self.api_key}