I keep getting an error when removing songs using the Spotify API

Viewed 32

You guys were very helpful last time I had trouble adding songs with the Spotify API but now I am having a very different problem and I can't seem to figure out why it is happening.

Everything works fine except for when I try to remove songs using their URIs and their positions in the playlists. The code seemed to work when I use test playlists but when I use it on my personal playlist, I keep receiving this JSON response:

{'error': {'status': 400, 'message': 'Could not remove tracks, please check parameters.'}}

Here are the functions I use to create the song lists to be deleted. The 'songs' parameter in the prep for cutting function is an array of song URIs and 'pos' is an array of song positions. The 'songs' parameter in the cut_selected_songs function is the songlist returned from the previous function:

def prep_for_cutting(songs, pos):
  songList = '{\"tracks\":['
  for i in range(len(songs)):
      songList += '{\"uri\":\"' + songs[i] + '\",\"positions\":[' + str(pos[i]) +']},'
  songList = songList[:-1]
  songList += ']}'
  print(songList)
  return songList

def cut_selected_songs(songs, playlist_id):
  ACCESS_TOKEN = input('Provide Access Token: ')
  URL = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks?uris={songs}'
  response = requests.delete(
        URL,
        data = songs,
        headers={
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Authorization": f"Bearer {ACCESS_TOKEN}"
        }
        )   
  response_json = response.json()
  print(response_json)
  print("Songs removed!")

And this is the song list I couldn't remove from my playlist and their positions:

{"tracks":[{"uri":"spotify:track:4TaEBIHkCz3fnVF8gdehSL","positions":[13]},{"uri":"spotify:track:1620rCO9xoE7kug77sS4AJ","positions":[14]},{"uri":"spotify:track:0G7Ik0gmxJBFJKAmK2p1k9","positions":[15]},{"uri":"spotify:track:3btBmFS3dZ1EEh0pEH9imU","positions":[16]},{"uri":"spotify:track:6VPdZLQmoTNu75CrnjhXay","positions":[17]},{"uri":"spotify:track:6rAGFY9D3ah6Lb7fUgbNNH","positions":[18]},{"uri":"spotify:track:3RfTeIrWS8LNrfHYwechtt","positions":[19]},{"uri":"spotify:track:0RchWnEHETINMAyrTrlBmo","positions":[20]},{"uri":"spotify:track:3wWXNV94Ys2vQcFxgnWkut","positions":[21]},{"uri":"spotify:track:5ri4zqtWhG07hIuNNDWP76","positions":[22]},{"uri":"spotify:track:43klOCoUcqtkNWCqQQvh9W","positions":[23]}]}

Now keep in mind, the positions parameter worked fine in my test playlists. And I tried removing the songs from my old playlist without the positions, and that worked fine as well. I have a feeling it might have something to do with my old playlist and how it's had songs added and removed and it's had unplayable tracks and Spotify "episodes" added to it and removed a bunch of times, so I think there is just a lot going on in my old playlist. When I remove the positions parameter it seemed to work fine but I need it to have that positions parameter so that it doesn't remove any song duplicates on that playlist. Is there anything I could be missing here? I can't even recreate the problem in my newer playlists.

0 Answers
Related