Added support for adding tracks to a playlist

This commit is contained in:
Paul Lamere 2014-06-17 13:15:07 -04:00
parent 6ae679600a
commit f0415e9de2
4 changed files with 18 additions and 19 deletions

2
deploy
View File

@ -1,2 +1,2 @@
python setup.py sdist upload sudo python setup.py sdist upload

View File

@ -5,7 +5,7 @@ import os
import subprocess import subprocess
import spotipy.oauth2 as oauth2 import spotipy.oauth2 as oauth2
def prompt_for_user_token(username): def prompt_for_user_token(username, scope):
''' prompts the user to login if necessary and returns ''' prompts the user to login if necessary and returns
the user token suitable for use with the spotipy.Spotify the user token suitable for use with the spotipy.Spotify
constructor constructor
@ -15,7 +15,8 @@ def prompt_for_user_token(username):
client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET') client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
redirect_uri = os.getenv('REDIRECT_URI', 'YOUR_REDIRECT_URI') redirect_uri = os.getenv('REDIRECT_URI', 'YOUR_REDIRECT_URI')
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, cache_path=username) sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
scope=scope, cache_path=username)
# try to get a valid token for this user, from the cache, # try to get a valid token for this user, from the cache,
# if not in the cache, the create a new (this will send # if not in the cache, the create a new (this will send

View File

@ -3,7 +3,7 @@ from setuptools import setup
setup( setup(
name='SpotipyWebApi', name='SpotipyWebApi',
version='1.200', version='1.40',
description='simple client for the Spotify Web API', description='simple client for the Spotify Web API',
author="@plamere", author="@plamere",
author_email="paul@echonest.com", author_email="paul@echonest.com",

View File

@ -65,7 +65,7 @@ class Spotify(object):
if self.trace: if self.trace:
print() print()
print(verb, r.url) print(verb, r.url)
if r.status_code != 200: if not (r.status_code >= 200 and r.status_code < 300):
raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url) raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url)
results = r.json() results = r.json()
if self.trace: if self.trace:
@ -86,7 +86,6 @@ class Spotify(object):
url = method url = method
headers = self._auth_headers() headers = self._auth_headers()
headers['Content-Type'] = 'application/json' headers['Content-Type'] = 'application/json'
print('headers', headers)
if payload: if payload:
r = requests.post(url, headers=headers, data=json.dumps(payload), **args) r = requests.post(url, headers=headers, data=json.dumps(payload), **args)
else: else:
@ -95,13 +94,16 @@ class Spotify(object):
print() print()
print("POST", r.url) print("POST", r.url)
print("DATA", json.dumps(payload)) print("DATA", json.dumps(payload))
if r.status_code != 200: if not (r.status_code >= 200 and r.status_code < 300):
raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url) raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url)
results = r.json() try:
if self.trace: results = r.json()
print('RESP', results) if self.trace:
print() print('RESP', results)
return results print()
return results
except json.decoder.JSONDecodeError:
return None
def next(self, result): def next(self, result):
''' returns the next result given a result ''' returns the next result given a result
@ -212,15 +214,11 @@ class Spotify(object):
data = {'name':name, 'public':True } data = {'name':name, 'public':True }
return self.post("users/%s/playlists" % (user,), payload = data) return self.post("users/%s/playlists" % (user,), payload = data)
def user_playlist_add_tracks(self, user, playlist_id, uris, position=None): def user_playlist_add_tracks(self, user, playlist_id, tracks, position=None):
''' Adds tracks to a playlist ''' Adds tracks to a playlist
''' '''
data = {'uris':uris} return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id),
uri_param = ','.join(uris) payload = tracks, position=position)
#return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id), payload = data)
#return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id), payload = data, position=position)
#return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id), uris=uri_param, position=position)
return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id), uris=uri_param)
def me(self): def me(self):
''' returns info about me ''' returns info about me