Skip to content

Commit

Permalink
Enable proxy country choice
Browse files Browse the repository at this point in the history
  • Loading branch information
sinedie committed May 4, 2023
1 parent 92eaf36 commit 1eee329
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
20 changes: 20 additions & 0 deletions examples/custom_proxy.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
license="FREE",
Expand Down
2 changes: 0 additions & 2 deletions srtranslator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@
help="Api key if needed on translator",
)

# TODO add custom proxy arg

builtin_translators = {
"deepl-scrap": DeeplTranslator,
"deepl-api": DeeplApi,
Expand Down
9 changes: 6 additions & 3 deletions srtranslator/translators/selenium_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 1eee329

Please sign in to comment.