Added support for 'Your Music' tracks

This commit is contained in:
Paul Lamere 2014-07-23 10:04:07 -04:00
parent 58123eda31
commit 2d98c30d01
3 changed files with 64 additions and 2 deletions

View File

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

View File

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

View File

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