Proper implementation for Tidal's add_to_playlist and remove_from_playlist methods

- Using tidalapi's `UserPlaylist.add` and `UserPlaylist.delete` methods
  instead of defining my own through `_api_request`, so we won't have to
  deal with the logic to set the ETag header.

- Added `remove_from_playlist` method.
This commit is contained in:
Fabio Manganiello 2022-09-18 05:22:12 +02:00
parent 7c610adc84
commit 61cda60751
Signed by: blacklight
GPG Key ID: D90FBA7F76362774
2 changed files with 33 additions and 17 deletions

View File

@ -36,7 +36,7 @@ class MusicTidalPlugin(RunnablePlugin):
Requires: Requires:
* **tidalapi** (``pip install tidalapi``) * **tidalapi** (``pip install 'tidalapi >= 0.7.0'``)
""" """
@ -186,7 +186,8 @@ class MusicTidalPlugin(RunnablePlugin):
:param playlist_id: ID of the playlist to delete. :param playlist_id: ID of the playlist to delete.
""" """
self._api_request(url=f'playlists/{playlist_id}', method='delete') pl = self.user.playlist(playlist_id)
pl.delete()
@action @action
def edit_playlist(self, playlist_id: str, title=None, description=None): def edit_playlist(self, playlist_id: str, title=None, description=None):
@ -207,7 +208,6 @@ class MusicTidalPlugin(RunnablePlugin):
:return: .. schema:: tidal.TidalPlaylistSchema(many=True) :return: .. schema:: tidal.TidalPlaylistSchema(many=True)
""" """
ret = self.user.playlists() + self.user.favorites.playlists() ret = self.user.playlists() + self.user.favorites.playlists()
return TidalPlaylistSchema().dump(ret, many=True) return TidalPlaylistSchema().dump(ret, many=True)
@action @action
@ -307,25 +307,41 @@ class MusicTidalPlugin(RunnablePlugin):
return self.session.track(track_id).get_url() return self.session.track(track_id).get_url()
@action @action
def add_to_playlist(self, playlist_id: str, track_ids: Iterable[str]): def add_to_playlist(self, playlist_id: str, track_ids: Iterable[Union[str, int]]):
""" """
Append one or more tracks to a playlist. Append one or more tracks to a playlist.
:param playlist_id: Target playlist ID. :param playlist_id: Target playlist ID.
:param track_ids: List of track IDs to append. :param track_ids: List of track IDs to append.
""" """
return self._api_request( pl = self.user.playlist(playlist_id)
url=f'playlists/{playlist_id}/items', pl.add(track_ids)
method='post',
headers={ @action
'If-None-Match': None, def remove_from_playlist(
}, self,
data={ playlist_id: str,
'onArtifactNotFound': 'SKIP', track_id: Optional[Union[str, int]] = None,
'onDupes': 'SKIP', index: Optional[int] = None,
'trackIds': ','.join(map(str, track_ids)), ):
}, """
) Remove a track from a playlist.
Specify either the ``track_id`` or the ``index``.
:param playlist_id: Target playlist ID.
:param track_id: ID of the track to remove.
:param index: Index of the track to remove.
"""
assert not (
track_id is None and index is None
), 'Please specify either track_id or index'
pl = self.user.playlist(playlist_id)
if index:
pl.remove_by_index(index)
if track_id:
pl.remove_by_id(track_id)
@action @action
def add_track(self, track_id: Union[str, int]): def add_track(self, track_id: Union[str, int]):

View File

@ -4,6 +4,6 @@ manifest:
is updated. is updated.
install: install:
pip: pip:
- tidalapi - tidalapi >= 0.7.0
package: platypush.plugins.music.tidal package: platypush.plugins.music.tidal
type: plugin type: plugin