[GH-ISSUE #601] Avoid duplicate tracks on a playlist #359

Closed
opened 2026-02-27 23:22:11 +03:00 by kerem · 6 comments
Owner

Originally created by @AlexandreAguido on GitHub (Oct 31, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/601

This is an awesome project, it saved me hours on coverting a radio history to a playlist,
but I did not find a way to avoid adding duplicate tracks to a playlist. I think that a native option to avoid duplicate
tracks on a playlist would be nice

Originally created by @AlexandreAguido on GitHub (Oct 31, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/601 This is an awesome project, it saved me hours on coverting a radio history to a playlist, but I did not find a way to avoid adding duplicate tracks to a playlist. I think that a native option to avoid duplicate tracks on a playlist would be nice
kerem 2026-02-27 23:22:11 +03:00
Author
Owner

@stephanebruckert commented on GitHub (Oct 31, 2020):

I agree, ideally the API would allow this https://github.com/spotify/web-api/issues/118, but it's closed as wont-fix.

The current recommended practice is that each app keeps tracks of the current tracks in a playlist, and check whether it's already there before inserting.

Unfortunately, it could be hard to do this in the library, because we don't know in advance how to persist the current playlist tracks (what DB? what structure?). The next option would be to retrieve the full playlist every time we call add_track_to_playlist(), but that could be misused and lead to poor performance (a playlist can have up to 11k tracks).

I'm going to keep this open for a bit in case someone can suggest a good solution, otherwise we will likely close it. Also keeping in mind that spotipy is "light weight".

<!-- gh-comment-id:719970761 --> @stephanebruckert commented on GitHub (Oct 31, 2020): I agree, ideally the API would allow this https://github.com/spotify/web-api/issues/118, but it's closed as wont-fix. The current recommended practice is that each app keeps tracks of the current tracks in a playlist, and check whether it's already there before inserting. Unfortunately, it could be hard to do this in the library, because we don't know in advance how to persist the current playlist tracks (what DB? what structure?). The next option would be to retrieve the full playlist every time we call `add_track_to_playlist()`, but that could be misused and lead to poor performance (a playlist can have up to 11k tracks). I'm going to keep this open for a bit in case someone can suggest a good solution, otherwise we will likely close it. Also keeping in mind that spotipy is "light weight".
Author
Owner

@Peter-Schorn commented on GitHub (Nov 1, 2020):

One suggestion I have is to cache the snapshot_id of a playlist, which changes each time the playlist changes. Think of it as like a hash code. This prevents you from having to retrieve all of the tracks in a playlist to see if it has changed. See https://developer.spotify.com/documentation/general/guides/working-with-playlists/#version-control-and-snapshots

You can use the following to retrieve just the snapshot id of a playlist:

snapshot_id = spotify.playlist("37i9dQZF1DXdLtD0qszB1w", fields="snapshot_id")["snapshot_id"]
<!-- gh-comment-id:720005356 --> @Peter-Schorn commented on GitHub (Nov 1, 2020): One suggestion I have is to cache the `snapshot_id` of a playlist, which changes each time the playlist changes. Think of it as like a hash code. This prevents you from having to retrieve all of the tracks in a playlist to see if it has changed. See https://developer.spotify.com/documentation/general/guides/working-with-playlists/#version-control-and-snapshots You can use the following to retrieve just the snapshot id of a playlist: ```python snapshot_id = spotify.playlist("37i9dQZF1DXdLtD0qszB1w", fields="snapshot_id")["snapshot_id"] ```
Author
Owner

@stephanebruckert commented on GitHub (Nov 1, 2020):

@Peter-Schorn isn't the snapshot_id changing even when a duplicate is added?

<!-- gh-comment-id:720076184 --> @stephanebruckert commented on GitHub (Nov 1, 2020): @Peter-Schorn isn't the `snapshot_id` changing even when a duplicate is added?
Author
Owner

@Peter-Schorn commented on GitHub (Nov 1, 2020):

@Peter-Schorn isn't the snapshot_id changing even when a duplicate is added?

Yes, it will change even if a duplicate is added. And, even if you add a track and then remove that same track, the snapshot id will still change to a unique value each time. However, if the user hasn't modified the playlist at all, then this parameter can still be useful for avoiding retrieving all of the tracks again. Admittedly, this isn't a direct solution to @AlexandreAguido 's question, but I thought it would still be worth mentioning.

<!-- gh-comment-id:720076909 --> @Peter-Schorn commented on GitHub (Nov 1, 2020): > @Peter-Schorn isn't the `snapshot_id` changing even when a duplicate is added? Yes, it will change even if a duplicate is added. And, even if you add a track and then remove that same track, the snapshot id will *still* change to a unique value each time. However, if the user hasn't modified the playlist at all, then this parameter can still be useful for avoiding retrieving all of the tracks again. Admittedly, this isn't a direct solution to @AlexandreAguido 's question, but I thought it would still be worth mentioning.
Author
Owner

@AlexandreAguido commented on GitHub (Nov 1, 2020):

Well I opened this issue because I thought that the spotify api could easily handle the option to add or avoid duplicate songs, but sadly they don't support it.

A way to manage it without depending of spotify api would be to keep track of the ids for each track in the playlist and the snapshot_id then check if the snapshot_id changed before downloading the playlist info and finally check with tracks don't belong to a playlist before insert them. But for a web app with hundreds of users that depends of this project it will increase significantly the resouce usage (space, bandwith, and CPU resource for the search operations) breaking the purpose of this project to be a "lightweight"

I say leave it as it is and hope that something like this https://github.com/spotify/web-api/issues/444 be implemented quickly.

<!-- gh-comment-id:720101561 --> @AlexandreAguido commented on GitHub (Nov 1, 2020): Well I opened this issue because I thought that the spotify api could easily handle the option to add or avoid duplicate songs, but sadly they don't support it. A way to manage it without depending of spotify api would be to keep track of the ids for each track in the playlist and the snapshot_id then check if the snapshot_id changed before downloading the playlist info and finally check with tracks don't belong to a playlist before insert them. But for a web app with hundreds of users that depends of this project it will increase significantly the resouce usage (space, bandwith, and CPU resource for the search operations) breaking the purpose of this project to be a "lightweight" I say leave it as it is and hope that something like this https://github.com/spotify/web-api/issues/444 be implemented quickly.
Author
Owner

@stephanebruckert commented on GitHub (Nov 1, 2020):

yep... closing then. Thanks for reporting though, I share the idea.

<!-- gh-comment-id:720101928 --> @stephanebruckert commented on GitHub (Nov 1, 2020): yep... closing then. Thanks for reporting though, I share the idea.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#359
No description provided.