-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from k-aito/add-pydeeplx-support
Add support for pydeeplx with random wait and proxy until 10 retry hardcoded
- Loading branch information
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |