From b412de8d75dc32cfbf7646ed8ddb9a2cd5285d0e Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Wed, 30 Mar 2016 15:43:47 -0400 Subject: [PATCH] Added artist recs example --- examples/artist_recommendations.py | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/artist_recommendations.py diff --git a/examples/artist_recommendations.py b/examples/artist_recommendations.py new file mode 100644 index 0000000..6efbc1e --- /dev/null +++ b/examples/artist_recommendations.py @@ -0,0 +1,35 @@ +import sys +import spotipy + +''' shows recommendations for the given artist +''' + +from spotipy.oauth2 import SpotifyClientCredentials +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) +sp.trace=False + +def get_artist(name): + results = sp.search(q='artist:' + name, type='artist') + items = results['artists']['items'] + if len(items) > 0: + return items[0] + else: + return None + +def show_recommendations_for_artist(artist): + albums = [] + results = sp.recommendations(seed_artists = [artist['id']]) + for track in results['tracks']: + print track['name'], '-', track['artists'][0]['name'] + +if __name__ == '__main__': + if len(sys.argv) < 2: + print(('Usage: {0} artist name'.format(sys.argv[0]))) + else: + name = ' '.join(sys.argv[1:]) + artist = get_artist(name) + if artist: + show_recommendations_for_artist(artist) + else: + print "Can't find that artist", name