2019-03-16 02:34:09 +00:00
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
|
|
|
|
class User:
|
|
|
|
|
|
|
|
def __init__(self, username, pagesize = 200):
|
|
|
|
self.api_key = os.environ['FMKEY']
|
|
|
|
|
|
|
|
self.username = username
|
|
|
|
self.pagesize = pagesize
|
|
|
|
|
|
|
|
def __makeRequest(self, method, extra = {}, page = 1):
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"format": 'json',
|
|
|
|
"method": method,
|
|
|
|
"limit": self.pagesize,
|
|
|
|
"page": page,
|
|
|
|
"user": self.username,
|
|
|
|
"api_key": self.api_key
|
|
|
|
}
|
|
|
|
data.update(extra)
|
|
|
|
|
2019-03-24 22:51:11 +00:00
|
|
|
req = requests.get('http://ws.audioscrobbler.com/2.0/', params = data)
|
2019-03-16 02:34:09 +00:00
|
|
|
|
|
|
|
if req.status_code < 200 or req.status_code > 299:
|
|
|
|
|
2019-03-24 22:51:11 +00:00
|
|
|
if req.json()['error'] == 8:
|
|
|
|
print('ERROR: retrying call ' + method)
|
2019-03-24 22:59:06 +00:00
|
|
|
return self.__makeRequest(method, extra, page)
|
2019-03-24 22:51:11 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('HTTP Error Raised: ' + str(req.json()['error']) + ' ' + req.json()['message'])
|
2019-03-16 02:34:09 +00:00
|
|
|
|
|
|
|
return req.json()
|
|
|
|
|
|
|
|
def getRecentTracks(self, offset = 1, pagelimit = 0):
|
|
|
|
|
|
|
|
scrobbles = []
|
|
|
|
|
|
|
|
print(str(offset) + ' offset')
|
|
|
|
|
|
|
|
json = self.__makeRequest('user.getrecenttracks', page = offset)
|
|
|
|
scrobbles += json['recenttracks']['track']
|
|
|
|
|
|
|
|
if pagelimit > 0:
|
|
|
|
if offset < pagelimit and offset < int(json['recenttracks']['@attr']['totalPages']):
|
|
|
|
scrobbles += self.getRecentTracks(offset = offset + 1, pagelimit = pagelimit)
|
|
|
|
else:
|
|
|
|
if offset < int(json['recenttracks']['@attr']['totalPages']):
|
|
|
|
scrobbles += self.getRecentTracks(offset = offset + 1, pagelimit = pagelimit)
|
|
|
|
|
|
|
|
return scrobbles
|