From a4e6fc5a3965a595e8a775a0717eb91ff530ae2d Mon Sep 17 00:00:00 2001 From: Michael Thelin Date: Thu, 24 Jul 2014 00:27:35 -0400 Subject: [PATCH] Add support for checking if a user's Your Music library contains a track --- README.md | 5 +++-- examples/contains_a_saved_track.py | 25 +++++++++++++++++++++++++ spotipy/__init__.py | 7 +++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 examples/contains_a_saved_track.py diff --git a/README.md b/README.md index fb2a188..d7ebb87 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,9 @@ A full set of examples can be found in the [Spotipy examples directory](https:// - **user_playlists(self, user)** - Gets playlists of a user - **current_user(self)** - Get detailed profile information about the current user. - **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 - - **current_user_saved_tracks_delete(self, limit=20, offset=0)** - Remove tracks from the current authorized user's "Your Music" library - - **current_user_saved_tracks_add(self, limit=20, offset=0)** - Add tracks to the current authorized user's "Your Music" library + - **current_user_saved_tracks_delete(self, tracks)** - Remove tracks from the current authorized user's "Your Music" library + - **current_user_saved_tracks_add(self, tracks)** - Add tracks to the current authorized user's "Your Music" library + - **current_user_saved_tracks_contains(self, tracks)** - Check if one or more tracks is already saved in the current Spotify user’s “Your Music” library. Refer to the [Spotify API documentation](https://developer.spotify.com/spotify-web-api/) for details on the methods and parameters. diff --git a/examples/contains_a_saved_track.py b/examples/contains_a_saved_track.py new file mode 100644 index 0000000..6155fad --- /dev/null +++ b/examples/contains_a_saved_track.py @@ -0,0 +1,25 @@ +import pprint +import sys + +import spotipy +import spotipy.oauth2 as oauth2 +import util + +scope = 'user-library-read' + +if len(sys.argv) > 2: + username = sys.argv[1] + tids = sys.argv[2:] +else: + print "Usage: %s username track-id ..." % (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_contains(ids=tids) + pprint.pprint(results) +else: + print "Can't get token for", username \ No newline at end of file diff --git a/spotipy/__init__.py b/spotipy/__init__.py index c7e884d..009d5a2 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -272,6 +272,13 @@ class Spotify(object): tlist = [self._get_id('track', t) for t in ids] return self.put('me/tracks/?ids=' + ','.join(tlist)) + def current_user_saved_tracks_contains(self, ids=[]): + ''' Check if one or more tracks is already saved in + the current Spotify user’s “Your Music” library. + ''' + tlist = [self._get_id('track', t) for t in ids] + return self.get('me/tracks/contains?ids=' + ','.join(tlist)) + def _get_id(self, type, id): fields = id.split(':') if len(fields) == 3: