Skip to content

Commit

Permalink
Merge pull request #51 from sinedie/rotate_proxy_on_fail
Browse files Browse the repository at this point in the history
Rotate proxy when translation fail one time
  • Loading branch information
sinedie authored Jun 20, 2023
2 parents 6faaa6c + d333fce commit 6c66ae8
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions srtranslator/translators/deepl_scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 6c66ae8

Please sign in to comment.