Saminul commited on
Commit
5a8d2be
·
1 Parent(s): 1d42175

implemented function for LibreTranslator class

Browse files
deep_translator/constants.py CHANGED
@@ -11,7 +11,9 @@ BASE_URLS = {
11
  "DEEPL_FREE": "https://api-free.deepl.com/v2/",
12
  "MICROSOFT_TRANSLATE": "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0",
13
  "PAPAGO": "https://papago.naver.com/",
14
- "PAPAGO_API": "https://openapi.naver.com/v1/papago/n2mt"
 
 
15
  }
16
 
17
  GOOGLE_CODES_TO_LANGUAGES = {
@@ -280,5 +282,5 @@ LIBRE_CODES_TO_LANGUAGES = {
280
  }
281
 
282
  LIBRE_LANGUAGES_TO_CODES = {
283
- v: k for k, v in LIBRE_CODE_TO_LANGUAGE.items()
284
  }
 
11
  "DEEPL_FREE": "https://api-free.deepl.com/v2/",
12
  "MICROSOFT_TRANSLATE": "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0",
13
  "PAPAGO": "https://papago.naver.com/",
14
+ "PAPAGO_API": "https://openapi.naver.com/v1/papago/n2mt",
15
+ "LIBRE": "https://libretranslate.com/",
16
+ "LIBRE_FREE": "https://libretranslate.de/",
17
  }
18
 
19
  GOOGLE_CODES_TO_LANGUAGES = {
 
282
  }
283
 
284
  LIBRE_LANGUAGES_TO_CODES = {
285
+ v: k for k, v in LIBRE_CODES_TO_LANGUAGES.items()
286
  }
deep_translator/exceptions.py CHANGED
@@ -123,12 +123,15 @@ class ServerException(Exception):
123
  Default YandexTranslate exception from the official website
124
  """
125
  errors = {
 
126
  401: "ERR_KEY_INVALID",
127
  402: "ERR_KEY_BLOCKED",
128
  403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
129
  404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
130
  413: "ERR_TEXT_TOO_LONG",
 
131
  422: "ERR_UNPROCESSABLE_TEXT",
 
132
  501: "ERR_LANG_NOT_SUPPORTED",
133
  503: "ERR_SERVICE_NOT_AVAIBLE",
134
  }
 
123
  Default YandexTranslate exception from the official website
124
  """
125
  errors = {
126
+ 400: "ERR_BAD_REQUEST",
127
  401: "ERR_KEY_INVALID",
128
  402: "ERR_KEY_BLOCKED",
129
  403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
130
  404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
131
  413: "ERR_TEXT_TOO_LONG",
132
+ 429: "ERR_TOO_MANY_REQUESTS",
133
  422: "ERR_UNPROCESSABLE_TEXT",
134
+ 500: "ERR_INTERNAL_SERVER_ERROR",
135
  501: "ERR_LANG_NOT_SUPPORTED",
136
  503: "ERR_SERVICE_NOT_AVAIBLE",
137
  }
deep_translator/libre.py CHANGED
@@ -2,27 +2,39 @@
2
  LibreTranslate API
3
  """
4
 
5
- from requests.models import Response
6
- from .parent import BaseTranslator
7
  import requests
8
  from bs4 import BeautifulSoup
9
- from .constants import LIBRE_LANGUAGES_TO_CODES, LIBRE_CODES_TO_LANGUAGES
 
 
 
 
 
 
10
 
11
 
12
  class LibreTranslator(BaseTranslator):
13
  """
14
  class that wraps functions, which use libre translator under the hood to translate text(s)
15
  """
 
 
16
 
17
- def __init__(self, base_url, api_key, source="auto", target="en", proxies=None, **kwargs):
18
  """
19
  @param source: source language to translate from
 
 
20
  @param target: target language to translate to
21
  """
22
- _languages = LIBRE_LANGUAGES_TO_CODES
23
- supported_languages = list(_languages.keys())
24
- return None
25
-
 
 
 
 
26
  @staticmethod
27
  def get_supported_languages(as_dict=False, **kwargs):
28
  """
@@ -30,22 +42,27 @@ class LibreTranslator(BaseTranslator):
30
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
31
  @return: list or dict
32
  """
33
- return None
34
 
35
- def _map_language_to_code(self, *languages):
36
  """
37
  map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
38
- @param languages: list of languages
39
  @return: mapped value of the language or raise an exception if the language is not supported
