[GH-ISSUE #524] sp.user_playlist_remove_all_occurrences_of_tracks can't remove local tracks from playlist - http status: 400 #318

Open
opened 2026-02-27 23:21:58 +03:00 by kerem · 4 comments
Owner

Originally created by @raiden-e on GitHub (Jun 30, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/524

Describe the bug
when trying to remove local tracks from a playlist, the uri is invalid
local tracks are those imported from a computer.

consider this playlist:
https://open.spotify.com/playlist/58RURV6kkw77FMF2ByQIe7

To test this you will have to copy the playlist or create one yourself, because of ownership. i can make it collaborative if you like.

"{}" indicate replaced text

Your code

import spotipy
# log in
token = spotipy.util.prompt_for_user_token(
	username,
	scope=str(settings['SpotipySC']),
	client_id=str(settings['SpotipyID']),
	client_secret=str(settings['SpotipyHS']),
	redirect_uri=str(settings['SpotipyRU']))
if token:
	sp = spotipy.Spotify(auth=token)
else:
	print(f"Can't get token for {username}")

# get the playlist (which for arguments sake has no more than 100 tracks)

result = sp.playlist_tracks("58RURV6kkw77FMF2ByQIe7")
tracklist = result["items"]
while result['next']:
	# this is in case the playlist is bigger than 100 items
	result = sp.next(result)
	tracklist.extend(result['items'])
tracks = [] # these tracks are in the spotify database and have an id
local = [] # these tracks only exist in playlists and are on your local hard drive
for x in result:
	if x["is_local"]:
		local.append(x["track"]["uri"]) # local tracks don't have an id so one will need to provide the uri
	else:
		tracks.append(x["track"]["id"])
i = int(0)
while i*100 < len(tracks):
	# again, this is in case there are more than 100 tracks to delete
	sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=tracks[(i*100):((i+1)*100)])
	i += 1
i = int(0)
while i*100 < len(local):
	sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=local[(i*100):((i+1)*100)])
	i += 1

Expected behavior

The playlist should be completly empty, in any case, whether it contains local and/or public tracks

Output

Exception has occurred: SpotifyException
http status: 400, code:-1 - https://api.spotify.com/v1/users/{username}/playlists/20R3fBgDzTLaf0VCuFT9jD/tracks:
JSON body contains an invalid track uri: spotify:track:265
File "{path to scipt}", line 63, in
sp.user_playlist_remove_all_occurrences_of_tracks(user="{username}", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=local[(i*100):((i+1)*100)])

Debugger Values
tracks: []
local: ['spotify:local:Meduza...dit%29:265', 'spotify:local:Exclus...MONGER:266', 'spotify:local:Nextar...+Error:222', 'spotify:local:Marbox...mix%29:390']

Environment:

  • OS: Windows
  • Python version 3.8.3
  • spotipy version 2.12.0
  • Coded in VS Code
Originally created by @raiden-e on GitHub (Jun 30, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/524 **Describe the bug** when trying to remove local tracks from a playlist, the uri is invalid local tracks are those imported from a computer. consider this playlist: [https://open.spotify.com/playlist/58RURV6kkw77FMF2ByQIe7](https://open.spotify.com/playlist/58RURV6kkw77FMF2ByQIe7) To test this you will have to copy the playlist or create one yourself, because of ownership. i can make it collaborative if you like. "{}" indicate replaced text **Your code** ``` import spotipy # log in token = spotipy.util.prompt_for_user_token( username, scope=str(settings['SpotipySC']), client_id=str(settings['SpotipyID']), client_secret=str(settings['SpotipyHS']), redirect_uri=str(settings['SpotipyRU'])) if token: sp = spotipy.Spotify(auth=token) else: print(f"Can't get token for {username}") # get the playlist (which for arguments sake has no more than 100 tracks) result = sp.playlist_tracks("58RURV6kkw77FMF2ByQIe7") tracklist = result["items"] while result['next']: # this is in case the playlist is bigger than 100 items result = sp.next(result) tracklist.extend(result['items']) tracks = [] # these tracks are in the spotify database and have an id local = [] # these tracks only exist in playlists and are on your local hard drive for x in result: if x["is_local"]: local.append(x["track"]["uri"]) # local tracks don't have an id so one will need to provide the uri else: tracks.append(x["track"]["id"]) i = int(0) while i*100 < len(tracks): # again, this is in case there are more than 100 tracks to delete sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=tracks[(i*100):((i+1)*100)]) i += 1 i = int(0) while i*100 < len(local): sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=local[(i*100):((i+1)*100)]) i += 1 ``` **Expected behavior** The playlist should be completly empty, in any case, whether it contains local and/or public tracks **Output** > Exception has occurred: SpotifyException http status: 400, code:-1 - https://api.spotify.com/v1/users/{username}/playlists/20R3fBgDzTLaf0VCuFT9jD/tracks: JSON body contains an invalid track uri: spotify:track:265 File "{path to scipt}", line 63, in <module> sp.user_playlist_remove_all_occurrences_of_tracks(user="{username}", playlist_id="58RURV6kkw77FMF2ByQIe7", tracks=local[(i*100):((i+1)*100)]) _Debugger Values_ _tracks:_ [] _local:_ ['spotify:local:Meduza...dit%29:265', 'spotify:local:Exclus...MONGER:266', 'spotify:local:Nextar...+Error:222', 'spotify:local:Marbox...mix%29:390'] **Environment:** - OS: Windows - Python version 3.8.3 - spotipy version 2.12.0 - Coded in VS Code
Author
Owner

@stephanebruckert commented on GitHub (Jul 5, 2020):

JSON body contains an invalid track uri: spotify:track:265

This does look like an invalid track URI, as if it was truncated. Can you share the full track URI that blocks your script?

I see you are generating track ids? tracks=local[(i*100):((i+1)*100)])

Track uris cannot be invented, they must match to real track uris?

<!-- gh-comment-id:653865051 --> @stephanebruckert commented on GitHub (Jul 5, 2020): > JSON body contains an invalid track uri: spotify:track:265 This does look like an invalid track URI, as if it was truncated. Can you share the full track URI that blocks your script? I see you are generating track ids? `tracks=local[(i*100):((i+1)*100)])` Track uris cannot be invented, they must match to real track uris?
Author
Owner

@raiden-e commented on GitHub (Jul 6, 2020):

tracks=local[(i*100):((i+1)*100)] this is to chop up all uri's into chunks of 100, since spotify API limits 100 uris per request. It works with official tracks on spotify so i guess the splice-syntax should be correct

<!-- gh-comment-id:654451623 --> @raiden-e commented on GitHub (Jul 6, 2020): `tracks=local[(i*100):((i+1)*100)]` this is to chop up all uri's into chunks of 100, since spotify API limits 100 uris per request. It works with official tracks on spotify so i guess the splice-syntax should be correct
Author
Owner

@raiden-e commented on GitHub (Jul 6, 2020):

Here is the data from debugger again along with the local variable. tracks is an empty list because all of the titles are is_local

Exception has occurred: SpotifyException
http status: 400, code:-1 - https://api.spotify.com/v1/users/raiden_e/playlists/4sm8XVbuB39QmY1toMILyi/tracks:
JSON body contains an invalid track uri: spotify:track:265
File "C:\Users\Raiden\source\repos\SpotyLoad\SpotyLoad.py", line 67, in
sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id=testset, tracks=local[(i*100):((i+1)*100)])

local: [''spotify:local:Meduza%2FOddprophet:Lose+Control+%28Oddprophet+Edit%29:Lose+Control+%28Oddprophet+Edit%29:265'', ''spotify:local:Exclusion::WARMONGER:266'', ''spotify:local:Nextars::Fatal+Error:222'', ''spotify:local:Marbox:This+Time:This+Time+%28Darpa+Remix%29:390'']

<!-- gh-comment-id:654458350 --> @raiden-e commented on GitHub (Jul 6, 2020): Here is the data from debugger again along with the `local` variable. `tracks` is an empty list because all of the titles are `is_local` > Exception has occurred: SpotifyException > http status: 400, code:-1 - https://api.spotify.com/v1/users/raiden_e/playlists/4sm8XVbuB39QmY1toMILyi/tracks: > JSON body contains an invalid track uri: spotify:track:265 > File "C:\Users\Raiden\source\repos\SpotyLoad\SpotyLoad.py", line 67, in <module> > sp.user_playlist_remove_all_occurrences_of_tracks(user="raiden_e", playlist_id=testset, tracks=local[(i*100):((i+1)*100)]) `local: [''spotify:local:Meduza%2FOddprophet:Lose+Control+%28Oddprophet+Edit%29:Lose+Control+%28Oddprophet+Edit%29:265'', ''spotify:local:Exclusion::WARMONGER:266'', ''spotify:local:Nextars::Fatal+Error:222'', ''spotify:local:Marbox:This+Time:This+Time+%28Darpa+Remix%29:390'']`
Author
Owner

@OberstVonGatow commented on GitHub (Nov 17, 2020):

same error here. Tried to remove recently played tracks from playlist.
With method current_user_recently_played i get the tracks and with current_user_playlists i get my playlists with ids.
Then i get one playlist and try to remove with playlist_remove_all_occurrences_of_items( playlist['id'], recently_played).

File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 1084, in playlist_remove_all_occurrences_of_items "playlists/%s/tracks" % (plid), payload=payload File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 294, in _delete return self._internal_call("DELETE", url, payload, kwargs) File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 264, in _internal_call headers=response.headers, spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/6NWB2JKrlJnHErAGkzGvBV/tracks: JSON body contains an invalid track uri: spotify:track:items, reason: None

<!-- gh-comment-id:729138173 --> @OberstVonGatow commented on GitHub (Nov 17, 2020): same error here. Tried to remove recently played tracks from playlist. With method current_user_recently_played i get the tracks and with current_user_playlists i get my playlists with ids. Then i get one playlist and try to remove with playlist_remove_all_occurrences_of_items( playlist['id'], recently_played). ` File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 1084, in playlist_remove_all_occurrences_of_items "playlists/%s/tracks" % (plid), payload=payload File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 294, in _delete return self._internal_call("DELETE", url, payload, kwargs) File "/home/pi/.local/lib/python2.7/site-packages/spotipy/client.py", line 264, in _internal_call headers=response.headers, spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/6NWB2JKrlJnHErAGkzGvBV/tracks: JSON body contains an invalid track uri: spotify:track:items, reason: None `
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#318
No description provided.