From 365d3714bfff9b64de9302d24f2f3ff01cfdfa9b Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Wed, 30 Dec 2015 16:11:27 -0500 Subject: [PATCH] checkpoint --- examples/audio_features.py | 32 ++++++++++++++++++++++++++++++++ examples/audio_features2.py | 32 ++++++++++++++++++++++++++++++++ spotipy/client.py | 23 +++++++++++++++++++++++ tests/authtests.py | 6 +++++- 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 examples/audio_features.py create mode 100644 examples/audio_features2.py diff --git a/examples/audio_features.py b/examples/audio_features.py new file mode 100644 index 0000000..6ad5973 --- /dev/null +++ b/examples/audio_features.py @@ -0,0 +1,32 @@ + +# shows acoustic features for tracks for the given artist + +from __future__ import print_function # (at top of module) +from spotipy.oauth2 import SpotifyClientCredentials +import json +import spotipy +import time +import sys + + +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) +sp.trace=True + +if len(sys.argv) > 1: + artist_name = ' '.join(sys.argv[1:]) + results = sp.search(q=artist_name, limit=50) + tids = [] + for i, t in enumerate(results['tracks']['items']): + 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_features2.py b/examples/audio_features2.py new file mode 100644 index 0000000..473c7e5 --- /dev/null +++ b/examples/audio_features2.py @@ -0,0 +1,32 @@ +# shows acoustic features for tracks for the given artist + +from __future__ import print_function # (at top of module) +from spotipy.oauth2 import SpotifyClientCredentials +import json +import spotipy +import time +import sys + +460V2cUWccVmYeTd4TLabO,01N7pbYH5f9TpLxmqosmUe,2Rrkgr8Kd0XGNbilwip6r8,3iJacHlEpRhG8CIStLXYbs,3D0stCwnqbPRkbXnUp28PR,5f1E93e9HPSRs24QM4WZsa,3BxHcg8PCUVK3m7gESNFg9,6chhc8QbIYUW9eROLwzTt7,6cIqoP7oV1H5NjmX4rxLie,4u10VvC4AV75eQ0boApzkq,%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C5nsikUg2MZkmb1nxvcFl5J%2C5PukeF41F4XNCyltPrjtfO%2C2JCpgkECOJyD7yUokQZDrs%2C7dwXipvdDxipgRu47xH98K%2C4IK53QTieazWuWUYm5fkP8%2C5dcQ2uEhO0hXdvcWFQShV9%2C3KpmsYSzgeummZ4leRtjbM%2C3kFuIYz4jYu1Wgq5HuFnid%2C5uEsEDcw8D30AmksKuavMh%2C6HjVlvOUu8zaLlNLCAELrd%2C4zrJmEMmQKxC5desjEgadB%2C41wBEIqFyttEGmbQcRHu13%2C5ATVpeENGTukudcWso7WeE%2C6qs7nmfAvHTIIpFLFsDTVc%2C4KSx0OmRJpT8EvTNnzZw2V%2C7eyhxafr2Q09JcqkSRSkOX%2C6JP7TRXXgTcuoDyotNR5Bu%2C2LwXZozk3SAWgqunHZq5pV%2C1lE88zONI0LR9hAi14F9tJ%2C3VxJ96B20xnFfxCrBCgjh0%2C2kEDlDyDht2OnN3eUD6AKy%2C59jyWHNXkL6R2rWsz2hnuH%2C2Brivg4Hjx6nqmUXJCkrDQ%2C1C0mAIhKJ5luyb2TNE7n7E%2C0y2nkKDa3VMaUx9rvzERJv%2C3AJK9D8d5AMxx0cgLbsL5W + +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) +sp.trace=True + +if len(sys.argv) > 1: + artist_name = ' '.join(sys.argv[1:]) + results = sp.search(q=artist_name, limit=50) + tids = [] + for i, t in enumerate(results['tracks']['items']): + 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/spotipy/client.py b/spotipy/client.py index da37602..e62b618 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -31,6 +31,7 @@ class Spotify(object): sp = spotipy.Spotify() sp.trace = True # turn on tracing + sp.trace_out = True # turn on trace out artist = sp.artist(urn) print(artist) @@ -40,6 +41,7 @@ class Spotify(object): ''' trace = False # Enable tracing? + trace_out = False max_get_retries = 10 def __init__(self, auth=None, requests_session=True, @@ -89,6 +91,8 @@ class Spotify(object): if payload: args["data"] = json.dumps(payload) + if self.trace_out: + print(url) r = self._session.request(method, url, headers=headers, **args) if self.trace: # pragma: no cover @@ -475,6 +479,17 @@ class Spotify(object): ''' return self.me() + def current_user_saved_albums(self, limit=20, offset=0): + ''' Gets a list of the albums saved in the current authorized user's + "Your Music" library + + Parameters: + - limit - the number of albums to return + - offset - the index of the first album to return + + ''' + return self._get('me/albums', limit=limit, offset=offset) + def current_user_saved_tracks(self, limit=20, offset=0): ''' Gets a list of the tracks saved in the current authorized user's "Your Music" library @@ -570,6 +585,14 @@ class Spotify(object): return self._get('browse/new-releases', country=country, limit=limit, offset=offset) + def audio_features(self, tracks=[]): + ''' Get audio features for multiple tracks based upon their Spotify IDs + Parameters: + - tracks - a list of track URIs, URLs or IDs, maximum: 50 ids + ''' + tlist = [self._get_id('track', t) for t in tracks] + return self._get('audio-features?ids=' + ','.join(tlist)) + def _get_id(self, type, id): fields = id.split(':') if len(fields) >= 3: diff --git a/tests/authtests.py b/tests/authtests.py index a28ad5b..1666f77 100644 --- a/tests/authtests.py +++ b/tests/authtests.py @@ -66,7 +66,7 @@ class AuthTestSpotipy(unittest.TestCase): user = playlist['owner']['id'] pid = playlist['id'] results = spotify.user_playlist_tracks(user, pid) - self.assertTrue(len(results['items']) > 0) + self.assertTrue(len(results['items']) >= 0) def user_playlist_tracks(self, user, playlist_id = None, fields=None, limit=100, offset=0): @@ -81,6 +81,10 @@ class AuthTestSpotipy(unittest.TestCase): tracks = spotify.current_user_saved_tracks() self.assertTrue(len(tracks['items']) > 0) + def test_current_user_saved_albums(self): + albums = spotify.current_user_saved_albums() + self.assertTrue(len(albums['items']) > 0) + def test_current_user_save_and_unsave_tracks(self): tracks = spotify.current_user_saved_tracks() total = tracks['total']