SRT-DeepL-Translator icon indicating copy to clipboard operation
SRT-DeepL-Translator copied to clipboard

Doesn't work at all just times out

Open tssajo opened this issue 2 years ago • 6 comments

Hi, Trying to translate from 'en' to 'hu' but it does not work at all. I tried it on Windows and Ubuntu and the same thing happened. See:

# python3 -m srt_deepl -i en -o hu -v -vv /t/srt
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.8/dist-packages/srt_deepl/__main__.py", line 112, in <module>
    translate(
  File "/usr/local/lib/python3.8/dist-packages/srt_deepl/main.py", line 84, in translate
    proxies = get_proxies()
  File "/usr/local/lib/python3.8/dist-packages/srt_deepl/utils.py", line 10, in get_proxies
    table_rows = Text(driver, "XPATH", "//tr[@role='row']", multiple=True).text
  File "/usr/local/lib/python3.8/dist-packages/srt_deepl/elements.py", line 10, in __init__
    WebDriverWait(driver, 100).until(lambda driver: find_element(*locator))
  File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

I get the exact same error message on Windows.

Please advise what to do. Thanks!

tssajo avatar Apr 27 '22 13:04 tssajo

Ok, so, the trouble is when it want to get the proxy in https://free-proxy-list.net/. It has to wait until the proxy's table shows up to get the one we want... We can't wait too long cause bad UX. I set the maximum wait time to 100s. Maybe it is too little for bad internet connection? idk. I should do a better error handling with this.

Right now im at work (yeah, stealing time XD), but I going to check it out in deep in the weekend. In the mean time, you could try changing the wait time in the next line and see if it works https://github.com/sinedie/SRT-DeepL-Translator/blob/b29d0c09a9b4a75bb060e80f9b7c3421b4960124/srt_deepl/elements.py#L10

Hope it does :D

sinedie avatar Apr 27 '22 15:04 sinedie

@sinedie Hey, thanks for the quick reply! I managed to make it work however a totally different way. I am using tor service now. Here is what I did: First, I tried to use what you have under examples/tor_driver.py however that did not work at all under Windows. I ran into this issue: https://stackoverflow.com/questions/56052021/which-is-the-valid-firefox-binary-path-for-python-tbselenium and it is stated there that Windows is not supported. I also have a Linux box (Ubuntu) but that is a headless server with no GUI just CLI, and I didn't want to mess up my Linux server by experimenting there... Instead, as suggested in the above link, now I run the tor service on port 9050 on my Windows PC. Instructions on how to do this can be found here: https://deepdarkweb.github.io/how-to-install-tor-on-windows-without-the-tor-browser-running-tutorial/ Once the tor service (not the tor browser!) is up and running in the background, I can use that service with your program like this:

from selenium import webdriver
from srt_deepl import translate

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "127.0.0.1")
profile.set_preference("network.proxy.socks_port", 9050)
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

translate(
    "D:\\srt", # directory where the srt files are
    "en", # original language
    "hu", # desired language
    wrap_limit=50,
    delete_old=False,
    driver=driver,
)

This works. However, since the translated text has UTF8 characters in it, your script as it is could not save the translated srt file! To fix this bug I had to patch your srt_parser.py file and change a line (near the very bottom of the file) from this: with open(f"{file_name}_{lang_to}.srt", "w") as file_out: to this: with open(f"{file_name}_{lang_to}.srt", "w", encoding="utf-8") as file_out: (This solution was suggested here: https://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters ) This is a must when using Python 3 in Windows at least when the resulted translation have one or more UTF8 characters.

tssajo avatar Apr 27 '22 15:04 tssajo

Thats beautiful, I'm glad it worked. I will make those changes (at least the utf-8 issue) as soon as possible. Also, feel free to PR if u want. Thanks for notice this :D

sinedie avatar Apr 27 '22 16:04 sinedie

Yeah, I won't create a PR. I leave the patching to you. ;)

But I think this needs to be added to open_srt function too:

with open(file_path, "r", encoding="utf-8", errors="ignore") as srt_file:

otherwise if the original srt file has some UTF8 characters in it (or a BOM at the beginning of the file!) then it won't open.

tssajo avatar Apr 27 '22 17:04 tssajo

In PR #22, getting the proxies is improved, pls wait till test come and can merge... Any way you solved with tor (ty for that)

In the next days im comming back to mantain this.

Also, as an enhance, you will be able use any traslator you want (not only deepl and even apis if u want). Keep watching for new features

sinedie avatar Jul 21 '22 15:07 sinedie

@sinedie Hey, thanks for the quick reply! I managed to make it work however a totally different way. I am using tor service now. Here is what I did: First, I tried to use what you have under examples/tor_driver.py however that did not work at all under Windows. I ran into this issue: https://stackoverflow.com/questions/56052021/which-is-the-valid-firefox-binary-path-for-python-tbselenium and it is stated there that Windows is not supported. I also have a Linux box (Ubuntu) but that is a headless server with no GUI just CLI, and I didn't want to mess up my Linux server by experimenting there... Instead, as suggested in the above link, now I run the tor service on port 9050 on my Windows PC. Instructions on how to do this can be found here: https://deepdarkweb.github.io/how-to-install-tor-on-windows-without-the-tor-browser-running-tutorial/ Once the tor service (not the tor browser!) is up and running in the background, I can use that service with your program like this:

from selenium import webdriver
from srt_deepl import translate

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "127.0.0.1")
profile.set_preference("network.proxy.socks_port", 9050)
profile.set_preference("network.proxy.socks_version", 5)
profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=profile)

translate(
    "D:\\srt", # directory where the srt files are
    "en", # original language
    "hu", # desired language
    wrap_limit=50,
    delete_old=False,
    driver=driver,
)

This works. However, since the translated text has UTF8 characters in it, your script as it is could not save the translated srt file! To fix this bug I had to patch your srt_parser.py file and change a line (near the very bottom of the file) from this: with open(f"{file_name}_{lang_to}.srt", "w") as file_out: to this: with open(f"{file_name}_{lang_to}.srt", "w", encoding="utf-8") as file_out: (This solution was suggested here: https://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters ) This is a must when using Python 3 in Windows at least when the resulted translation have one or more UTF8 characters.

I had the exact same issues. Thanks a lot for your sharing your excellent solution 🙏

devjoda avatar Aug 16 '22 01:08 devjoda