Added top lists support

This commit is contained in:
Paul Lamere 2016-03-12 09:59:16 -05:00
parent 4cf7a8ab90
commit dd021c4087
2 changed files with 19 additions and 9 deletions

View File

@ -97,6 +97,7 @@ class Spotify(object):
if self.trace: # pragma: no cover
print()
print ('headers', headers)
print ('http status', r.status_code)
print(method, r.url)
if payload:
@ -548,29 +549,27 @@ class Spotify(object):
tlist = [self._get_id('track', t) for t in tracks]
return self._put('me/tracks/?ids=' + ','.join(tlist))
def current_user_top_artists(self, limit=20, offset=0, time_range='6-months'):
def current_user_top_artists(self, limit=20, offset=0, time_range='medium_term'):
''' Get the current user's top artists
Parameters:
- limit - the number of entities to return
- offset - the index of the first entity to return
- time_range - Over what time frame are the affinities computed.
Valid-values: all-time, 6-months, 14-days. Default: 6-months
Valid-values: short_term, medium_term, long_term
'''
return self._get('me/toplists/artists', args={'time-range':time_range},
limit=limit,offset=offset)
return self._get('me/top/artists', time_range=time_range, limit=limit,offset=offset)
def current_user_top_tracks(self, limit=20, offset=0, time_range='6-months'):
def current_user_top_tracks(self, limit=20, offset=0, time_range='medium_term'):
''' Get the current user's top tracks
Parameters:
- limit - the number of entities to return
- offset - the index of the first entity to return
- time_range - Over what time frame are the affinities computed.
Valid-values: all-time, 6-months, 14-days. Default: 6-months
Valid-values: short_term, medium_term, long_term
'''
return self._get('me/toplists/tracks', args={'time-range':time_range},
limit=limit,offset=offset)
return self._get('me/top/tracks', time_range=time_range, limit=limit,offset=offset)
def featured_playlists(self, locale=None, country=None,

View File

@ -126,6 +126,16 @@ class AuthTestSpotipy(unittest.TestCase):
artists = response['artists']
self.assertTrue(len(artists['items']) > 0)
def test_current_user_top_tracks(self):
response = spotify.current_user_top_tracks()
items = response['items']
self.assertTrue(len(items) > 0)
def test_current_user_top_artists(self):
response = spotify.current_user_top_artists()
items = response['items']
self.assertTrue(len(items) > 0)
def get_or_create_spotify_playlist(self, username, playlist_name):
playlists = spotify.user_playlists(username)
while playlists:
@ -183,7 +193,8 @@ if __name__ == '__main__':
scope += 'user-library-read '
scope += 'user-follow-read '
scope += 'user-library-modify '
scope += 'user-read-private'
scope += 'user-read-private '
scope += 'user-top-read'
token = util.prompt_for_user_token(username, scope)
spotify = spotipy.Spotify(auth=token)