ssut/py-googletrans

JSONDecodeError while translating

xLinkOut opened this issue · 2 comments

Googletrans version:

  • 4.0.0rc1
  • 3.1.0a0
  • 3.0.0
  • 2.x

I'm submitting a ...

  • bug report
  • feature request

Current behavior:
During translation, a JSON decode error occured.

Expected behavior:
Translate and correctly return the translated text.

Steps to reproduce:
It randomly happear during a translation loop. Retrying the same loop, it doesn't happend.

Related code:
File: googletrans/client.py:198
data = json.loads(resp), where resp seems to be None.

Other information:

Traceback (most recent call last):
  File "translate.py", line 95, in <module>
    output[lang][page_name][key] = translator.translate(value, src='en', dest=lang).text
  File "[redacted]/venv/lib/python3.8/site-packages/googletrans/client.py", line 218, in translate
    data = json.loads(resp)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The reason the resp is None could be the Google API returns 400 or 404 for translator's requests (which was in my case).
I was initializing Translator instance as:
Translator(service_urls=['translate.googleapis.com'])
which was casing the requests to fail with 404. Changed to:
Translator(service_urls=['translate.google.com'])
and it fixed the problem for me.

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.