mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-04-27 08:35:49 +03:00
[GH-ISSUE #763] 405 Client Error when using "playlist_remove_all_occurrences_of_items" #463
Labels
No labels
api-bug
bug
dependencies
documentation
duplicate
enhancement
external-ide
headless-mode
implicit-grant-flow
invalid
missing-endpoint
pr-welcome
private-api
pull-request
question
spotipy3
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/spotipy#463
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Ovcheric on GitHub (Dec 31, 2021).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/763
Trying to remove tracks from a playlist from a list that has all the URI's of the songs I want removed.
When calling the "playlist_remove_all_occurrences_of_items" I get the following error:
I am calling the function in this way:
sp.playlist_remove_all_occurrences_of_items(targetPlaylistID, savedURIs)Where
targetPlaylistIDis the full ID of the playlist from which I want the songs removed from, andsavedURIsis a list of all the URI's I want removed from the target playlist.For example,
At the start of my program, I do have the
playlist-modify-publicandplaylist-modify-privatescopes. I have also used the same playlistID to save all tracks URI from the playlist to a local text file, so I know I have the authorization flow correct.Why am I getting the 405 error? All I want to do is remove every occurrence of the tracks saved in the
savedURIsfrom the target playlist.@Peter-Schorn commented on GitHub (Dec 31, 2021):
50uWPcNFdJElMVZWo0IebB?si=fe98d76d56314a7ais not a valid id. It should be50uWPcNFdJElMVZWo0IebB. A?character denotes the beginning of the query string in a URL, which is why you are getting this error:["2Vc6NJ9PW9gD9q343XFRKx","1s70cjkrdj9lpEeQQlmS9l"]are not URIs. These are URIs:["spotify:track:2Vc6NJ9PW9gD9q343XFRKx", "spotify:track:1s70cjkrdj9lpEeQQlmS9l"].@Ovcheric commented on GitHub (Dec 31, 2021):
Thanks for the quick response. It worked!
If the first ID is not a valid ID, then how come it worked when I passed it to
playlist_items(playlistID)? Also even if I dont have thespotify:track:at the beginning of the URI, it still removes the correct songs.If I remove everything past the "?" in the playlist ID, and pass it to the
playlist_items(playlistID)then I get a key error. This does not happen if I keep everything after the "?". Its not a big deal because I can just use a different ID for different purposes, but from a conceptual POV, do you know why that is?@Peter-Schorn commented on GitHub (Dec 31, 2021):
The
playlist_itemsmethod uses the/playlists/{playlist_id}/tracksendpoint. Endpoints are specified by the path components of a URL.When you insert the invalid ID
50uWPcNFdJElMVZWo0IebB?si=fe98d76d56314a7a, the full URL becomeshttps://api.spotify.com/v1/playlists/50uWPcNFdJElMVZWo0IebB?si=fe98d76d56314a7a/tracks. Everything after the?is part of the query string, including/tracks. Read about the structure of a URL here. Therefore, you're actually calling the/playlist/{playlist_id}endpoint, which corresponds to theSpotify.playlistmethod in spotipy. This is why adding/removing?si=fe98d76d56314a7achanges the structure of the JSON response and can result in a key error in your code.Decide which endpoint you really want to use: the
/playlists/{playlist_id}/tracksendpoint or the/playlist/{playlist_id}endpoint, and then pass a valid ID into theplaylist_itemsorplaylistmethod of spotipy, respectively.If the string has a
?in it, then it's not a valid ID. IDs can only have alphanumeric characters in them. I can't be any more clear about it than that. See here.spotipy will automatically prefix IDs with
spotify:track:when you pass them in to certain methods. However, it's still a bad idea to have a variable calledsavedURIsthat actually contains IDs.@Ovcheric commented on GitHub (Dec 31, 2021):
Im not sure if "JSON dump" is the correct term for the huge nested dictionary/list/array that you get from
playlist_items().Also its not really a bug, but you said that
["2Vc6NJ9PW9gD9q343XFRKx","1s70cjkrdj9lpEeQQlmS9l"]are not URIs. But when I pass this list to a function it still works.For example,
In this case,
savedURIs = ["2Vc6NJ9PW9gD9q343XFRKx","1s70cjkrdj9lpEeQQlmS9l"].As of right now, everything works exactly as I want it to, except that when calling the
save_track_namesfunction I need to pass the ID with the "?" such as50uWPcNFdJElMVZWo0IebB?si=fe98d76d56314a7aotherwise I get aKey Error, and when calling theremove_songsfunction, I need to pass the ID without the "?" such as50uWPcNFdJElMVZWo0IebB@Peter-Schorn commented on GitHub (Dec 31, 2021):
Your
remove_songsfunction is extremely bizarre. If you want to create a list with one item, then just write:uri = [track]. It never makes sense to use theinsertmethod on an empty list. More importantly, though, why are you removing the tracks one at a time? You're slowing down your code by several orders of magnitude by making a separate API call for each track. theplaylist_remove_all_occurrences_of_itemsmethod accepts a list of items for a reason. The maximum length is 100. Trying to remove an item that does not exist in the playlist will NOT cause an error.@Ovcheric commented on GitHub (Dec 31, 2021):
Yeah my lack of python experience is showing haha... I'm very proficient in C++ so I had to learn python from scratch for this program.
I'll definitely change my code to speed it up. I was passing a single uri because the text file contains thousands of tracks at this point. I'll update it to pass in uris in increments of 100.
Thanks for you help!
@Peter-Schorn commented on GitHub (Dec 31, 2021):
I strongly discourage you from using incorrect variable names. With this line, you've just changed the URI to an ID.
track['name']is already a string and so istrack['artists'][0]['name']andtrack['uri']. Passing these into thestrinitializer has no effect.@Peter-Schorn commented on GitHub (Dec 31, 2021):
Change
sp.playlist_itemstosp.playlist, and then make sure you pass in valid IDs.@stephanebruckert commented on GitHub (Jul 9, 2024):
This seems resolved