40
  """
41
- return None
 
 
 
 
42
 
43
- def is_language_supported(self, language, **kwargs):
44
  """
45
  check if the language is supported by the translator
46
- @param language: a string (if 1 lang) or a list (if multiple langs)
47
  @return: bool or raise an Exception
48
  """
 
49
 
50
  def translate(self, text, **kwargs):
51
  """
@@ -53,8 +70,29 @@ class LibreTranslator(BaseTranslator):
53
  @param text: desired text to translate
54
  @return: str: translated text
55
  """
56
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- response = requests.post("https://libretranslate.de/detect",data={'q': 'bonjour'})
59
- soup = BeautifulSoup(response.text, 'html.parser')
60
- print(soup)
 
2
  LibreTranslate API
3
  """
4
 
 
 
5
  import requests
6
  from bs4 import BeautifulSoup
7
+ from requests.models import Response
8
+ from .parent import BaseTranslator
9
+ from .constants import BASE_URLS,LIBRE_LANGUAGES_TO_CODES, LIBRE_CODES_TO_LANGUAGES
10
+ from .exceptions import (ServerException,
11
+ TranslationNotFound,
12
+ LanguageNotSupportedException,
13
+ AuthorizationException)
14
 
15
 
16
  class LibreTranslator(BaseTranslator):
17
  """
18
  class that wraps functions, which use libre translator under the hood to translate text(s)
19
  """
20
+ _languages = LIBRE_LANGUAGES_TO_CODES
21
+ supported_languages = list(_languages.keys())
22
 
23
+ def __init__(self, base_url = BASE_URLS.get("LIBRE_FREE"), api_key="", source="auto", target="en", **kwargs):
24
  """
25
  @param source: source language to translate from
26
+ List of LibreTranslate nedpoints can be found at : https://github.com/LibreTranslate/LibreTranslate#mirrors
27
+ Some require an API key
28
  @param target: target language to translate to
29
  """
30
+ if base_url == BASE_URLS.get("LIBRE") and not api_key:
31
+ raise ServerException(401)
32
+ self.__base_url = base_url
33
+ self.api_key = api_key
34
+ self.source = self._map_language_to_code(source)
35
+ self.target = self._map_language_to_code(target)
36
+
37
+
38
  @staticmethod
39
  def get_supported_languages(as_dict=False, **kwargs):
40
  """
 
42
  @param as_dict: if True, the languages will be returned as a dictionary mapping languages to their abbreviations
43
  @return: list or dict
44
  """
45
+ return [*LibreTranslator._languages.keys()] if not as_dict else LibreTranslator._languages
46
 
47
+ def _map_language_to_code(self, language, **kwargs):
48
  """
49
  map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
50
+ @param language: a string for 1 language
51
  @return: mapped value of the language or raise an exception if the language is not supported
52
  """
53
+ if language in self._languages.keys():
54
+ return self._languages[language]
55
+ elif language in self._languages.values():
56
+ return language
57
+ raise LanguageNotSupportedException(language)
58
 
59
+ def _is_language_supported(self, language, **kwargs):
60
  """
61
  check if the language is supported by the translator
62
+ @param language: a string for 1 language
63
  @return: bool or raise an Exception
64
  """
65
+ return language == 'auto' or language in self._languages.keys() or language in self._languages.values()
66
 
67
  def translate(self, text, **kwargs):
68
  """
 
70
  @param text: desired text to translate
71
  @return: str: translated text
72
  """
73
+ # Create the request parameters.
74
+ translate_endpoint = 'translate'
75
+ params = {
76
+ "q": text,
77
+ "source": self.source,
78
+ "target": self.target,
79
+ "api_key": self.api_key
80
+ }
81
+ # Do the request and check the connection.
82
+ try:
83
+ response = requests.post(self.__base_url + translate_endpoint, params=params)
84
+ except ConnectionError:
85
+ raise ServerException(503)
86
+ # If the answer is not success, raise server exception.
87
+
88
+ if response.status_code == 403:
89
+ raise AuthorizationException(self.api_key)
90
+ elif response.status_code != 200:
91
+ raise ServerException(response.status_code)
92
+ # Get the response and check is not empty.
93
+ res = response.json()
94
+ if not res:
95
+ raise TranslationNotFound(text)
96
+ # Process and return the response.
97
+ return res['translatedText']
98