spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

Duplicate playlists created even when I check if the playlist exists #1020

Closed arsaboo closed 1 year ago

arsaboo commented 1 year ago

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