sleepyfran / itunespy

:snake: A simple library to fetch data from the iTunes Store API made for Python >= 3.5
MIT License
64 stars 6 forks source link

JSONDecodeError: Expecting value: line 1 column 1 (char 0) #16

Closed miguelssimao closed 2 months ago

miguelssimao commented 2 months ago

Occasionally, lookup() throws me json.decoder.JSONDecodeError and requests.exceptions.JSONDecodeError exceptions. I believe this happens because the server did not respond to my request and so there is no data to decode.

Here is the full log:

Exception in thread Thread-1 (icy_meta):
Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\requests\models.py", line 963, in json
    return complexjson.loads(self.content.decode(encoding), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\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)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\Python311\Lib\threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "c:\Users\migl\streamplayer\gui.py", line 130, in icy_meta
    meta = itunes.get_meta(current)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\migl\streamplayer\lib\itunes.py", line 64, in get_meta
    lookup = itunespy.lookup(id=track[0].artist_id, limit=1)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\itunespy\__init__.py", line 106, in lookup
    json = r.json()['results']
           ^^^^^^^^
  File "C:\Python311\Lib\site-packages\requests\models.py", line 971, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Would it be possible for you to handle these exceptions in the module?

pfouque commented 2 months ago

In such cases, when iTunes itself doesn't answer a proper JSON response, what behavior do you expect? We could raise a custom Exception, or you can simply catch this exception in your code and decide to retry.

NB: It would be interesting to see the raw response when it happens. NB2: If you're intensively calling itunes it may be possible they raise a rate limit error

miguelssimao commented 2 months ago

In such cases, when iTunes itself doesn't answer a proper JSON response, what behavior do you expect? We could raise a custom Exception, or you can simply catch this exception in your code and decide to retry.

NB: It would be interesting to see the raw response when it happens. NB2: If you're intensively calling itunes it may be possible they raise a rate limit error

Not even necessarily a custom exception, I think it could raise a LookupError as well.

Yes, iTunes is being called everytime the metadata from the stream changes, so I was expecting this scenario to happen due to the frequent number of calls.

sleepyfran commented 2 months ago

Thanks for raising this, I think it definitely makes sense to handle this in itunespy itself, since manually catching this exception would require you to couple yourself with specific errors from the JSON parser library itunespy uses, which is never a good thing :^)

I think it makes sense to catch it inside of the library and expose some LookupError with details about what happened. It's been a while since I worked on the library, I might take a stab at it whenever I get some time, but feel free to create a PR if you feel like it @miguelssimao!

sleepyfran commented 2 months ago

Hey folks, just for awareness I've just published version 1.6.1 with this fix. Happy coding :^) https://pypi.org/project/itunespy/1.6.1/

Thanks @pfouque for addressing this 🙇🏻