From 2d98c30d0172024bcbf1356f874ed6779e21ac4d Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Wed, 23 Jul 2014 10:04:07 -0400 Subject: [PATCH] Added support for 'Your Music' tracks --- examples/show_my_saved_tracks.py | 27 +++++++++++++++++++++++ setup.py | 2 +- spotipy/__init__.py | 37 +++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 examples/show_my_saved_tracks.py diff --git a/examples/show_my_saved_tracks.py b/examples/show_my_saved_tracks.py new file mode 100644 index 0000000..eecf20e --- /dev/null +++ b/examples/show_my_saved_tracks.py @@ -0,0 +1,27 @@ + +# Adds tracks to a playlist + +import pprint +import sys + +import spotipy +import spotipy.oauth2 as oauth2 +import util + +scope = 'user-library-read' + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + print "Usage: %s username" % (sys.argv[0],) + sys.exit() + +token = util.prompt_for_user_token(username, scope) + +if token: + sp = spotipy.Spotify(auth=token) + sp.trace = False + results = sp.current_user_saved_tracks() + pprint.pprint(results) +else: + print "Can't get token for", username diff --git a/setup.py b/setup.py index 5beb49b..3d35f16 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup setup( name='SpotipyWebApi', - version='1.45', + version='1.46', description='simple client for the Spotify Web API', author="@plamere", author_email="paul@echonest.com", diff --git a/spotipy/__init__.py b/spotipy/__init__.py index f944304..a011c2e 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -78,6 +78,16 @@ class Spotify(object): kwargs.update(args) return self._internal_call('GET', method, kwargs) + def delete(self, method, args=None, **kwargs): + if args: + kwargs.update(args) + return self._internal_call('DELETE', method, kwargs) + + def put(self, method, args=None, **kwargs): + if args: + kwargs.update(args) + return self._internal_call('PUT', method, kwargs) + def post(self, method, payload=None, **kwargs): args = dict(params=kwargs) if not method.startswith('http'): @@ -230,10 +240,35 @@ class Spotify(object): payload = tracks, position=position) def me(self): - ''' returns info about me + ''' Get detailed profile information about the current user. ''' return self.get('me/') + def current_user(self): + ''' Get detailed profile information about the current user. + ''' + return self.me() + + 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 + ''' + return self.get('me/tracks', limit=limit, offset=offset) + + def current_user_saved_tracks_delete(self, ids=[]): + ''' Remove one or more tracks from the current user’s + “Your Music” library. + ''' + tlist = [self._get_id('track', t) for t in ids] + return self.delete('me/tracks/?ids=' + ','.join(tlist)) + + def current_user_saved_tracks_add(self, ids=[]): + ''' Add one or more tracks to the current user’s + “Your Music” library. + ''' + tlist = [self._get_id('track', t) for t in ids] + return self.put('me/tracks/?ids=' + ','.join(tlist)) + def _get_id(self, type, id): fields = id.split(':') if len(fields) == 3: