salvoventura / pyunsplash

An open source Python wrapper for the unsplash.com REST API
MIT License
52 stars 16 forks source link

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) #12

Open helpsocialbotspy opened 4 years ago

helpsocialbotspy commented 4 years ago
import logging
from pyunsplash import PyUnsplash

api_key = 'MY_APP_ACCESS_KEY'
pu = PyUnsplash(api_key=api_key)
logging.getLogger("pyunsplash").setLevel(logging.DEBUG)

search = pu.search(type_='photos', query='red,car')
for photo in search.entries:
    print(photo.id, photo.link_download)

Traceback (most recent call last): File "unsplashdownload.py", line 24, in search = pu.search(type='photos', query='red,car') File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/pyunsplash.py", line 58, in search return Search(api_key=self._apikey, where=type, kwargs) File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/search.py", line 29, in init super(Search, self).init(url=url, api_key=api_key, valid_options=valid_options, kwargs) File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/unpage.py", line 47, in init r = self._agent.get(self.url, self.query_parameters) File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/rest.py", line 84, in get self._body = _r.json() File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/requests/models.py", line 858, in json self.content.decode(encoding), **kwargs File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/json/init.py", line 348, in loads return _default_decoder.decode(s) File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/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)

helpsocialbotspy commented 4 years ago

Solved it myself. I downgraded python from 3.7.0 to 3.6.0 to resolve the issue. Please update the documentation to clearly call out which python versions are supported. Keeping the issue open till the documentation is updated.

salvoventura commented 4 years ago

Hi, thanks for reporting this.

The support matrix includes Py2.7x, and Py3.5, Py3.6, Py3.7, and Py3.8: they are tested in all regressions.

I can't reproduce that error with that code on Python 3.7: it doesn't look like something related to that particular Python version, unless you have an uncommon Json library, which I doubt.

Can you reproduce consistently? If you can, could you run this code:

import logging
from pyunsplash import PyUnsplash

# Initialize app logging
logger = logging.getLogger()
logging.basicConfig(filename='app.log', level=logging.DEBUG)

api_key = '<YOUR_API_KEY>'
pu = PyUnsplash(api_key=api_key)
logging.getLogger("pyunsplash").setLevel(logging.DEBUG)

search = pu.search(type_='photos', query='red,car')
for photo in search.entries:
    print(photo.id, photo.link_download)`

and upload the app.log logfile? I'd like to take a look.

Cheers,

salvoventura commented 4 years ago

After further investigation, I cannot reproduce, and the issue seems more related to an invalid body. I am going to close this now. Feel free to reopen if you can provide the requested logs.

PureDreamer commented 3 years ago

app.log i have the same error

salvoventura commented 3 years ago

@PureDreamer Thank you for reporting this, and including the logs. The issues is that your program has exceeded the [https://unsplash.com/documentation#rate-limiting](Unsplash Rate Limiting) threshold. For that reason, the library received an error 403.

Unfortunately, Unsplash API also returns a body which is not JSON, hence when PyUnplash tries to decode it as json, an exception is raised.

The only improvement I will add here is a custom exception with a better description for specific error cases (like this one, and more in the future if we discover new).

However, from your point of view, you should implement the following:

  1. Wrap your PyUnsplash GET requests within a try... except... block
  2. Decide how to handle exceptions and retries with timeout

Let me know if you have questions.