From 63eebfa9300203e9a50d1ecd2ab3e4502639c21f Mon Sep 17 00:00:00 2001 From: Alisha Ukani Date: Tue, 16 Jan 2018 18:58:21 -0500 Subject: [PATCH] Use client credentials flow to fix broken non-authorized requests --- examples/artist_albums.py | 4 +++- examples/artist_discography.py | 4 +++- examples/search.py | 4 +++- examples/show_album.py | 4 +++- examples/show_artist.py | 4 +++- examples/show_artist_top_tracks.py | 4 +++- examples/show_related.py | 4 +++- examples/show_track_info.py | 4 +++- examples/show_tracks.py | 4 +++- examples/show_user.py | 4 +++- examples/simple0.py | 5 ++++- examples/simple1.py | 4 +++- examples/simple2.py | 4 +++- examples/simple3.py | 4 +++- examples/title_chain.py | 6 +++++- examples/tracks.py | 6 +++++- tests/tests.py | 15 ++++++++++----- 17 files changed, 63 insertions(+), 21 deletions(-) diff --git a/examples/artist_albums.py b/examples/artist_albums.py index 6b9f6ce..220d32a 100644 --- a/examples/artist_albums.py +++ b/examples/artist_albums.py @@ -1,3 +1,4 @@ +from spotipy.oauth2 import SpotifyClientCredentials import sys import spotipy @@ -28,7 +29,8 @@ def show_artist_albums(artist): seen.add(name) if __name__ == '__main__': - sp = spotipy.Spotify() + client_credentials_manager = SpotifyClientCredentials() + sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) if len(sys.argv) < 2: print(('Usage: {0} artist name'.format(sys.argv[0]))) diff --git a/examples/artist_discography.py b/examples/artist_discography.py index 34114bb..d0e8075 100644 --- a/examples/artist_discography.py +++ b/examples/artist_discography.py @@ -1,3 +1,4 @@ +from spotipy.oauth2 import SpotifyClientCredentials import sys import spotipy @@ -47,7 +48,8 @@ def show_artist(artist): print('Genres: ', ','.join(artist['genres'])) if __name__ == '__main__': - sp = spotipy.Spotify() + client_credentials_manager = SpotifyClientCredentials() + sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) sp.trace = False if len(sys.argv) < 2: diff --git a/examples/search.py b/examples/search.py index 6955e4f..adaf6c1 100644 --- a/examples/search.py +++ b/examples/search.py @@ -1,5 +1,6 @@ # shows artist info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -9,6 +10,7 @@ if len(sys.argv) > 1: else: search_str = 'Radiohead' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) result = sp.search(search_str) pprint.pprint(result) diff --git a/examples/show_album.py b/examples/show_album.py index e492b3c..d2daa57 100644 --- a/examples/show_album.py +++ b/examples/show_album.py @@ -1,6 +1,7 @@ # shows album info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -11,6 +12,7 @@ else: urn = 'spotify:album:5yTx83u3qerZF7GRJu7eFk' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) album = sp.album(urn) pprint.pprint(album) diff --git a/examples/show_artist.py b/examples/show_artist.py index 0180b2a..b4c397e 100644 --- a/examples/show_artist.py +++ b/examples/show_artist.py @@ -1,5 +1,6 @@ # shows artist info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -9,7 +10,8 @@ if len(sys.argv) > 1: else: urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) artist = sp.artist(urn) diff --git a/examples/show_artist_top_tracks.py b/examples/show_artist_top_tracks.py index a80beb8..988dba8 100644 --- a/examples/show_artist_top_tracks.py +++ b/examples/show_artist_top_tracks.py @@ -1,5 +1,6 @@ # shows artist info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -9,7 +10,8 @@ if len(sys.argv) > 1: else: urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) response = sp.artist_top_tracks(urn) for track in response['tracks']: diff --git a/examples/show_related.py b/examples/show_related.py index b78e1dc..143bea4 100644 --- a/examples/show_related.py +++ b/examples/show_related.py @@ -1,6 +1,7 @@ # shows related artists for the given seed artist +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -10,7 +11,8 @@ if len(sys.argv) > 1: else: artist_name = 'weezer' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) result = sp.search(q='artist:' + artist_name, type='artist') try: name = result['artists']['items'][0]['name'] diff --git a/examples/show_track_info.py b/examples/show_track_info.py index b286c0d..27522ef 100644 --- a/examples/show_track_info.py +++ b/examples/show_track_info.py @@ -1,5 +1,6 @@ # shows track info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -9,6 +10,7 @@ if len(sys.argv) > 1: else: urn = 'spotify:track:0Svkvt5I79wficMFgaqEQJ' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) track = sp.track(urn) pprint.pprint(track) diff --git a/examples/show_tracks.py b/examples/show_tracks.py index 79a5302..686a3bb 100644 --- a/examples/show_tracks.py +++ b/examples/show_tracks.py @@ -3,6 +3,7 @@ given a list of track IDs show the artist and track name ''' +from spotipy.oauth2 import SpotifyClientCredentials import sys import spotipy @@ -14,7 +15,8 @@ if __name__ == '__main__': file = sys.stdin tids = file.read().split() - sp = spotipy.Spotify() + client_credentials_manager = SpotifyClientCredentials() + sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) 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']: diff --git a/examples/show_user.py b/examples/show_user.py index 07b102f..a6dc4e3 100644 --- a/examples/show_user.py +++ b/examples/show_user.py @@ -1,6 +1,7 @@ # shows artist info for a URN or URL +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys import pprint @@ -10,7 +11,8 @@ if len(sys.argv) > 1: else: username = 'plamere' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) sp.trace = True user = sp.user(username) pprint.pprint(user) diff --git a/examples/simple0.py b/examples/simple0.py index 52540bb..a89ca42 100644 --- a/examples/simple0.py +++ b/examples/simple0.py @@ -1,5 +1,8 @@ +from spotipy.oauth2 import SpotifyClientCredentials import spotipy -sp = spotipy.Spotify() + +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) results = sp.search(q='weezer', limit=20) for i, t in enumerate(results['tracks']['items']): diff --git a/examples/simple1.py b/examples/simple1.py index 1748b24..c161131 100644 --- a/examples/simple1.py +++ b/examples/simple1.py @@ -1,9 +1,11 @@ +from spotipy.oauth2 import SpotifyClientCredentials import spotipy birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP' -spotify = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) results = spotify.artist_albums(birdy_uri, album_type='album') albums = results['items'] diff --git a/examples/simple2.py b/examples/simple2.py index 3b06ddc..ee74fcb 100644 --- a/examples/simple2.py +++ b/examples/simple2.py @@ -1,10 +1,12 @@ +from spotipy.oauth2 import SpotifyClientCredentials import spotipy lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp' -spotify = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) results = spotify.artist_top_tracks(lz_uri) diff --git a/examples/simple3.py b/examples/simple3.py index 96f300a..d8a755a 100644 --- a/examples/simple3.py +++ b/examples/simple3.py @@ -1,7 +1,9 @@ +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys -spotify = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) if len(sys.argv) > 1: name = ' '.join(sys.argv[1:]) diff --git a/examples/title_chain.py b/examples/title_chain.py index 67a55c0..16e7ecc 100644 --- a/examples/title_chain.py +++ b/examples/title_chain.py @@ -1,3 +1,4 @@ +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import random import simplejson as json @@ -5,9 +6,12 @@ import simplejson as json ''' generates a list of songs where the first word in each subsequent song matches the last word of the previous song. + + usage: python title_chain.py [song name] ''' -sp = spotipy.Spotify() +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) skiplist = set(['dm', 'remix']) diff --git a/examples/tracks.py b/examples/tracks.py index 0e3f8c0..33e943e 100644 --- a/examples/tracks.py +++ b/examples/tracks.py @@ -1,9 +1,13 @@ # shows tracks for the given artist +# usage: python tracks.py [artist name] +from spotipy.oauth2 import SpotifyClientCredentials import spotipy import sys -sp = spotipy.Spotify() + +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) if len(sys.argv) > 1: artist_name = ' '.join(sys.argv[1:]) diff --git a/tests/tests.py b/tests/tests.py index 760b91c..af8d352 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -4,6 +4,7 @@ import unittest import pprint import requests from spotipy.client import SpotifyException +from spotipy.oauth2 import SpotifyClientCredentials class TestSpotipy(unittest.TestCase): @@ -23,7 +24,8 @@ class TestSpotipy(unittest.TestCase): bad_id = 'BAD_ID' def setUp(self): - self.spotify = spotipy.Spotify() + client_credentials_manager = SpotifyClientCredentials() + self.spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) def test_artist_urn(self): artist = self.spotify.artist(self.radiohead_urn) @@ -121,11 +123,12 @@ class TestSpotipy(unittest.TestCase): self.assertTrue(found) def test_search_timeout(self): - sp = spotipy.Spotify(requests_timeout=.1) + client_credentials_manager = SpotifyClientCredentials() + sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_timeout=.1) try: results = sp.search(q='my*', type='track') self.assertTrue(False, 'unexpected search timeout') - except requests.ReadTimeout: + except requests.exceptions.Timeout: self.assertTrue(True, 'expected search timeout') @@ -169,12 +172,14 @@ class TestSpotipy(unittest.TestCase): from requests import Session sess = Session() sess.headers["user-agent"] = "spotipy-test" - with_custom_session = spotipy.Spotify(requests_session=sess) + client_credentials_manager = SpotifyClientCredentials() + with_custom_session = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_session=sess) self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx") def test_force_no_requests_session(self): from requests import Session - with_no_session = spotipy.Spotify(requests_session=False) + client_credentials_manager = SpotifyClientCredentials() + with_no_session = spotipy.Spotify(client_credentials_manager=client_credentials_manager, requests_session=False) self.assertFalse(isinstance(with_no_session._session, Session)) self.assertTrue(with_no_session.user(user="akx")["uri"] == "spotify:user:akx")