= commited on
Commit
f5c8ab8
·
1 Parent(s): 8d18691

catched too many requests exception

Browse files
deep_translator/exceptions.py CHANGED
@@ -81,6 +81,18 @@ class RequestError(Exception):
81
  return self.message
82
 
83
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  class YandexDefaultException(Exception):
85
  """
86
  Default YandexTranslate exception from the official website
 
81
  return self.message
82
 
83
 
84
+ class TooManyRequests(Exception):
85
+ """
86
+ exception thrown if an error occured during the request call, e.g a connection problem.
87
+ """
88
+
89
+ def __init__(self, message="Server Error: You made too many requests to the server. According to google, you are allowed to make 5 requests per second and up to 200k requests per day. You can wait and try again later or you can try the translate_batch function"):
90
+ self.message = message
91
+
92
+ def __str__(self):
93
+ return self.message
94
+
95
+
96
  class YandexDefaultException(Exception):
97
  """
98
  Default YandexTranslate exception from the official website
deep_translator/google_trans.py CHANGED
@@ -3,10 +3,11 @@ google translator API
3
  """
4
 
5
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
6
- from deep_translator.exceptions import LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
7
  from deep_translator.parent import BaseTranslator
8
  from bs4 import BeautifulSoup
9
  import requests
 
10
 
11
 
12
  class GoogleTranslator(BaseTranslator):
@@ -84,9 +85,13 @@ class GoogleTranslator(BaseTranslator):
84
  self._url_params[self.payload_key] = text
85
 
86
  response = requests.get(self.__base_url,
87
- params=self._url_params)
 
 
 
88
 
89
  if response.status_code != 200:
 
90
  raise RequestError()
91
 
92
  soup = BeautifulSoup(response.text, 'html.parser')
@@ -136,3 +141,24 @@ class GoogleTranslator(BaseTranslator):
136
  except Exception as e:
137
  raise e
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  """
4
 
5
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
6
+ from deep_translator.exceptions import TooManyRequests, LanguageNotSupportedException, TranslationNotFound, NotValidPayload, RequestError
7
  from deep_translator.parent import BaseTranslator
8
  from bs4 import BeautifulSoup
9
  import requests
10
+ from time import sleep
11
 
12
 
13
  class GoogleTranslator(BaseTranslator):
 
85
  self._url_params[self.payload_key] = text
86
 
87
  response = requests.get(self.__base_url,
88
+ params=self._url_params, headers ={'User-agent': 'your bot 0.1'})
89
+
90
+ if response.status_code == 429:
91
+ raise TooManyRequests()
92
 
93
  if response.status_code != 200:
94
+ print("status code", response.status_code)
95
  raise RequestError()
96
 
97
  soup = BeautifulSoup(response.text, 'html.parser')
 
141
  except Exception as e:
142
  raise e
143
 
144
+ def translate_batch(self, batch=None):
145
+ """
146
+ translate a list of texts
147
+ @param batch: list of texts you want to translate
148
+ @return: list of translations
149
+ """
150
+ if not batch:
151
+ raise Exception("Enter your text list that you want to translate")
152
+
153
+ arr = []
154
+ for text in batch:
155
+ translated = self.translate(text)
156
+ arr.append(translated)
157
+ sleep(2)
158
+
159
+ return arr
160
+
161
+
162
+ if __name__ == '__main__':
163
+ res = GoogleTranslator(target="fr").translate("good")
164
+ print(res)
deep_translator/linguee.py CHANGED
@@ -7,7 +7,8 @@ from deep_translator.exceptions import (LanguageNotSupportedException,
7
  TranslationNotFound,
8
  NotValidPayload,
9
  ElementNotFoundInGetRequest,
10
- RequestError)
 
11
  from deep_translator.parent import BaseTranslator
12
  from bs4 import BeautifulSoup
13
  import requests
@@ -88,6 +89,10 @@ class LingueeTranslator(BaseTranslator):
88
  url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word)
89
  url = requote_uri(url)
90
  response = requests.get(url)
 
 
 
 
91
  if response.status_code != 200:
92
  raise RequestError()
93
  soup = BeautifulSoup(response.text, 'html.parser')
 
7
  TranslationNotFound,
8
  NotValidPayload,
9
  ElementNotFoundInGetRequest,
10
+ RequestError,
11
+ TooManyRequests)
12
  from deep_translator.parent import BaseTranslator
13
  from bs4 import BeautifulSoup
14
  import requests
 
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()
95
+
96
  if response.status_code != 200:
97
  raise RequestError()
98
  soup = BeautifulSoup(response.text, 'html.parser')
deep_translator/mymemory.py CHANGED
@@ -2,9 +2,14 @@
2
  mymemory translator API
