From f0415e9de28bde1739459c3f2a8624f8aa4c651f Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Tue, 17 Jun 2014 13:15:07 -0400 Subject: [PATCH] Added support for adding tracks to a playlist --- deploy | 2 +- examples/util.py | 5 +++-- setup.py | 2 +- spotipy/__init__.py | 28 +++++++++++++--------------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/deploy b/deploy index 7daefcf..d760a2a 100755 --- a/deploy +++ b/deploy @@ -1,2 +1,2 @@ -python setup.py sdist upload +sudo python setup.py sdist upload diff --git a/examples/util.py b/examples/util.py index 995c09e..8173e7e 100644 --- a/examples/util.py +++ b/examples/util.py @@ -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 diff --git a/setup.py b/setup.py index 94c54e5..fa88b33 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/spotipy/__init__.py b/spotipy/__init__.py index 1170222..c39e2e3 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -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