Skip to content

Commit

Permalink
Merge pull request #72 from k-aito/add-pydeeplx-support
Browse files Browse the repository at this point in the history
Add support for pydeeplx with random wait and proxy until 10 retry hardcoded
  • Loading branch information
sinedie authored Dec 3, 2023
2 parents 22dff98 + a28b957 commit 56bf1d7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ from srtranslator import SrtFile
from srtranslator.translators.deepl_api import DeeplApi
from srtranslator.translators.deepl_scrap import DeeplTranslator
from srtranslator.translators.translatepy import TranslatePy
from srtranslator.translators.pydeeplx import DeepLX
```

Initialize translator. It can be any translator, even your own, check the docs, there are instructions per translator and how to create your own.

```python
translator = DeeplTranslator() # or TranslatePy() or DeeplApi(api_key)
translator = DeeplTranslator() # or TranslatePy() or DeeplApi(api_key) or DeepLX()
```

Load, translate and save. For multiple recursive files in folder, check `examples folder`
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jinxed==1.2.0
lxml==4.9.3
outcome==1.2.0
pycparser==2.21
PyDeepLX==1.0.4
PySocks==1.7.1
python-editor==1.0.4
pyuseragents==1.0.5
Expand Down
4 changes: 3 additions & 1 deletion srtranslator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .translators.deepl_api import DeeplApi
from .translators.deepl_scrap import DeeplTranslator
from .translators.translatepy import TranslatePy
from .translators.pydeeplx import DeepLX

parser = argparse.ArgumentParser(description="Translate an .STR file")

Expand Down Expand Up @@ -71,7 +72,7 @@
"-t",
"--translator",
type=str,
choices=["deepl-scrap", "translatepy", "deepl-api"],
choices=["deepl-scrap", "translatepy", "deepl-api", "deeplx"],
help="Built-in translator to use",
default="deepl-scrap",
)
Expand All @@ -86,6 +87,7 @@
"deepl-scrap": DeeplTranslator,
"deepl-api": DeeplApi,
"translatepy": TranslatePy,
"deeplx": DeepLX,
}

args = parser.parse_args()
Expand Down
52 changes: 52 additions & 0 deletions srtranslator/translators/pydeeplx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from PyDeepLX import PyDeepLX
from random import randint
from time import sleep

from .base import Translator as BaseTranslator
from fp.fp import FreeProxy
from .selenium_utils import (
create_proxy,
)


class DeepLX(BaseTranslator):
max_char = 1500

def __init__(self):
self.proxies = None

def translate(self, text, source_language, destination_language):
# Sleep a random number of seconds (between 3 and 5)
# https://www.shellhacks.com/python-sleep-random-time-web-scraping/
RANDOM_WAIT = randint(3,5)
print(f"...... Wait randomly {RANDOM_WAIT}s")
sleep(RANDOM_WAIT)

# Max retry 3
RETRY_COUNTER = 10
result = None
while RETRY_COUNTER > 0 :
try:
result = PyDeepLX.translate(
text,
source_language,
destination_language,
proxies=self.proxies
)

if result == None:
print("...... Exception: result is empty raise exception")
raise Exception("Result is empty")

# Everyting alright
break
except Exception as e:
print(f"...... Exception {e} with retry number {RETRY_COUNTER}")

# Get a random proxy
print("...... Use or change proxy")
self.proxies = FreeProxy(rand=True, timeout=1).get()

# Decrease RETRY_COUNTER
RETRY_COUNTER -= 1
return result

0 comments on commit 56bf1d7

Please sign in to comment.