Search code examples
pythonjsonpython-2.7google-translate

python googletrans library - No JSON object could be decoded


I'm creating a project that will help users to translate files from any language to English. I am using python 2.7 and the googletrans library.

google has a limitation allowing only 15k characters be translated at a time, so when a user enter a path to a file larger then 15k, the program will split it into a few files, run requests to translate each file separately, save the translated string from each file and save it in a new file (translated file).

Here is the section where the program decides if the file is large enough to split it and what to do (if else):

file = open(path, "r")
string = file.read()
if len(string) > 15000: #15k maximum google server can translate
    file.close()
    devideFile(path)
    print "Large file, please wait.."
    output = translate_all(path)
    combineFile(path)
else:
    file.close()
    try:
        output = trans.translate(string).text
    except requests.exceptions.ConnectionError:
        print "Connection refused"
        print "You might not have internet connection"
        print "Or there's a problem with google translate server."

Here is the translation part of translate_all function:

while True: #run as long as there are files left.
    try:
        temp = open(path + "(" + str(i) + ")" + "." + file_type, "r") #file(1).txt 
        string = temp.read() #for code readability
        try:
            output += (trans.translate(string).text) #for code readability (temp.read() => string)
        except requests.exceptions.ConnectionError:
            print "Connection refused"
            print "You might not have internet connection"
            print "Or there's a problem with google translate server."
        temp.close()
        i += 1
    except IOError:
        break
    sleep(1) #just so google wouldn't block client.
return output

The thing is, translating a small file works perfectly but when i try to translate a file that is bigger then 15k characters this is the output:

Traceback (most recent call last):
File "translate.py", line 64, in <module>
main()
File "translate.py", line 47, in main
output = translate_all(path)
File "translate.py", line 23, in translate_all
output += (trans.translate(string).text) #for code readability (temp.read() => string)
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 132, in translate
data = self._translate(text, dest, src)
File "/usr/local/lib/python2.7/dist-packages/googletrans/client.py", line 63, in _translate
data = utils.format_json(r.text)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 62, in format_json
converted = legacy_format_json(original)
File "/usr/local/lib/python2.7/dist-packages/googletrans/utils.py", line 54, in legacy_format_json
converted = json.loads(text)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

I know its alot, please help :) my project on github: https://github.com/sisitrs2/fileTranslate


Solution

  • Apparently the letters limit was 5k. change if len(string) > 15000: to if len(string) > 5000 (less if you use unicode).