ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.

Home Page:http://py-googletrans.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

raise JSONDecodeError("Expecting value", s, err.value) from None

raomanohar opened this issue · comments

I am getting the below error when I try to translate a Portuguese tweet to English.
"raise JSONDecodeError("Expecting value", s, err.value) from None"

I have tried to encode the string, tried to limit the string to only 3000 characters nothing works.
Please advise how can i fix this?
The issues which were raised by others and the solutions provided are not working for me

Below is my code.

from googletrans import Translator

translator = Translator()
d = 'Eu fujo de discussão na internet como o diabo foge da cruz,  leio e expresso minha opinião p mim mesmo pq todos tão afim de lacrar além de ouvir e trocar idéias'
d = d.encode('utf-8')  
d = str(d)
translations = translator.translate(d, dest='en')
print(translations.origin, '\n -> \n', translations.text)

Error

Traceback (most recent call last):
  File "C:\Users\Stramzik\Desktop\SentizAnalyzer\jsf.py", line 8, in <module>
    translations = translator.translate(d, dest='en')
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 172, in translate
    data = self._translate(text, dest, src)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\client.py", line 81, in _translate
    data = utils.format_json(r.text)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\utils.py", line 62, in format_json
    converted = legacy_format_json(original)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\site-packages\googletrans\utils.py", line 54, in legacy_format_json
    converted = json.loads(text)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\lib\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)
[Finished in 4.3s with exit code 1]
[shell_cmd: python -u "C:\Users\Stramzik\Desktop\SentizAnalyzer\jsf.py"]
[dir: C:\Users\Stramzik\Desktop\SentizAnalyzer]
[path: C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Webex\Webex\Applications;C:\Program Files\IBM\SPSS\Modeler\18.1.1\ext\bin\spss.TMWBServer\bin;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37;C:\Users\Stramzik\AppData\Local\Programs\Python\Python37\Scripts;C:\Users\Stramzik\AppData\Roaming\nltk_data;C:\Users\Stramzik\AppData\Local\Microsoft\WindowsApps;C:\Users\Stramzik\AppData\Local\Box\Box Edit\]

try removing the "d = d.encode('utf-8') "

If I wont encode and if the tweet contains an emoji it Throws JSONDecodeError error.

oh, is that how you have the code exactly in the file?

Yes, Well Its so weird but it works for some time and it stops all of a sudden.
Right now the above code is running with no error. But when I submitted the issue yesterday it was throwing the above error.
I think the API wasblocking my IP from accessing it. Please assist me in bypass that?

Yeah it might of ip banned you temporary.
Could try this aswell:

def translate(trans):
    try:
        trans = raw_input("Enter word/words here: ")
        translations = translator.translate([trans], dest='en')
        for translation in translations:
            print(translation.origin, ' English -> ', translation.text)
    except:
        try:
            trans_encode = trans.encode("ascii", "ignore")
            translations = translator.translate([trans_encode], dest='en')
            for translation in translations:
                print(translation.origin, ' English -> ', translation.text)
        except:
            pass

trans = ''
threading.Thread(translate(trans)).start()

This is what I had to do to bypass their API call restriction... I use a VPN, specifically Nord-Vpn, so to do it the way I did you would need to be able to connect/disconnect from/to a VPN through the terminal...

    def translate_text(text, dest_language="en"):
        # Used to translate using the googletrans library
        import json
        translator = googletrans.Translator()
        try:
            translation = translator.translate(text=text, dest=dest_language)
        except json.decoder.JSONDecodeError:
            # api call restriction
            process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
            process.wait()
            process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
            process.wait()
            return Process_Data.translate_text(text=text, dest_language=dest_language)
        return translation

This is what I had to do to bypass their API call restriction... I use a VPN, specifically Nord-Vpn, so to do it the way I did you would need to be able to connect/disconnect from/to a VPN through the terminal...

    def translate_text(text, dest_language="en"):
        # Used to translate using the googletrans library
        import json
        translator = googletrans.Translator()
        try:
            translation = translator.translate(text=text, dest=dest_language)
        except json.decoder.JSONDecodeError:
            # api call restriction
            process = subprocess.Popen(["nordvpn", "d"], stdout=subprocess.PIPE)
            process.wait()
            process = subprocess.Popen(["nordvpn", "c", "canada"], stdout=subprocess.PIPE)
            process.wait()
            return Process_Data.translate_text(text=text, dest_language=dest_language)
        return translation

I did import "subprocess" but how would "Process_Data" work? as is I tried pip install Process_Data but didn't think it worked

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.