nidhal baccouri
commited on
Commit
·
c9c6f12
1
Parent(s):
fcb8bdf
added proxy to googletrans
Browse files
deep_translator/google_trans.py
CHANGED
|
@@ -19,12 +19,13 @@ class GoogleTranslator(BaseTranslator):
|
|
| 19 |
_languages = GOOGLE_LANGUAGES_TO_CODES
|
| 20 |
supported_languages = list(_languages.keys())
|
| 21 |
|
| 22 |
-
def __init__(self, source="auto", target="en"):
|
| 23 |
"""
|
| 24 |
@param source: source language to translate from
|
| 25 |
@param target: target language to translate to
|
| 26 |
"""
|
| 27 |
self.__base_url = BASE_URLS.get("GOOGLE_TRANSLATE")
|
|
|
|
| 28 |
|
| 29 |
if self.is_language_supported(source, target):
|
| 30 |
self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
|
|
@@ -36,7 +37,8 @@ class GoogleTranslator(BaseTranslator):
|
|
| 36 |
element_query={"class": "t0"},
|
| 37 |
payload_key='q', # key of text in the url
|
| 38 |
hl=self._target,
|
| 39 |
-
sl=self._source
|
|
|
|
| 40 |
|
| 41 |
self._alt_element_query = {"class": "result-container"}
|
| 42 |
|
|
@@ -89,8 +91,8 @@ class GoogleTranslator(BaseTranslator):
|
|
| 89 |
self._url_params[self.payload_key] = text
|
| 90 |
|
| 91 |
response = requests.get(self.__base_url,
|
| 92 |
-
params=self._url_params,
|
| 93 |
-
|
| 94 |
if response.status_code == 429:
|
| 95 |
raise TooManyRequests()
|
| 96 |
|
|
@@ -180,7 +182,8 @@ class GoogleTranslator(BaseTranslator):
|
|
| 180 |
return arr
|
| 181 |
|
| 182 |
|
| 183 |
-
|
| 184 |
-
#
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
| 19 |
_languages = GOOGLE_LANGUAGES_TO_CODES
|
| 20 |
supported_languages = list(_languages.keys())
|
| 21 |
|
| 22 |
+
def __init__(self, source="auto", target="en", proxies=None, **kwargs):
|
| 23 |
"""
|
| 24 |
@param source: source language to translate from
|
| 25 |
@param target: target language to translate to
|
| 26 |
"""
|
| 27 |
self.__base_url = BASE_URLS.get("GOOGLE_TRANSLATE")
|
| 28 |
+
self.proxies = proxies
|
| 29 |
|
| 30 |
if self.is_language_supported(source, target):
|
| 31 |
self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
|
|
|
|
| 37 |
element_query={"class": "t0"},
|
| 38 |
payload_key='q', # key of text in the url
|
| 39 |
hl=self._target,
|
| 40 |
+
sl=self._source,
|
| 41 |
+
**kwargs)
|
| 42 |
|
| 43 |
self._alt_element_query = {"class": "result-container"}
|
| 44 |
|
|
|
|
| 91 |
self._url_params[self.payload_key] = text
|
| 92 |
|
| 93 |
response = requests.get(self.__base_url,
|
| 94 |
+
params=self._url_params,
|
| 95 |
+
proxies=self.proxies)
|
| 96 |
if response.status_code == 429:
|
| 97 |
raise TooManyRequests()
|
| 98 |
|
|
|
|
| 182 |
return arr
|
| 183 |
|
| 184 |
|
| 185 |
+
if __name__ == '__main__':
|
| 186 |
+
# test proxy config
|
| 187 |
+
proxies = None
|
| 188 |
+
text = GoogleTranslator(source="en", target="de", proxies=proxies).translate("you are good")
|
| 189 |
+
print(text)
|
deep_translator/linguee.py
CHANGED
|
@@ -22,12 +22,13 @@ class LingueeTranslator(BaseTranslator):
|
|
| 22 |
_languages = LINGUEE_LANGUAGES_TO_CODES
|
| 23 |
supported_languages = list(_languages.keys())
|
| 24 |
|
| 25 |
-
def __init__(self, source, target="en"):
|
| 26 |
"""
|
| 27 |
@param source: source language to translate from
|
| 28 |
@param target: target language to translate to
|
| 29 |
"""
|
| 30 |
self.__base_url = BASE_URLS.get("LINGUEE")
|
|
|
|
| 31 |
|
| 32 |
if self.is_language_supported(source, target):
|
| 33 |
self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
|
|
@@ -88,7 +89,7 @@ class LingueeTranslator(BaseTranslator):
|
|
| 88 |
# %s-%s/translation/%s.html
|
| 89 |
url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word)
|
| 90 |
url = requote_uri(url)
|
| 91 |
-
response = requests.get(url)
|
| 92 |
|
| 93 |
if response.status_code == 429:
|
| 94 |
raise TooManyRequests()
|
|
|
|
| 22 |
_languages = LINGUEE_LANGUAGES_TO_CODES
|
| 23 |
supported_languages = list(_languages.keys())
|
| 24 |
|
| 25 |
+
def __init__(self, source, target="en", proxies=None):
|
| 26 |
"""
|
| 27 |
@param source: source language to translate from
|
| 28 |
@param target: target language to translate to
|
| 29 |
"""
|
| 30 |
self.__base_url = BASE_URLS.get("LINGUEE")
|
| 31 |
+
self.proxies = proxies
|
| 32 |
|
| 33 |
if self.is_language_supported(source, target):
|
| 34 |
self._source, self._target = self._map_language_to_code(source.lower(), target.lower())
|
|
|
|
| 89 |
# %s-%s/translation/%s.html
|
| 90 |
url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word)
|
| 91 |
url = requote_uri(url)
|
| 92 |
+
response = requests.get(url, proxies=self.proxies)
|
| 93 |
|
| 94 |
if response.status_code == 429:
|
| 95 |
raise TooManyRequests()
|
deep_translator/microsoft.py
CHANGED
|
@@ -17,7 +17,7 @@ class MicrosoftTranslator:
|
|
| 17 |
_languages = MICROSOFT_CODES_TO_LANGUAGES
|
| 18 |
supported_languages = list(_languages.values())
|
| 19 |
|
| 20 |
-
def __init__(self, api_key=None, region=None, source=None, target=None, **kwargs):
|
| 21 |
"""
|
| 22 |
@params api_key and target are the required params
|
| 23 |
@param api_key: your Microsoft API key
|
|
@@ -27,6 +27,8 @@ class MicrosoftTranslator:
|
|
| 27 |
raise ServerException(401)
|
| 28 |
else:
|
| 29 |
self.api_key = api_key
|
|
|
|
|
|
|
| 30 |
self.headers = {
|
| 31 |
"Ocp-Apim-Subscription-Key": self.api_key,
|
| 32 |
"Content-type": "application/json",
|
|
@@ -108,7 +110,8 @@ class MicrosoftTranslator:
|
|
| 108 |
requested = requests.post(self.__base_url,
|
| 109 |
params=self.url_params,
|
| 110 |
headers=self.headers,
|
| 111 |
-
json=valid_microsoft_json
|
|
|
|
| 112 |
except requests.exceptions.RequestException:
|
| 113 |
exc_type, value, traceback = sys.exc_info()
|
| 114 |
logging.warning(f"Returned error: {exc_type.__name__}")
|
|
|
|
| 17 |
_languages = MICROSOFT_CODES_TO_LANGUAGES
|
| 18 |
supported_languages = list(_languages.values())
|
| 19 |
|
| 20 |
+
def __init__(self, api_key=None, region=None, source=None, target=None, proxies=None, **kwargs):
|
| 21 |
"""
|
| 22 |
@params api_key and target are the required params
|
| 23 |
@param api_key: your Microsoft API key
|
|
|
|
| 27 |
raise ServerException(401)
|
| 28 |
else:
|
| 29 |
self.api_key = api_key
|
| 30 |
+
|
| 31 |
+
self.proxies = proxies
|
| 32 |
self.headers = {
|
| 33 |
"Ocp-Apim-Subscription-Key": self.api_key,
|
| 34 |
"Content-type": "application/json",
|
|
|
|
| 110 |
requested = requests.post(self.__base_url,
|
| 111 |
params=self.url_params,
|
| 112 |
headers=self.headers,
|
| 113 |
+
json=valid_microsoft_json,
|
| 114 |
+
proxies=self.proxies)
|
| 115 |
except requests.exceptions.RequestException:
|
| 116 |
exc_type, value, traceback = sys.exc_info()
|
| 117 |
logging.warning(f"Returned error: {exc_type.__name__}")
|