diff --git a/srtranslator/translators/deepl_scrap.py b/srtranslator/translators/deepl_scrap.py index 33ea351..592b873 100644 --- a/srtranslator/translators/deepl_scrap.py +++ b/srtranslator/translators/deepl_scrap.py @@ -55,11 +55,16 @@ class DeeplTranslator(Translator): } def __init__(self, driver: Optional[WebDriver] = None): + self.last_translation_failed = False self.driver = driver + if self.driver is None: - proxy = create_proxy() - self.driver = create_driver(proxy) + self._rotate_proxy() + return + + self._reset() + def _reset(self): logging.info(f"Going to {self.url}") self.driver.get(self.url) self._closePopUp() @@ -74,6 +79,16 @@ def __init__(self, driver: Optional[WebDriver] = None): self.src_lang = None self.target_lang = None + def _rotate_proxy(self): + if self.driver is not None: + logging.info(" ======= Translation failed. Probably got banned. ======= ") + logging.info("Rotating proxy") + self.quit() + + proxy = create_proxy() + self.driver = create_driver(proxy) + self._reset() + def _closePopUp(self): Button( self.driver, @@ -130,9 +145,18 @@ def translate(self, text: str, source_language: str, destination_language: str): if self._is_translated(clean_text, translation): time.sleep(2) translation = self.input_destination_language.value + + # Reset the proxy flag + self.last_translation_failed = False return translation.replace("@[.]@", "[...]") time.sleep(1) + # Maybe proxy got banned, so we try with a new proxy, but just once. + if not self.last_translation_failed: + self.last_translation_failed = True + self._rotate_proxy() + return self.translate(text, source_language, destination_language) + self.quit() raise TimeOutException("Translation timed out")