Skip to content

Commit

Permalink
Use new short playlist URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
girst committed Nov 15, 2020
1 parent 3f61ebe commit b96662c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
14 changes: 6 additions & 8 deletions mopidy_spotify/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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}"')
Expand Down Expand Up @@ -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}')
Expand Down
6 changes: 3 additions & 3 deletions tests/test_playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b96662c

Please sign in to comment.