From c18bd8e671b01b91dfa8e7c486affab4e2f0c006 Mon Sep 17 00:00:00 2001 From: EAR Date: Sun, 3 Dec 2023 11:08:12 -0500 Subject: [PATCH] Retry on getting proxy exception --- srtranslator/translators/selenium_utils.py | 32 ++++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/srtranslator/translators/selenium_utils.py b/srtranslator/translators/selenium_utils.py index 1d8ec0b..7e3475b 100644 --- a/srtranslator/translators/selenium_utils.py +++ b/srtranslator/translators/selenium_utils.py @@ -23,18 +23,26 @@ def create_proxy(country_id: Optional[List[str]] = ["US"]) -> Proxy: Returns: Proxy: Selenium WebDriver proxy """ - logging.info("Getting a new Proxy from https://www.sslproxies.org/") - proxy = FreeProxy(country_id=country_id).get() - proxy = Proxy( - dict( - proxyType=ProxyType.MANUAL, - httpProxy=proxy, - ftpProxy=proxy, - sslProxy=proxy, - noProxy="", - ) - ) - return proxy + i = 0 + while i < 3: + try: + logging.info("Getting a new Proxy from https://www.sslproxies.org/") + proxy = FreeProxy(country_id=country_id).get() + proxy = Proxy( + dict( + proxyType=ProxyType.MANUAL, + httpProxy=proxy, + ftpProxy=proxy, + sslProxy=proxy, + noProxy="", + ) + ) + return proxy + except: + logging.info("Exception while getting Proxy. Trying again") + i += 1 + + raise Exception("Unable to get proxy") def create_driver(proxy: Optional[Proxy] = None) -> WebDriver: