From 651080e3da091990716a6389c51150f4e884f18a Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Tue, 25 Aug 2020 14:37:53 -0400 Subject: [PATCH] Fix playlist add items (#559) * fixed uri issue in playlist_add_items All uris were being converted to track uris making it impossible to add episodes to playlists. * Added tests for episode adds also fixed creep uri so those tests no longer fail * Fixed creep_url to match creep_uri * revert pip version, added FIX to changelog Co-authored-by: Paul Lamere --- CHANGELOG.md | 3 +++ spotipy/client.py | 8 +++++- tests/integration/test_non_user_endpoints.py | 6 ++--- tests/integration/test_user_endpoints.py | 27 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 313ba9e..dd956de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `user_playlist_is_following` in favor of `playlist_is_following` - `playlist_tracks` in favor of `playlist_items` +### Fixed +- fixed issue where episode URIs were being converted to track URIs in playlist calls + ## [2.13.0] - 2020-06-25 ### Added diff --git a/spotipy/client.py b/spotipy/client.py index c389eac..c13be53 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -1824,7 +1824,13 @@ class Spotify(object): return id def _get_uri(self, type, id): - return "spotify:" + type + ":" + self._get_id(type, id) + if self._is_uri(id): + return id + else: + return "spotify:" + type + ":" + self._get_id(type, id) + + def _is_uri(self, uri): + return uri.startswith("spotify:") and len(uri.split(':')) == 3 def _search_multiple_markets(self, q, limit, offset, type, markets, total): if total and limit > total: diff --git a/tests/integration/test_non_user_endpoints.py b/tests/integration/test_non_user_endpoints.py index 7e9c666..02b1a78 100644 --- a/tests/integration/test_non_user_endpoints.py +++ b/tests/integration/test_non_user_endpoints.py @@ -36,9 +36,9 @@ class AuthTestSpotipy(unittest.TestCase): bad_id = 'BAD_ID' - creep_urn = 'spotify:track:3HfB5hBU0dmBt8T0iCmH42' - creep_id = '3HfB5hBU0dmBt8T0iCmH42' - creep_url = 'http://open.spotify.com/track/3HfB5hBU0dmBt8T0iCmH42' + creep_urn = 'spotify:track:6b2oQwSGFkzsMtQruIWm2p' + creep_id = '6b2oQwSGFkzsMtQruIWm2p' + creep_url = 'http://open.spotify.com/track/6b2oQwSGFkzsMtQruIWm2p' el_scorcho_urn = 'spotify:track:0Svkvt5I79wficMFgaqEQJ' el_scorcho_bad_urn = 'spotify:track:0Svkvt5I79wficMFgaqEQK' pinkerton_urn = 'spotify:album:04xe676vyiTeYNXw15o9jT' diff --git a/tests/integration/test_user_endpoints.py b/tests/integration/test_user_endpoints.py index ade2061..ad6aa52 100644 --- a/tests/integration/test_user_endpoints.py +++ b/tests/integration/test_user_endpoints.py @@ -25,6 +25,17 @@ class SpotipyPlaylistApiTest(unittest.TestCase): "spotify:track:1PB7gRWcvefzu7t3LJLUlf"] cls.username = os.getenv(CCEV['client_username']) + # be wary here, episodes sometimes go away forever + # which could cause tests that rely on four_episodes + # to fail + + cls.four_episodes = [ + "spotify:episode:7f9e73vfXKRqR6uCggK2Xy", + "spotify:episode:4wA1RLFNOWCJ8iprngXmM0", + "spotify:episode:32vhLjJjT7m3f9DFCJUCVZ", + "spotify:episode:7cRcsGYYRUFo1OF3RgRzdx", + ] + scope = ( 'playlist-modify-public ' 'user-library-read ' @@ -125,6 +136,22 @@ class SpotipyPlaylistApiTest(unittest.TestCase): playlist = self.spotify.playlist_items(self.new_playlist['id']) self.assertEqual(playlist["total"], 0) + def test_playlist_add_episodes(self): + # add episodes to playlist + self.spotify.playlist_add_items( + self.new_playlist['id'], self.four_episodes) + playlist = self.spotify.playlist_items(self.new_playlist['id']) + self.assertEqual(playlist['total'], 4) + self.assertEqual(len(playlist['items']), 4) + + pl = self.spotify.playlist_items(self.new_playlist['id'], limit=2) + self.assertEqual(len(pl["items"]), 2) + + self.spotify.playlist_remove_all_occurrences_of_items( + self.new_playlist['id'], self.four_episodes) + playlist = self.spotify.playlist_items(self.new_playlist['id']) + self.assertEqual(playlist["total"], 0) + def test_playlist_cover_image(self): # Upload random dog image r = requests.get('https://dog.ceo/api/breeds/image/random')