diff --git a/docs/conf.py b/docs/conf.py index bcfcfd0..9185e1f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -42,8 +42,8 @@ source_suffix = '.rst' master_doc = 'index' # General information about the project. -project = u'spotipy' -copyright = u'2014, Paul Lamere' +project = 'spotipy' +copyright = '2014, Paul Lamere' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -185,8 +185,8 @@ latex_elements = { # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). latex_documents = [ - ('index', 'spotipy.tex', u'spotipy Documentation', - u'Paul Lamere', 'manual'), + ('index', 'spotipy.tex', 'spotipy Documentation', + 'Paul Lamere', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -215,8 +215,8 @@ latex_documents = [ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'spotipy', u'spotipy Documentation', - [u'Paul Lamere'], 1) + ('index', 'spotipy', 'spotipy Documentation', + ['Paul Lamere'], 1) ] # If true, show URL addresses after external links. @@ -229,8 +229,8 @@ man_pages = [ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - ('index', 'spotipy', u'spotipy Documentation', - u'Paul Lamere', 'spotipy', 'One line description of project.', + ('index', 'spotipy', 'spotipy Documentation', + 'Paul Lamere', 'spotipy', 'One line description of project.', 'Miscellaneous'), ] diff --git a/examples/add_a_saved_track.py b/examples/add_a_saved_track.py index f403d82..fd8ec1e 100644 --- a/examples/add_a_saved_track.py +++ b/examples/add_a_saved_track.py @@ -13,7 +13,7 @@ if len(sys.argv) > 2: username = sys.argv[1] tids = sys.argv[2:] else: - print "Usage: %s username track-id ..." % (sys.argv[0],) + print("Usage: %s username track-id ..." % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username, scope) @@ -24,4 +24,4 @@ if token: results = sp.current_user_saved_tracks_add(tracks=tids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/add_tracks_to_playlist.py b/examples/add_tracks_to_playlist.py index bda1cd8..a5297da 100644 --- a/examples/add_tracks_to_playlist.py +++ b/examples/add_tracks_to_playlist.py @@ -12,7 +12,7 @@ if len(sys.argv) > 3: playlist_id = sys.argv[2] track_ids = sys.argv[3:] else: - print "Usage: %s username playlist_id track_id ..." % (sys.argv[0],) + print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],)) sys.exit() scope = 'playlist-modify-public' @@ -22,6 +22,6 @@ if token: sp = spotipy.Spotify(auth=token) sp.trace = False results = sp.user_playlist_add_tracks(username, playlist_id, track_ids) - print results + print(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/artist_albums.py b/examples/artist_albums.py index b8b35e2..6b9f6ce 100644 --- a/examples/artist_albums.py +++ b/examples/artist_albums.py @@ -24,14 +24,14 @@ def show_artist_albums(artist): for album in albums: name = album['name'] if name not in seen: - print(' ' + name) + print((' ' + name)) seen.add(name) if __name__ == '__main__': sp = spotipy.Spotify() if len(sys.argv) < 2: - print('Usage: {0} artist name'.format(sys.argv[0])) + print(('Usage: {0} artist name'.format(sys.argv[0]))) else: name = ' '.join(sys.argv[1:]) artist = get_artist(name) diff --git a/examples/artist_discography.py b/examples/artist_discography.py index cd2fb38..8d30d15 100644 --- a/examples/artist_discography.py +++ b/examples/artist_discography.py @@ -20,9 +20,9 @@ def show_album_tracks(album): results = sp.next(results) tracks.extend(results['items']) for track in tracks: - print ' ', track['name'] - print - print track + print(' ', track['name']) + print() + print(track) def show_artist_albums(id): albums = [] @@ -31,27 +31,27 @@ def show_artist_albums(id): while results['next']: results = sp.next(results) albums.extend(results['items']) - print 'Total albums:', len(albums) + print('Total albums:', len(albums)) unique = set() # skip duplicate albums for album in albums: name = album['name'] if not name in unique: - print name + print(name) unique.add(name) show_album_tracks(album) def show_artist(artist): - print '====', artist['name'], '====' - print 'Popularity: ', artist['popularity'] + print('====', artist['name'], '====') + print('Popularity: ', artist['popularity']) if len(artist['genres']) > 0: - print 'Genres: ', ','.join(artist['genres']) + print('Genres: ', ','.join(artist['genres'])) if __name__ == '__main__': sp = spotipy.Spotify() sp.trace = False if len(sys.argv) < 2: - print('Usage: {0} artist name'.format(sys.argv[0])) + print(('Usage: {0} artist name'.format(sys.argv[0]))) else: name = ' '.join(sys.argv[1:]) artist = get_artist(name) diff --git a/examples/contains_a_saved_track.py b/examples/contains_a_saved_track.py index 864e5c3..2dac2f5 100644 --- a/examples/contains_a_saved_track.py +++ b/examples/contains_a_saved_track.py @@ -10,7 +10,7 @@ if len(sys.argv) > 2: username = sys.argv[1] tids = sys.argv[2:] else: - print "Usage: %s username track-id ..." % (sys.argv[0],) + print("Usage: %s username track-id ..." % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username, scope) @@ -21,4 +21,4 @@ if token: results = sp.current_user_saved_tracks_contains(tracks=tids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/create_playlist.py b/examples/create_playlist.py index 29881cc..aacbac7 100644 --- a/examples/create_playlist.py +++ b/examples/create_playlist.py @@ -13,7 +13,7 @@ if len(sys.argv) > 2: username = sys.argv[1] playlist_name = sys.argv[2] else: - print "Usage: %s username playlist-name" % (sys.argv[0],) + print("Usage: %s username playlist-name" % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username) @@ -24,4 +24,4 @@ if token: playlists = sp.user_playlist_create(username, playlist_name) pprint.pprint(playlists) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/delete_a_saved_track.py b/examples/delete_a_saved_track.py index 0087bb7..7d40b6b 100644 --- a/examples/delete_a_saved_track.py +++ b/examples/delete_a_saved_track.py @@ -12,7 +12,7 @@ if len(sys.argv) > 2: username = sys.argv[1] tids = sys.argv[2:] else: - print "Usage: %s username track-id ..." % (sys.argv[0],) + print("Usage: %s username track-id ..." % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username, scope) @@ -23,4 +23,4 @@ if token: results = sp.current_user_saved_tracks_delete(tracks=tids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/remove_specific_tracks_from_playlist.py b/examples/remove_specific_tracks_from_playlist.py index 70e6525..a99dc94 100644 --- a/examples/remove_specific_tracks_from_playlist.py +++ b/examples/remove_specific_tracks_from_playlist.py @@ -16,7 +16,7 @@ if len(sys.argv) > 3: tid, pos = t_pos.split(',') track_ids.append( { "uri" : tid, "positions": [ int(pos)] } ) else: - print "Usage: %s username playlist_id track_id,pos track_id,pos ..." % (sys.argv[0],) + print("Usage: %s username playlist_id track_id,pos track_id,pos ..." % (sys.argv[0],)) sys.exit() scope = 'playlist-modify-public' @@ -28,4 +28,4 @@ if token: results = sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, track_ids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/remove_tracks_from_playlist.py b/examples/remove_tracks_from_playlist.py index a7ba3d8..e9fca7b 100644 --- a/examples/remove_tracks_from_playlist.py +++ b/examples/remove_tracks_from_playlist.py @@ -12,7 +12,7 @@ if len(sys.argv) > 3: playlist_id = sys.argv[2] track_ids = sys.argv[3:] else: - print "Usage: %s username playlist_id track_id ..." % (sys.argv[0],) + print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],)) sys.exit() scope = 'playlist-modify-public' @@ -24,4 +24,4 @@ if token: results = sp.user_playlist_remove_all_occurrences_of_tracks(username, playlist_id, track_ids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/replace_tracks_in_playlist.py b/examples/replace_tracks_in_playlist.py index 2d40178..452d328 100644 --- a/examples/replace_tracks_in_playlist.py +++ b/examples/replace_tracks_in_playlist.py @@ -12,7 +12,7 @@ if len(sys.argv) > 3: playlist_id = sys.argv[2] track_ids = sys.argv[3:] else: - print "Usage: %s username playlist_id track_id ..." % (sys.argv[0],) + print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],)) sys.exit() scope = 'playlist-modify-public' @@ -24,4 +24,4 @@ if token: results = sp.user_playlist_replace_tracks(username, playlist_id, track_ids) pprint.pprint(results) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/show_artist_top_tracks.py b/examples/show_artist_top_tracks.py index e710d82..a80beb8 100644 --- a/examples/show_artist_top_tracks.py +++ b/examples/show_artist_top_tracks.py @@ -13,4 +13,4 @@ sp = spotipy.Spotify() response = sp.artist_top_tracks(urn) for track in response['tracks']: - print track['name'] + print(track['name']) diff --git a/examples/show_featured_playlists.py b/examples/show_featured_playlists.py index 823f8df..2c7f8d9 100644 --- a/examples/show_featured_playlists.py +++ b/examples/show_featured_playlists.py @@ -8,8 +8,8 @@ import spotipy.util as util if len(sys.argv) > 1: username = sys.argv[1] else: - print "Whoops, need your username!" - print "usage: python featured_playlists.py [username]" + print("Whoops, need your username!") + print("usage: python featured_playlists.py [username]") sys.exit() token = util.prompt_for_user_token(username) @@ -18,16 +18,16 @@ if token: sp = spotipy.Spotify(auth=token) response = sp.featured_playlists() - print response['message'] + print(response['message']) while response: playlists = response['playlists'] for i, item in enumerate(playlists['items']): - print playlists['offset'] + i, item['name'] + print(playlists['offset'] + i, item['name']) if playlists['next']: response = sp.next(playlists) else: response = None else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/show_my_saved_tracks.py b/examples/show_my_saved_tracks.py index ee972fa..4e74562 100644 --- a/examples/show_my_saved_tracks.py +++ b/examples/show_my_saved_tracks.py @@ -10,7 +10,7 @@ scope = 'user-library-read' if len(sys.argv) > 1: username = sys.argv[1] else: - print "Usage: %s username" % (sys.argv[0],) + print("Usage: %s username" % (sys.argv[0],)) sys.exit() token = util.prompt_for_user_token(username, scope) @@ -20,6 +20,6 @@ if token: results = sp.current_user_saved_tracks() for item in results['items']: track = item['track'] - print track['name'] + ' - ' + track['artists'][0]['name'] + print(track['name'] + ' - ' + track['artists'][0]['name']) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/show_new_releases.py b/examples/show_new_releases.py index 5485435..5205245 100644 --- a/examples/show_new_releases.py +++ b/examples/show_new_releases.py @@ -8,8 +8,8 @@ import spotipy.util as util if len(sys.argv) > 1: username = sys.argv[1] else: - print "Whoops, need your username!" - print "usage: python new_releases.py [username]" + print("Whoops, need your username!") + print("usage: python new_releases.py [username]") sys.exit() token = util.prompt_for_user_token(username) @@ -22,11 +22,11 @@ if token: while response: albums = response['albums'] for i, item in enumerate(albums['items']): - print albums['offset'] + i,item['name'] + print(albums['offset'] + i,item['name']) if albums['next']: response = sp.next(albums) else: response = None else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/show_related.py b/examples/show_related.py index ce96e42..b78e1dc 100644 --- a/examples/show_related.py +++ b/examples/show_related.py @@ -17,9 +17,9 @@ try: uri = result['artists']['items'][0]['uri'] related = sp.artist_related_artists(uri) - print 'Related artists for', name + print('Related artists for', name) for artist in related['artists']: - print ' ', artist['name'] + print(' ', artist['name']) except: - print "usage show_related.py [artist-name]" + print("usage show_related.py [artist-name]") diff --git a/examples/show_tracks.py b/examples/show_tracks.py index de64bbb..79a5302 100644 --- a/examples/show_tracks.py +++ b/examples/show_tracks.py @@ -15,10 +15,10 @@ if __name__ == '__main__': tids = file.read().split() sp = spotipy.Spotify() - for start in xrange(0, len(tids), max_tracks_per_call): + for start in range(0, len(tids), max_tracks_per_call): results = sp.tracks(tids[start: start + max_tracks_per_call]) for track in results['tracks']: - print track['name'] + ' - ' + track['artists'][0]['name'] + print(track['name'] + ' - ' + track['artists'][0]['name']) diff --git a/examples/simple0.py b/examples/simple0.py index f5e76c1..52540bb 100644 --- a/examples/simple0.py +++ b/examples/simple0.py @@ -3,4 +3,4 @@ sp = spotipy.Spotify() results = sp.search(q='weezer', limit=20) for i, t in enumerate(results['tracks']['items']): - print ' ', i, t['name'] + print(' ', i, t['name']) diff --git a/examples/simple1.py b/examples/simple1.py index 48d8e97..1748b24 100644 --- a/examples/simple1.py +++ b/examples/simple1.py @@ -12,5 +12,5 @@ while results['next']: albums.extend(results['items']) for album in albums: - print(album['name']) + print((album['name'])) diff --git a/examples/simple2.py b/examples/simple2.py index 6bb6526..3b06ddc 100644 --- a/examples/simple2.py +++ b/examples/simple2.py @@ -9,7 +9,7 @@ spotify = spotipy.Spotify() results = spotify.artist_top_tracks(lz_uri) for track in results['tracks'][:10]: - print 'track : ' + track['name'] - print 'audio : ' + track['preview_url'] - print 'cover art: ' + track['album']['images'][0]['url'] - print + print('track : ' + track['name']) + print('audio : ' + track['preview_url']) + print('cover art: ' + track['album']['images'][0]['url']) + print() diff --git a/examples/simple3.py b/examples/simple3.py index bac271b..96f300a 100644 --- a/examples/simple3.py +++ b/examples/simple3.py @@ -12,5 +12,5 @@ results = spotify.search(q='artist:' + name, type='artist') items = results['artists']['items'] if len(items) > 0: artist = items[0] - print artist['name'], artist['images'][0]['url'] + print(artist['name'], artist['images'][0]['url']) diff --git a/examples/tracks.py b/examples/tracks.py index 92104b1..0e3f8c0 100644 --- a/examples/tracks.py +++ b/examples/tracks.py @@ -1,6 +1,6 @@ # shows tracks for the given artist -from __future__ import print_function + import spotipy import sys sp = spotipy.Spotify() diff --git a/examples/user_playlists.py b/examples/user_playlists.py index 76bf71c..7bcc1f5 100644 --- a/examples/user_playlists.py +++ b/examples/user_playlists.py @@ -13,8 +13,8 @@ import spotipy.util as util if len(sys.argv) > 1: username = sys.argv[1] else: - print "Whoops, need your username!" - print "usage: python user_playlists.py [username]" + print("Whoops, need your username!") + print("usage: python user_playlists.py [username]") sys.exit() token = util.prompt_for_user_token(username) @@ -23,6 +23,6 @@ if token: sp = spotipy.Spotify(auth=token) playlists = sp.user_playlists(username) for playlist in playlists['items']: - print playlist['name'] + print(playlist['name']) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/user_playlists_contents.py b/examples/user_playlists_contents.py index 895e83e..b0bac03 100644 --- a/examples/user_playlists_contents.py +++ b/examples/user_playlists_contents.py @@ -8,15 +8,15 @@ import spotipy.util as util def show_tracks(results): for i, item in enumerate(tracks['items']): track = item['track'] - print " %d %32.32s %s" % (i, track['artists'][0]['name'], track['name']) + print(" %d %32.32s %s" % (i, track['artists'][0]['name'], track['name'])) if __name__ == '__main__': if len(sys.argv) > 1: username = sys.argv[1] else: - print "Whoops, need your username!" - print "usage: python user_playlists.py [username]" + print("Whoops, need your username!") + print("usage: python user_playlists.py [username]") sys.exit() token = util.prompt_for_user_token(username) @@ -27,9 +27,9 @@ if __name__ == '__main__': playlists = sp.user_playlists(username) for playlist in playlists['items']: if playlist['owner']['id'] == username: - print - print playlist['name'] - print ' total tracks', playlist['tracks']['total'] + print() + print(playlist['name']) + print(' total tracks', playlist['tracks']['total']) results = sp.user_playlist(username, playlist['id'], fields="tracks,next") tracks = results['tracks'] show_tracks(tracks) @@ -37,5 +37,5 @@ if __name__ == '__main__': tracks = sp.next(tracks) show_tracks(tracks) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/examples/user_starred_playlist.py b/examples/user_starred_playlist.py index 28435e1..914344a 100644 --- a/examples/user_starred_playlist.py +++ b/examples/user_starred_playlist.py @@ -9,8 +9,8 @@ import spotipy.util as util if len(sys.argv) > 1: username = sys.argv[1] else: - print "Whoops, need your username!" - print "usage: python user_playlists.py [username]" + print("Whoops, need your username!") + print("usage: python user_playlists.py [username]") sys.exit() token = util.prompt_for_user_token(username) @@ -23,9 +23,9 @@ if token: while tracks: for item in tracks['items']: track = item['track'] - print which, track['name' ], ' --', track['artists'][0]['name'] + print(which, track['name' ], ' --', track['artists'][0]['name']) which += 1 tracks = sp.next(tracks) else: - print "Can't get token for", username + print("Can't get token for", username) diff --git a/spotipy/__init__.py b/spotipy/__init__.py index 9339d2a..9be1dce 100644 --- a/spotipy/__init__.py +++ b/spotipy/__init__.py @@ -1,2 +1,2 @@ VERSION='2.0.1' -from client import Spotify, SpotifyException +from .client import Spotify, SpotifyException diff --git a/spotipy/client.py b/spotipy/client.py index 5c2ff21..c926814 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -1,7 +1,7 @@ # coding: utf-8 -from __future__ import print_function +from __future__ import print_function import sys import base64 import requests @@ -18,7 +18,7 @@ class SpotifyException(Exception): self.msg = msg def __str__(self): - return u'http status: {0}, code:{1} - {2}'.format( + return 'http status: {0}, code:{1} - {2}'.format( self.http_status, self.code, self.msg) class Spotify(object): @@ -101,7 +101,9 @@ class Spotify(object): r.raise_for_status() except: raise SpotifyException(r.status_code, - -1, u'%s:\n %s' % (r.url, r.json()['error']['message'])) + -1, '%s:\n %s' % (r.url, r.json()['error']['message'])) + finally: + r.connection.close() if len(r.text) > 0: results = r.json() if self.trace: # pragma: no cover diff --git a/spotipy/oauth2.py b/spotipy/oauth2.py index aa8034e..bb10e32 100644 --- a/spotipy/oauth2.py +++ b/spotipy/oauth2.py @@ -1,12 +1,20 @@ + from __future__ import print_function import base64 -import urllib import requests import os import json import time import sys +# Workaround to support both python 2 & 3 +try: + import urllib.request, urllib.error + import urllib.parse as urllibparse +except ImportError: + import urllib as urllibparse + + class SpotifyOauthError(Exception): pass @@ -54,8 +62,12 @@ class SpotifyClientCredentials(object): """Gets client credentials access token """ payload = { 'grant_type': 'client_credentials'} - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} + if sys.version_info[0] >= 3: # Python 3 + auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) + headers = {'Authorization': 'Basic %s' % auth_header.decode()} + else: # Python 2 + auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) + headers = {'Authorization': 'Basic %s' % auth_header} response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, verify=True) @@ -154,7 +166,7 @@ class SpotifyOAuth(object): if self.state: payload['state'] = self.state - urlparams = urllib.urlencode(payload) + urlparams = urllibparse.urlencode(payload) return "%s?%s" % (self.OAUTH_AUTHORIZE_URL, urlparams) @@ -185,9 +197,12 @@ class SpotifyOAuth(object): if self.state: payload['state'] = self.state - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} - + if sys.version_info[0] >= 3: # Python 3 + auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) + headers = {'Authorization': 'Basic %s' % auth_header.decode()} + else: # Python 2 + auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) + headers = {'Authorization': 'Basic %s' % auth_header} response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, verify=True) @@ -210,8 +225,12 @@ class SpotifyOAuth(object): payload = { 'refresh_token': refresh_token, 'grant_type': 'refresh_token'} - auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) - headers = {'Authorization': 'Basic %s' % auth_header} + if sys.version_info[0] >= 3: # Python 3 + auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode()) + headers = {'Authorization': 'Basic %s' % auth_header.decode()} + else: # Python 2 + auth_header = base64.b64encode(self.client_id + ':' + self.client_secret) + headers = {'Authorization': 'Basic %s' % auth_header} response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers) diff --git a/spotipy/util.py b/spotipy/util.py index 8df4572..e3422f4 100644 --- a/spotipy/util.py +++ b/spotipy/util.py @@ -1,9 +1,10 @@ # shows a user's playlists (need to be authenticated via oauth) +from __future__ import print_function import os import subprocess -import oauth2 +from . import oauth2 import spotipy def prompt_for_user_token(username, scope=None, client_id = None, @@ -32,7 +33,7 @@ def prompt_for_user_token(username, scope=None, client_id = None, redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI') if not client_id: - print ''' + print(''' You need to set your Spotify API credentials. You can do this by setting environment variables like so: @@ -42,7 +43,7 @@ def prompt_for_user_token(username, scope=None, client_id = None, Get your credentials at https://developer.spotify.com/my-applications - ''' + ''') raise spotipy.SpotifyException(550, -1, 'no credentials set') sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, @@ -55,7 +56,7 @@ def prompt_for_user_token(username, scope=None, client_id = None, token_info = sp_oauth.get_cached_token() if not token_info: - print ''' + print(''' User authentication requires interaction with your web browser. Once you enter your credentials and @@ -63,19 +64,23 @@ def prompt_for_user_token(username, scope=None, client_id = None, a url. Paste that url you were directed to to complete the authorization. - ''' + ''') auth_url = sp_oauth.get_authorize_url() try: subprocess.call(["open", auth_url]) - print "Opening %s in your browser" % auth_url + print("Opening %s in your browser" % auth_url) except: - print "Please navigate here: %s" % auth_url + print("Please navigate here: %s" % auth_url) - print - print - response = raw_input("Enter the URL you were redirected to: ") - print - print + print() + print() + try: + response = raw_input("Enter the URL you were redirected to: ") + except NameError: + response = input("Enter the URL you were redirected to: ") + + print() + print() code = sp_oauth.parse_response_code(response) token_info = sp_oauth.get_access_token(code) diff --git a/tests/authtests.py b/tests/authtests.py index 348c30a..b5c7f00 100644 --- a/tests/authtests.py +++ b/tests/authtests.py @@ -57,11 +57,6 @@ class AuthTestSpotipy(unittest.TestCase): def test_user_playlists(self): playlists = spotify.user_playlists(username, limit=5) self.assertTrue('items' in playlists) - - # known API issue currently causes this test to fail - # the issue is that the API doesn't currently respect the - # limit paramter - self.assertTrue(len(playlists['items']) == 5) def test_user_playlist_tracks(self): @@ -174,4 +169,4 @@ if __name__ == '__main__': spotify.trace = False unittest.main() else: - print "Usage: %s username" % (sys.argv[0],) + print("Usage: %s username" % (sys.argv[0],)) diff --git a/tests/client_credentials_tests.py b/tests/client_credentials_tests.py index 767406b..4905062 100644 --- a/tests/client_credentials_tests.py +++ b/tests/client_credentials_tests.py @@ -17,7 +17,7 @@ class ClientCredentialsTestSpotipy(unittest.TestCase): def test_request_with_token(self): artist = spotify.artist(self.muse_urn) - self.assertTrue(artist['name'] == u'Muse') + self.assertTrue(artist['name'] == 'Muse') if __name__ == '__main__': diff --git a/tests/tests.py b/tests/tests.py index 7308e1a..c78233d 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -25,7 +25,7 @@ class TestSpotipy(unittest.TestCase): def test_artist_urn(self): artist = self.spotify.artist(self.radiohead_urn) - self.assertTrue(artist['name'] == u'Radiohead') + self.assertTrue(artist['name'] == 'Radiohead') def test_artists(self): results = self.spotify.artists([self.weezer_urn, self.radiohead_urn]) @@ -34,7 +34,7 @@ class TestSpotipy(unittest.TestCase): def test_album_urn(self): album = self.spotify.album(self.pinkerton_urn) - self.assertTrue(album['name'] == u'Pinkerton') + self.assertTrue(album['name'] == 'Pinkerton') def test_album_tracks(self): results = self.spotify.album_tracks(self.pinkerton_urn) @@ -58,15 +58,15 @@ class TestSpotipy(unittest.TestCase): def test_track_urn(self): track = self.spotify.track(self.creep_urn) - self.assertTrue(track['name'] == u'Creep') + self.assertTrue(track['name'] == 'Creep') def test_track_id(self): track = self.spotify.track(self.creep_id) - self.assertTrue(track['name'] == u'Creep') + self.assertTrue(track['name'] == 'Creep') def test_track_url(self): track = self.spotify.track(self.creep_url) - self.assertTrue(track['name'] == u'Creep') + self.assertTrue(track['name'] == 'Creep') def test_tracks(self): results = self.spotify.tracks([self.creep_url, self.el_scorcho_urn]) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..ffce4a8 --- /dev/null +++ b/tox.ini @@ -0,0 +1,5 @@ +[tox] +envlist = py27,py34 +[testenv] +deps=requests +commands=python -m unittest discover tests