lushan88a / google_trans_new

A free and unlimited python API for google translate.
MIT License
391 stars 171 forks source link

PyPi release is outdated which causes JSON decode error on Google Colab #45

Open hugocool opened 2 years ago

hugocool commented 2 years ago

Google trans new does not work in Google Colab when installed through pypi

!pip install google_trans_new

input:

from google_trans_new import google_translator  

translator = google_translator()  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')  
print(translate_text)

error:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-7-a6eb592f82a8> in <module>()
      2 
      3 translator = google_translator()
----> 4 translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')
      5 print(translate_text)
      6 

3 frames
/usr/local/lib/python3.7/dist-packages/google_trans_new/google_trans_new.py in translate(self, text, lang_tgt, lang_src, pronounce)
    186                                 return [sentences, pronounce_src, pronounce_tgt]
    187                     except Exception as e:
--> 188                         raise e
    189             r.raise_for_status()
    190         except requests.exceptions.ConnectTimeout as e:

/usr/local/lib/python3.7/dist-packages/google_trans_new/google_trans_new.py in translate(self, text, lang_tgt, lang_src, pronounce)
    150                     try:
    151                         response = (decoded_line + ']')
--> 152                         response = json.loads(response)
    153                         response = list(response)
    154                         response = json.loads(response[0][2])

/usr/lib/python3.7/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

/usr/lib/python3.7/json/decoder.py in decode(self, s, _w)
    338         end = _w(s, end).end()
    339         if end != len(s):
--> 340             raise JSONDecodeError("Extra data", s, end)
    341         return obj
    342 

JSONDecodeError: Extra data: line 1 column 339 (char 338)

I think the package is not updated on pypi, the latest release dates from dec 2020, while your latest commit is from 17 days ago.

This can be temporarily solved by doing

!git clone https://github.com/lushan88a/google_trans_new.git
from google_trans_new.google_trans_new import google_translator  

translator = google_translator(url_suffix='nl',timeout=9)  
translate_text = translator.translate('สวัสดีจีน',lang_tgt='en')  
print(translate_text)
>>> Hello Chinese

A new pypi release might fix all the issues

ghost commented 2 years ago

This error came after cloning: google_new_transError: Failed to connect. Probable cause: timeout.

sahilkhose commented 2 years ago

Thanks a lot!

talpay commented 2 years ago

Ok half of the issues are now just people asking for a new PyPi release.

TsunayoshiSawada commented 2 years ago

This is still a thing and its an annoying thing

fire17 commented 2 years ago

since they are not updating.. i cloned locally and extracted google_trans_new.py and constant.py to my project folder

changed line 8 of google_trans_new.py from from .constant import LANGUAGES, DEFAULT_SERVICE_URLS to from constant import LANGUAGES, DEFAULT_SERVICE_URLS

removed _google_trans_new_ from requirements.txt

and so my original import from code can stay the same.. from google_trans_new import google_translator

working on heroku now :)

aranjan-covanta commented 2 years ago

removed google_trans_new from requirements.txt

Can you tell me where the requirements.txt file is? I am unable to locate it when I cloned the repo (it isn't in the file list).

fire17 commented 2 years ago

removed google_trans_new from requirements.txt

Can you tell me where the requirements.txt file is? I am unable to locate it when I cloned the repo (it isn't in the file list).

requirements.txt is a file i have in my repo, heroku pip installs all of them prior to running my code if you dont have it, it seems you're not using external python packages on heroku anyhow therefore putting google_trans_new.py and constant.py in your project folder will let you use the local version when importing