freesound-python icon indicating copy to clipboard operation
freesound-python copied to clipboard

HTTP Error 429: Unknown Status Code

Open justinsalamon opened this issue 6 years ago • 1 comments

Trying to retrieve metadata (in particular, the license) for multiple sounds on FreeSound, using the Python API with python 3.

Working example:

import freesound
client = freesound.FreesoundClient()
client.set_token(MYTOKEN, "token")
sound = client.get_sound(100852)

Error:

---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/dev/miniconda3/envs/py35/lib/python3.5/site-packages/freesound.py in request(cls, uri, params, client, wrapper, method, data)
    241         try:
--> 242             f = urlopen(req)
    243         except HTTPError as e:

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    162         opener = _opener
--> 163     return opener.open(url, data, timeout)
    164 

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in open(self, fullurl, data, timeout)
    471             meth = getattr(processor, meth_name)
--> 472             response = meth(req, response)
    473 

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in http_response(self, request, response)
    581             response = self.parent.error(
--> 582                 'http', request, response, code, msg, hdrs)
    583 

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in error(self, proto, *args)
    509             args = (dict, 'default', 'http_error_default') + orig_args
--> 510             return self._call_chain(*args)
    511 

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    443             func = getattr(handler, meth_name)
--> 444             result = func(*args)
    445             if result is not None:

~/dev/miniconda3/envs/py35/lib/python3.5/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    589     def http_error_default(self, req, fp, code, msg, hdrs):
--> 590         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    591 

HTTPError: HTTP Error 429: Unknown Status Code

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-7-6d65a93aff27> in <module>()
----> 1 sound = client.get_sound(100852)

~/dev/miniconda3/envs/py35/lib/python3.5/site-packages/freesound.py in get_sound(self, sound_id, **params)
     88         """
     89         uri = URIS.uri(URIS.SOUND, sound_id)
---> 90         return FSRequest.request(uri, params, self, Sound)
     91 
     92     def text_search(self, **params):

~/dev/miniconda3/envs/py35/lib/python3.5/site-packages/freesound.py in request(cls, uri, params, client, wrapper, method, data)
    246                 return resp
    247             else:
--> 248                 raise FreesoundException(e.code, json.loads(resp))
    249         if py3:
    250             resp = f.read().decode("utf-8")

~/dev/miniconda3/envs/py35/lib/python3.5/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    310     if not isinstance(s, str):
    311         raise TypeError('the JSON object must be str, not {!r}'.format(
--> 312                             s.__class__.__name__))
    313     if s.startswith(u'\ufeff'):
    314         raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)",

TypeError: the JSON object must be str, not 'bytes'

Note: the code worked fine earlier for this file, but now it crashes. Maybe I'm being throttled and the response message breaks the API?

justinsalamon avatar Oct 07 '19 23:10 justinsalamon

That's definitely because of throttling. We should make the python client handle that in a better way. In the meanwhile you can try/except HTTPError. Thanks for reporting @justinsalamon!

ffont avatar Oct 08 '19 10:10 ffont