Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise ConnectionError (Connection aborted,BadStatusLine): using python and Last.fm API

Tags:

python

last.fm

I'm using python to call Last.fm API to fetch user information. This error occurs after fetching about 7000 users:

Traceback (most recent call last):

  File "C:/Python27/My Codes & Data/scanuserinfo_100K_3.py", line 48, in <module>

    r=requests.get(host,params=params)

  File "C:\Python27\requests\api.py", line 60, in get

    return request('get', url, **kwargs)

  File "C:\Python27\requests\api.py", line 49, in request

    return session.request(method=method, url=url, **kwargs)

  File "C:\Python27\requests\sessions.py", line 457, in request

    resp = self.send(prep, **send_kwargs)

  File "C:\Python27\requests\sessions.py", line 569, in send

    r = adapter.send(request, **kwargs)

  File "C:\Python27\requests\adapters.py", line 407, in send

    raise ConnectionError(err, request=request)

ConnectionError: ('Connection aborted.', BadStatusLine("''",))

Here is my code for calling the API:

import requests

API_KEY='####my api key input here#####'

host='http://ws.audioscrobbler.com/2.0/'

params={'method':'user.getInfo',

            'user':'###userid input here####',

            'api_key':API_KEY   
    }

r=requests.get(host,params=params)

Could someone help me out here?

like image 255
Ruby Avatar asked Nov 09 '22 23:11

Ruby


1 Answers

It might be related to their API throttling, as they expect you not to make over 5 calls/s * 60 sec/min * 5 min = 1500 calls/ 5min

http://www.last.fm/api/tos

4.4 You will implement suitable caching in accordance with the HTTP headers sent with web service responses. You will not make more than 5 requests per originating IP address per second, averaged over a 5 minute period, without prior written consent. You agree to cache similar artist and any chart data (top tracks, top artists, top albums) for a minimum of one week.

like image 121
yi cheng Avatar answered Nov 14 '22 21:11

yi cheng