diff --git a/examples/custom_proxy.py b/examples/custom_proxy.py new file mode 100644 index 0000000..6c730ba --- /dev/null +++ b/examples/custom_proxy.py @@ -0,0 +1,20 @@ +import os +import glob + +from srtranslator import SrtFile +from srtranslator.translators.deepl_scrap import DeeplTranslator +from srtranslator.translators.selenium_utils import create_proxy, create_driver + +folder = "srt_test/" +for filepath in glob.glob(os.path.join(folder, "**/*.srt"), recursive=True): + # The country ids are the ones in https://www.sslproxies.org/ + proxy = create_proxy(country_id=["US", "GB"]) + driver = create_driver(proxy) + translator = DeeplTranslator(driver) + + srt = SrtFile(filepath) + srt.translate(translator, "en", "es") + srt.wrap_lines() + srt.save(f"{os.path.splitext(filepath)[0]}_translated.srt") + + translator.quit() diff --git a/setup.py b/setup.py index 2513ad2..23bdade 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ long_description=Path("README.md").read_text(encoding="utf-8"), long_description_content_type="text/markdown", url="https://github.com/sinedie/SRTranslator", - version="0.2.3", + version="0.2.4", author="EAR", author_email="sinedie@protonmail.com", license="FREE", diff --git a/srtranslator/__main__.py b/srtranslator/__main__.py index fcbb51f..71ef6f8 100644 --- a/srtranslator/__main__.py +++ b/srtranslator/__main__.py @@ -81,8 +81,6 @@ help="Api key if needed on translator", ) -# TODO add custom proxy arg - builtin_translators = { "deepl-scrap": DeeplTranslator, "deepl-api": DeeplApi, diff --git a/srtranslator/translators/selenium_utils.py b/srtranslator/translators/selenium_utils.py index 11739d5..1d8ec0b 100644 --- a/srtranslator/translators/selenium_utils.py +++ b/srtranslator/translators/selenium_utils.py @@ -1,7 +1,7 @@ import sys import logging -from typing import Optional +from typing import Optional, List from fp.fp import FreeProxy from selenium import webdriver from webdriverdownloader import GeckoDriverDownloader @@ -14,14 +14,17 @@ from selenium.webdriver.support import expected_conditions as EC -def create_proxy() -> Proxy: +def create_proxy(country_id: Optional[List[str]] = ["US"]) -> Proxy: """Creates a new proxy to use with a selenium driver and avoid get banned + Args: + country_id (Optional[List[str]], optional): Contry id to create proxy. Defaults to ['US']. + Returns: Proxy: Selenium WebDriver proxy """ logging.info("Getting a new Proxy from https://www.sslproxies.org/") - proxy = FreeProxy().get() + proxy = FreeProxy(country_id=country_id).get() proxy = Proxy( dict( proxyType=ProxyType.MANUAL,