3
  """
4
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
5
- from deep_translator.exceptions import NotValidPayload, TranslationNotFound, LanguageNotSupportedException, RequestError
 
 
 
 
6
  from deep_translator.parent import BaseTranslator
7
  import requests
 
8
 
9
 
10
  class MyMemoryTranslator(BaseTranslator):
@@ -87,6 +92,8 @@ class MyMemoryTranslator(BaseTranslator):
87
  params=self._url_params,
88
  headers=self.headers)
89
 
 
 
90
  if response.status_code != 200:
91
  raise RequestError()
92
 
@@ -143,3 +150,19 @@ class MyMemoryTranslator(BaseTranslator):
143
  except Exception as e:
144
  raise e
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  mymemory translator API
3
  """
4
  from deep_translator.constants import BASE_URLS, GOOGLE_LANGUAGES_TO_CODES
5
+ from deep_translator.exceptions import (NotValidPayload,
6
+ TranslationNotFound,
7
+ LanguageNotSupportedException,
8
+ RequestError,
9
+ TooManyRequests)
10
  from deep_translator.parent import BaseTranslator
11
  import requests
12
+ from time import sleep
13
 
14
 
15
  class MyMemoryTranslator(BaseTranslator):
 
92
  params=self._url_params,
93
  headers=self.headers)
94
 
95
+ if response.status_code == 429:
96
+ raise TooManyRequests()
97
  if response.status_code != 200:
98
  raise RequestError()
99
 
 
150
  except Exception as e:
151
  raise e
152
 
153
+ def translate_batch(self, batch=None):
154
+ """
155
+ translate a list of texts
156
+ @param batch: list of texts you want to translate
157
+ @return: list of translations
158
+ """
159
+ if not batch:
160
+ raise Exception("Enter your text list that you want to translate")
161
+
162
+ arr = []
163
+ for text in batch:
164
+ translated = self.translate(text)
165
+ arr.append(translated)
166
+ sleep(2)
167
+
168
+ return arr
deep_translator/pons.py CHANGED
@@ -8,7 +8,8 @@ from deep_translator.exceptions import (LanguageNotSupportedException,
8
  TranslationNotFound,
9
  NotValidPayload,
10
  ElementNotFoundInGetRequest,
11
- RequestError)
 
12
  from deep_translator.parent import BaseTranslator
13
  from requests.utils import requote_uri
14
 
@@ -87,6 +88,9 @@ class PonsTranslator(BaseTranslator):
87
  url = requote_uri(url)
88
  response = requests.get(url)
89
 
 
 
 
90
  if response.status_code != 200:
91
  raise RequestError()
92
 
 
8
  TranslationNotFound,
9
  NotValidPayload,
10
  ElementNotFoundInGetRequest,
11
+ RequestError,
12
+ TooManyRequests)
13
  from deep_translator.parent import BaseTranslator
14
  from requests.utils import requote_uri
15
 
 
88
  url = requote_uri(url)
89
  response = requests.get(url)
90
 
91
+ if response.status_code == 429:
92
+ raise TooManyRequests()
93
+
94
  if response.status_code != 200:
95
  raise RequestError()
96
 
deep_translator/tests/test_google_trans.py CHANGED
@@ -52,3 +52,6 @@ def test_payload(google_translator):
52
 
53
  with pytest.raises(exceptions.NotValidLength):
54
  google_translator.translate("a"*5001)
 
 
 
 
52
 
53
  with pytest.raises(exceptions.NotValidLength):
54
  google_translator.translate("a"*5001)
55
+
56
+ #for _ in range(1):
57
+ #assert google_translator.translate(text='좋은') == "good"
deep_translator/yandex.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  from requests import exceptions
6
  from deep_translator.constants import BASE_URLS
7
  from deep_translator.exceptions import (RequestError,
8
- YandexDefaultException, TranslationNotFound)
9
 
10
 
11
  class YandexTranslator(object):
@@ -95,8 +95,13 @@ def translate(self, text, lang, proxies=None):
95
  raise YandexDefaultException(503)
96
  else:
97
  response = response.json()
 
 
 
 
98
  if response['code'] != 200:
99
  raise YandexDefaultException(response['code'])
 
100
  if not response['text']:
101
  raise TranslationNotFound()
102
 
 
5
  from requests import exceptions
6
  from deep_translator.constants import BASE_URLS
7
  from deep_translator.exceptions import (RequestError,
8
+ YandexDefaultException, TranslationNotFound, TooManyRequests)
9
 
10
 
11
  class YandexTranslator(object):
 
95
  raise YandexDefaultException(503)
96
  else:
97
  response = response.json()
98
+
99
+ if response['code'] == 429:
100
+ raise TooManyRequests()
101
+
102
  if response['code'] != 200:
103
  raise YandexDefaultException(response['code'])
104
+
105
  if not response['text']:
106
  raise TranslationNotFound()
107