From 4587a0771d415c402692ea7acd881c7fefc0d68a Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Sat, 20 Feb 2016 08:54:11 -0500 Subject: [PATCH] Cleaning up some tests --- examples/audio_features.py | 6 +----- examples/audio_features_for_track.py | 1 - spotipy/client.py | 29 ++++++++++++++++++++++++++-- tests/authtests.py | 20 +------------------ tests/authtests2.py | 2 -- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/examples/audio_features.py b/examples/audio_features.py index 6ad5973..d956c37 100644 --- a/examples/audio_features.py +++ b/examples/audio_features.py @@ -11,7 +11,7 @@ import sys client_credentials_manager = SpotifyClientCredentials() sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) -sp.trace=True +sp.trace=False if len(sys.argv) > 1: artist_name = ' '.join(sys.argv[1:]) @@ -21,12 +21,8 @@ if len(sys.argv) > 1: print(' ', i, t['name']) tids.append(t['uri']) - print(tids) - start = time.time() features = sp.audio_features(tids) delta = time.time() - start - print ("features", features) print(json.dumps(features, indent=4)) - print ("features retrieved in %.2f seconds" % (delta,)) diff --git a/examples/audio_features_for_track.py b/examples/audio_features_for_track.py index 817fcc3..cdc980a 100644 --- a/examples/audio_features_for_track.py +++ b/examples/audio_features_for_track.py @@ -21,6 +21,5 @@ if len(sys.argv) > 1: start = time.time() features = sp.audio_features(tids) delta = time.time() - start - print ("features", features) print(json.dumps(features, indent=4)) print ("features retrieved in %.2f seconds" % (delta,)) diff --git a/spotipy/client.py b/spotipy/client.py index 87ac29d..ff12ee4 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -97,6 +97,7 @@ class Spotify(object): if self.trace: # pragma: no cover print() + print ('http status', r.status_code) print(method, r.url) if payload: print("DATA", json.dumps(payload)) @@ -547,6 +548,30 @@ 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'): + ''' 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 + ''' + return self._get('me/toplists/artists', args={'time-range':time_range}, + limit=limit,offset=offset) + + def current_user_top_tracks(self, limit=20, offset=0, time_range='6-months'): + ''' 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 + ''' + return self._get('me/toplists/tracks', args={'time-range':time_range}, + limit=limit,offset=offset) + def featured_playlists(self, locale=None, country=None, timestamp=None, limit=20, offset = 0): @@ -636,8 +661,8 @@ class Spotify(object): results = self._get('audio-features?ids=' + ','.join(tlist)) # the response has changed, look for the new style first, and if # its not there, fallback on the old style - if 'audio_attributes' in results: - return results['audio_attributes'] + if 'audio_features' in results: + return results['audio_features'] else: return results diff --git a/tests/authtests.py b/tests/authtests.py index 9352b16..6502abd 100644 --- a/tests/authtests.py +++ b/tests/authtests.py @@ -5,6 +5,7 @@ from spotipy import util import unittest import pprint import sys +import simplejson as json ''' Since these tests require authentication they are maintained @@ -173,25 +174,6 @@ class AuthTestSpotipy(unittest.TestCase): self.assertTrue(playlist['tracks']['total'] == 3) self.assertTrue(len(playlist['tracks']['items']) == 3) - def test_audio_features(self): - results = spotify.audio_features(self.four_tracks) - self.assertTrue(len(results) == len(self.four_tracks)) - for attr in results: - assert('speechiness' in attr) - - def test_audio_features_with_bad_track(self): - bad_tracks = ['spotify:track:bad'] - input = self.four_tracks + bad_tracks - results = spotify.audio_features(input) - self.assertTrue(len(results) == len(input)) - none_count = 0 - for attr in results: - if attr == None: - none_count +=1 - else: - assert('speechiness' in attr) - self.assertTrue(none_count == len(bad_tracks)) - if __name__ == '__main__': if len(sys.argv) > 1: username = sys.argv[1] diff --git a/tests/authtests2.py b/tests/authtests2.py index 0b2ba07..f72e45f 100644 --- a/tests/authtests2.py +++ b/tests/authtests2.py @@ -38,7 +38,6 @@ class AuthTestSpotipy(unittest.TestCase): def test_audio_features(self): - print "test audio features" results = spotify.audio_features(self.four_tracks) self.assertTrue(len(results) == len(self.four_tracks)) for track in results: @@ -49,7 +48,6 @@ class AuthTestSpotipy(unittest.TestCase): bad_tracks = ['spotify:track:bad'] input = bad_tracks + self.four_tracks + bad_tracks results = spotify.audio_features(input) - print(json.dumps(results, indent=4)) self.assertTrue(len(results) == len(input)) for track in results[:-1]: assert('speechiness' in track)