From 4cf7a8ab90b6be8f85915dd7075dd0afd5373b9d Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Sat, 12 Mar 2016 07:32:01 -0500 Subject: [PATCH] Added top artists and tracks support --- examples/my_top_artists.py | 30 ++++++++++++++++++++++++++++++ examples/my_top_tracks.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 examples/my_top_artists.py create mode 100644 examples/my_top_tracks.py diff --git a/examples/my_top_artists.py b/examples/my_top_artists.py new file mode 100644 index 0000000..91cbfe2 --- /dev/null +++ b/examples/my_top_artists.py @@ -0,0 +1,30 @@ +# Shows the top artists for a user + +import pprint +import sys + +import spotipy +import spotipy.util as util +import simplejson as json + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + print("Usage: %s username" % (sys.argv[0],)) + sys.exit() + +scope = 'user-top-read' +token = util.prompt_for_user_token(username, scope) + +if token: + sp = spotipy.Spotify(auth=token) + sp.trace = False + ranges = ['short_term', 'medium_term', 'long_term'] + for range in ranges: + print "range:", range + results = sp.current_user_top_artists(time_range=range, limit=50) + for i, item in enumerate(results['items']): + print i, item['name'] + print +else: + print("Can't get token for", username) diff --git a/examples/my_top_tracks.py b/examples/my_top_tracks.py new file mode 100644 index 0000000..6bd72ec --- /dev/null +++ b/examples/my_top_tracks.py @@ -0,0 +1,31 @@ +# Adds tracks to a playlist + +import pprint +import sys + +import spotipy +import spotipy.util as util +import simplejson as json + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + print("Usage: %s username" % (sys.argv[0],)) + sys.exit() + +scope = 'user-top-read' +token = util.prompt_for_user_token(username, scope) + +if token: + sp = spotipy.Spotify(auth=token) + sp.trace = False + ranges = ['short_term', 'medium_term', 'long_term'] + for range in ranges: + print "range:", range + results = sp.current_user_top_tracks(time_range=range, limit=50) + for i, item in enumerate(results['items']): + print i, item['name'], '//', item['artists'][0]['name'] + print + +else: + print("Can't get token for", username)