I'm using the Python bindings for the Spotify API to list an artists top tracks and add them to a playlist, but it fails every time, as if it excepts a different type of input.
New_Track_List is a string containing the output of the top tracks lookup as either:
Example: "spotify:track:1pAyyxlkPuGnENdj4g7Y4f, spotify:track:7D2xaUXQ4DGY5JJAdM5mGP, spotify:track:74mG2xIxEUJwHckS0Co6jF, spotify:track:2rjqDPbLlbQRlcj8DVM9kn,"
Using URIs I get this back from the function
sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks=New_Track_List)
Traceback:
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: You can add a maximum of 100 tracks per request.
There's only 20 tracks in the string.
Example: "1pAyyxlkPuGnENdj4g7Y4f, 7D2xaUXQ4DGY5JJAdM5mGP, 74mG2xIxEUJwHckS0Co6jF, 2rjqDPbLlbQRlcj8DVM9kn"
Same traceback output.
sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks="spotify:track:74mG2xIxEUJwHckS0Co6jF")
Trying to add just ONE track, gets me this message:
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: Invalid track uri: spotify:track:s
Same message using only the Track ID, as if it only checks the first letter of the passed string.
sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks="7D2xaUXQ4DGY5JJAdM5mGP")
Error traceback:
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: Invalid track uri: spotify:track:7
This looks like a problem with duck typing in python and bad error message in the api. I guess that the api requires you to send a list and not a string, but doesn't actually check that. The problem is that a string is also iterable.
>>> tracks = "spotify:track:1pAyyxlkPuGnENdj4g7Y4f, spotify:track:7D2xaUXQ4DGY5JJAdM5mGP, spotify:track:74mG2xIxEUJwHckS0Co6jF, spotify:track:2rjqDPbLlbQRlcj8DVM9kn"
>>> len(tracks)
150
and that the api takes actually a list of track ids (not a string of comma separated track uris) and prepends 'spotify:track:' infront of all ids:
>>> tracks = "spotify:track:74mG2xIxEUJwHckS0Co6jF"
>>> ["spotify:track:" + track for track in tracks][0]
'spotify:track:s'
Thus, if you instead give the api a list of track Ids, it might work:
>>> tracks = ["1pAyyxlkPuGnENdj4g7Y4f", "7D2xaUXQ4DGY5JJAdM5mGP"]
>>> ["spotify:track:" + track for track in tracks][0]
'spotify:track:1pAyyxlkPuGnENdj4g7Y4f'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With