python-tidal
python-tidal copied to clipboard
[Question] Remove multiple items from a playlist
Hello, in my Tidal Plugin for Upmpdcli, I have implement a "Listen Queue", leveraging a Playlist with a given name. While adding multiple items (the tracks of an album) to the playlist is fast (see this issue) using this method of your library:
def add(self, media_ids: List[str]) -> None:
... I tried the same for deleting tracks, using the
def _calculate_id(self, media_id: str) -> Optional[int]:
however, this does not look as fast as the "add" solution. Can the _calculate_id method result in multiple api calls if the playlist is long enough, in term of tracks it contains? And also, the fact that it has an underscore, means that it is a methods that I cannot rely on in the future, with upcoming releases?
Thank you!
Hello, I think this cannot work, because indexes would shift after deletion... I would probably need and api that would accept a list of ids but that does not seem to match any real use case for the tidal applications. So probably I will re-implement my Listening Queue using the local database. Just to clarify, I use the "Listening Queue" when I stumble upon an album which I'd like to listen when I've got time, but I am just not sure to want it among the Favorites. I don't really know if this is a common use case... but happens to me quite frequently.
I wrote some code for this in my repo spotify-to-tidal:
def _remove_indices_from_playlist(playlist: tidalapi.UserPlaylist, indices: List[int]):
headers = {'If-None-Match': playlist._etag}
index_string = ",".join(map(str, indices))
playlist.request.request('DELETE', (playlist._base_url + '/items/%s') % (playlist.id, index_string), headers=headers)
playlist._reparse()
def clear_tidal_playlist(playlist: tidalapi.UserPlaylist, chunk_size: int=20):
with tqdm(desc="Erasing existing tracks from Tidal playlist", total=playlist.num_tracks) as progress:
while playlist.num_tracks:
indices = range(min(playlist.num_tracks, chunk_size))
_remove_indices_from_playlist(playlist, indices)
progress.update(len(indices))
Thank you! Your code will be valuable for features I might include in the future. For the reason I opened this initially, this code would result in slow operation because of the multiple calls to that "DELETE" endpoint. For this reason I implemented the features called "Bookmarks" in my Tidal Plugin for upmpdcli using a local database. So currently I do not implement any playlist action in the plugin, but that is something I'd like to implement in the near future. So thank you again for your contribution.