Add support for checking if a user's Your Music library contains a track

This commit is contained in:
Michael Thelin 2014-07-24 00:27:35 -04:00
parent 6d6841d387
commit a4e6fc5a39
3 changed files with 35 additions and 2 deletions

View File

@ -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 users “Your Music” library.
Refer to the [Spotify API documentation](https://developer.spotify.com/spotify-web-api/) for details on the methods and parameters.

View File

@ -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

View File

@ -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 users 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: