Maed
Maed

Reputation: 11

Spotify API get playlist tracks offset parameter not working

I try to get a playlist from the Spotify API that contains more than 100 tracks. Since there is a rate limit of 100, I try to change the offset parameter each request to get the next 100 tracks. I tried the python library spotipy and tekore, but when I change the offset parameter, I still get the first 100 tracks of the playlist. It doesn't seem to work. I followed this post, as well as this. I also tried to add the query to the url, but I still get the first 100 tracks. What am I missing out?

#using tekore
app_token = tk.request_client_token(client_id, client_secret)
tekore = tk.Spotify(app_token)
playlist = tekore.playlist_items(playlist_id=playlist_id, fields=["tracks.items.track.name"], offset=300)

# using spotipy
spotipy = sp.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
response = spotipy.playlist_tracks(playlist_id, fields=["tracks.items.track.name"], offset=400)

#using url
r = requests.get(BASE_URL + 'playlists/' + playlist_id + "?offset=100", headers=headers)

Upvotes: 1

Views: 1237

Answers (1)

JnLlnd
JnLlnd

Reputation: 195

Looking at the JSON returned by the API, I found that the endpoint to get the playlist BASE_URL + 'playlists/' + playlist_id returns info about the playlist and includes the 100 first tracks in the tracks.items array. This endpoint does not support the offset parameter.

To get the remaining tracks, I used the BASE_URL + 'playlists/' + playlist_id + '/tracks?offset=100'. This endpoint supports the limit and offset parameters. It returns the tracks in the items array. All you have to do is loop this API call incrementing offset by 100 until the JSON return an empty items array.

Upvotes: 1

Related Questions