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 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
the user token suitable for use with the spotipy.Spotify
constructor
@ -15,7 +15,8 @@ def prompt_for_user_token(username):
client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
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,
# if not in the cache, the create a new (this will send

View File

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

View File

@ -65,7 +65,7 @@ class Spotify(object):
if self.trace:
print()
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)
results = r.json()
if self.trace:
@ -86,7 +86,6 @@ class Spotify(object):
url = method
headers = self._auth_headers()
headers['Content-Type'] = 'application/json'
print('headers', headers)
if payload:
r = requests.post(url, headers=headers, data=json.dumps(payload), **args)
else:
@ -95,13 +94,16 @@ class Spotify(object):
print()
print("POST", r.url)
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)
results = r.json()
if self.trace:
print('RESP', results)
print()
return results
try:
results = r.json()
if self.trace:
print('RESP', results)
print()
return results
except json.decoder.JSONDecodeError:
return None
def next(self, result):
''' returns the next result given a result
@ -212,15 +214,11 @@ class Spotify(object):
data = {'name':name, 'public':True }
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
'''
data = {'uris':uris}
uri_param = ','.join(uris)
#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)
return self.post("users/%s/playlists/%s/tracks" % (user,playlist_id),
payload = tracks, position=position)
def me(self):
''' returns info about me