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)
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
isTrue
even when the playlist does not exist.Your code
Here is the scope that I am using:
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: