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: