[GH-ISSUE #1020] Duplicate playlists created even when I check if the playlist exists #604

Closed
opened 2026-02-28 00:00:11 +03:00 by kerem · 0 comments
Owner

Originally created by @arsaboo on GitHub (Aug 20, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1020

Describe the bug
I am trying to create a Spotify playlist. Basically, I check if the playlist exists or not. If it exists, just add the tracks to that playlist otherwise create a new one. However, it always creates a new playlist with the same name. The function is part of a project and self.sp is the Spotify object.

The weird thing is playlist_exists is True even when the playlist does not exist.

Your code

    def add_tracks_to_playlist(self, playlist_name, track_uris):
        user_id = self.sp.current_user()['id']
        playlists = self.sp.user_playlists(user_id)
        playlist_exists = False
        for playlist in playlists['items']:
            self._log.debug(f'Processing Playlist {playlist["name"]} '
                            f'with id {playlist["id"]}')
            if playlist['name'].lower() == playlist_name.lower():
                playlist_id = playlist['id']
                playlist_exists = True
                self._log.debug(f'Playlist {playlist_name} exists '
                                f'with id {playlist_id}')
                break
        if not playlist_exists:
            playlist = self.sp.user_playlist_create(user_id, playlist_name)
            playlist_id = playlist['id']
            self._log.debug(f'Playlist {playlist_name} created '
                            f'with id {playlist_id}')
        self._log.debug(f'Adding tracks to playlist {playlist_id}')
        self.sp.user_playlist_add_tracks(user_id, playlist_id, track_uris)

Here is the scope that I am using:

        scope = "user-read-private user-read-email playlist-modify-public " \
                "playlist-modify-private playlist-read-private"

Expected behavior
Duplicate playlists should not be created.

Output
In the debug logs, I can see a playlist_id that is actually created new but it shows as pre-existing:

Processing Playlist Testing with id 693a4GDycGvc0izn0u7H4F
Originally created by @arsaboo on GitHub (Aug 20, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1020 **Describe the bug** I am trying to create a Spotify playlist. Basically, I check if the playlist exists or not. If it exists, just add the tracks to that playlist otherwise create a new one. However, it always creates a new playlist with the same name. The function is part of a project and `self.sp` is the Spotify object. The weird thing is `playlist_exists` is `True` even when the playlist does not exist. **Your code** ```python def add_tracks_to_playlist(self, playlist_name, track_uris): user_id = self.sp.current_user()['id'] playlists = self.sp.user_playlists(user_id) playlist_exists = False for playlist in playlists['items']: self._log.debug(f'Processing Playlist {playlist["name"]} ' f'with id {playlist["id"]}') if playlist['name'].lower() == playlist_name.lower(): playlist_id = playlist['id'] playlist_exists = True self._log.debug(f'Playlist {playlist_name} exists ' f'with id {playlist_id}') break if not playlist_exists: playlist = self.sp.user_playlist_create(user_id, playlist_name) playlist_id = playlist['id'] self._log.debug(f'Playlist {playlist_name} created ' f'with id {playlist_id}') self._log.debug(f'Adding tracks to playlist {playlist_id}') self.sp.user_playlist_add_tracks(user_id, playlist_id, track_uris) ``` Here is the scope that I am using: ```python scope = "user-read-private user-read-email playlist-modify-public " \ "playlist-modify-private playlist-read-private" ``` **Expected behavior** Duplicate playlists should not be created. **Output** In the debug logs, I can see a playlist_id that is actually created new but it shows as pre-existing: ``` Processing Playlist Testing with id 693a4GDycGvc0izn0u7H4F ```
kerem 2026-02-28 00:00:11 +03:00
  • closed this issue
  • added the
    bug
    label
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#604
No description provided.