Imgur / imgurpython

Official Imgur python client library (deprecated)
MIT License
546 stars 116 forks source link

raise ImgurClientError('JSON decoding of response failed.') imgurpython.helpers.error.ImgurClientError: JSON decoding of response failed. #74

Open monajalal opened 8 years ago

monajalal commented 8 years ago

What should I do when I get the following error in the middle of my running code? What's the cause for it? http://stackoverflow.com/questions/40099046/raise-imgurclienterrorjson-decoding-of-response-failed-imgurpython-helpers

Traceback (most recent call last):
  File "download.py", line 14, in <module>
    search = imgur_client.gallery_search('cat', window='all', sort='time', page=i)
  File "/usr/local/lib/python2.7/dist-packages/imgurpython/client.py", line 531, in gallery_search
    response = self.make_request('GET', 'gallery/search/%s/%s/%s' % (sort, window, page), data)
  File "/usr/local/lib/python2.7/dist-packages/imgurpython/client.py", line 158, in make_request
    raise ImgurClientError('JSON decoding of response failed.')
imgurpython.helpers.error.ImgurClientError: JSON decoding of response failed.

My code is:

 12 imgur_client = ImgurClient(client_id, client_secret, access_token, refresh_token)
 13 for p in range(100, 500):
 14     search = imgur_client.gallery_search('cat', window='all', sort='time', page=p)
 15     for i in range(0, len(search)):
 16         #print(search[i].link)
 17         #print(dir(search[i]))
 18         print(search[i].type)
 19         if search[i].comment_count > 30 and search[i].type!='gif':
 20             count = 0
 21             image_file = urllib2.urlopen(search[i].link, timeout = 5)
 22             image_file_name = 'images/'+ search[i].id+search[i].type
 23             output_image = open(image_file_name, 'wb')
 24             output_image.write(image_file.read())
 25 
 26             for post in imgur_client.gallery_item_comments(search[i].id, sort='best'):
 27                 if count <= 30:
 28                     count += 1
Backscratcher commented 8 years ago
  1. I think there is a problem with Your image_file_name, try with something like this: image_file_name = 'images/{0}.{1}'.format(search[i].id, type) <- but still you need to parse type because it comes in format like "image/gif"
  2. It is not the only issue, I ran your code and it will fail on objects like GalleryAlbum - because it has no type. wrap your code with try/except like: try: your code here except Exception: pass or make an if for it handle this situation
monajalal commented 8 years ago

I get similar error here: http://stackoverflow.com/questions/40115380/imgurpython-helpers-error-imgurclientratelimiterror-rate-limit-exceeded The previous one was fixed

On Tue, Oct 18, 2016 at 7:24 AM, Backscratcher notifications@github.com wrote:

  1. I think there is a problem with Your image_file_name, try with something like this: image_file_name = 'images/{0}.{1}'.format(search[i].id, type) <- but still you need to parse type because it comes in format like "image/gif"
  2. It is not the only issue, I ran your code and it will fail on objects like GalleryAlbum - because it has no type. wrap your code with try/except like: try: your code here except Exception: pass or make an if for it handle this situation

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Imgur/imgurpython/issues/74#issuecomment-254491420, or mute the thread https://github.com/notifications/unsubscribe-auth/ABziNVsG6i3Oy7mvipNhrPVjA2nIOV4rks5q1LqWgaJpZM4KZVir .