pythonspotify

Spotify API get tracks information from playlist with over 100 tracks using Python


I use plain Python to access the Spotify API. What I want, is to get specific information (song name, artist, album etc.) from the playlists of my account. I have managed to do that, but for playlists with less than 100 songs. Spotify API has a limit of 100 songs to its get requests.

Searching for possible solutions, I found that making a get request at the BASE_URL/playlists/playlist_id/tracks URL, I can use an offset parameter, so that the get request will respond with songs from the offset and on. For example, if I use offset = 100, that will result in a URL BASE_URL/playlists/playlist_id/tracks?offset=100 which should bring 100 songs starting from the 100th. This is also what the documentation of the API says. Doing that itetaratively, I can get all songs.

Although, doing that request, I get a response with the same first 100 songs. It is like that the offset parameter is ignored.

Any ideas?


Solution

  • I found a solution. Both Get Playlist Items and Get Playlist respond with a next variable, which is the link to the next 100 tracks of the playlist, or any tracks that are left if not 100, or it gets the value None when there are no more left. In the first case, is it visible immediately in the response, while in the latter, it is nested under the tracks object (tracks.next).

    I got all tracks by iteratively getting responded tracks until next was None, and using every time the URL returned in the response .

    Cheers!