From b96662cd399bba917cfb93de6e3fc2d1260bd28a Mon Sep 17 00:00:00 2001 From: girst Date: Sun, 15 Nov 2020 15:44:30 +0100 Subject: [PATCH] Use new short playlist URIs https://developer.spotify.com/community/news/2018/06/12/changes-to-playlist-uris/ https://developer.spotify.com/community/news/2019/01/15/update-changes-to-playlist-uris/ --- mopidy_spotify/playlists.py | 14 ++++++-------- tests/test_playlists.py | 6 +++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/mopidy_spotify/playlists.py b/mopidy_spotify/playlists.py index c7cb483a..21711d70 100644 --- a/mopidy_spotify/playlists.py +++ b/mopidy_spotify/playlists.py @@ -54,10 +54,8 @@ def _get_playlist(self, uri, as_items=False): ) @staticmethod - def _get_user_and_playlist_id_from_uri(uri): - user_id = uri.split(':')[-3] - playlist_id = uri.split(':')[-1] - return user_id, playlist_id + def _get_playlist_id_from_uri(uri): + return uri.split(':')[-1] @staticmethod def partitions(lst, n=_chunk_size): @@ -118,8 +116,8 @@ def _replace_playlist(self, playlist, tracks): self._playlist_edit(playlist, method=method, uris=uris) def _playlist_edit(self, playlist, method, **kwargs): - user_id, playlist_id = self._get_user_and_playlist_id_from_uri(playlist.uri) - url = f'users/{user_id}/playlists/{playlist_id}/tracks' + playlist_id = self._get_playlist_id_from_uri(playlist.uri) + url = f'playlists/{playlist_id}/tracks' method = getattr(self._backend._web_client, method.lower()) if not method: raise AttributeError(f'Invalid HTTP method "{method}"') @@ -197,8 +195,8 @@ def save(self, playlist): # Playlist rename logic if playlist.name != saved_playlist.name: logger.info(f'Renaming playlist [{saved_playlist.name}] to [{playlist.name}]') - user_id, playlist_id = self._get_user_and_playlist_id_from_uri(saved_playlist.uri) - self._backend._web_client.put(f'users/{user_id}/playlists/{playlist_id}', + playlist_id = self._get_playlist_id_from_uri(saved_playlist.uri) + self._backend._web_client.put(f'playlists/{playlist_id}', json={'name': playlist.name}) self._backend._web_client.remove_from_cache("me/playlists") self._backend._web_client.remove_from_cache(f'playlists/{playlist_id}') diff --git a/tests/test_playlists.py b/tests/test_playlists.py index b4e710f0..c2fac5e0 100644 --- a/tests/test_playlists.py +++ b/tests/test_playlists.py @@ -133,9 +133,9 @@ def request(method, path, *, json=None): method = method.lower() path_parts = path.split('/') web_client_mock._test_requests_history.append(method) - if re.fullmatch(r"users/(.*?)/playlists/(.*?)/tracks", path): - user_id, playlist_id = path_parts[1], path_parts[3] - playlist_uri = f'spotify:user:{user_id}:playlist:{playlist_id}' + if re.fullmatch(r"playlists/(.*?)/tracks", path): + playlist_id = path_parts[1] + playlist_uri = next((k for k in web_playlists_map.keys() if k.endswith(f':playlist:{playlist_id}'))) rv = _edit_playlist(method, playlist_uri, json) return mock.Mock(status_ok=rv) elif re.fullmatch(r"users/(.*?)/playlists", path):