spotifyr
spotifyr copied to clipboard
get_playlist_tracks() limit is set pretty low
According to the documentation the limit parameter can range from 1 to 100.
This is a bit low since many playlists have several hundred entries. Is there a way to increase this or is this a limitation of the spotify API?
Thanks!
EDIT: I just realized how the pagination system of the spotify API works, see: https://developer.spotify.com/documentation/web-api/
Its pretty straight forward to parse through the whole playlist by increasing the offset parameter as long as there non-null list returns. The issue can be closed :-). But maybe adding an example in the documentation would be helpful for future users.
Btw.: thanks for building this package! Awesome work!
Documentation pull request or examples are more than welcome.
I can try... Would be my first pull request tho 😅
Is a tidyverse and purrr centric example okay or should it be base-R?
Maybe you can write the example here and we'll see if it will be an illustration or an example or something else. Not the entire tidyverse is imported into spotifyr.
How about this:
library(tidyverse)
library(purrr)
library(spotifyr)
# The get_playlist_tracks() function can only fetch 100 tracks each time it is called.
# This is a limitation of the Spotify API.
# We use the popular Lofi Hiphop Playlist as example. It has currently 300 entries.
playlist_id <- "0vvXsWCC9xrXsKd4FyS8kM"
# As we can see only the first 100 tracks are fetched
nrow(get_playlist_tracks(playlist_id))
# To get all the other tracks we need to increase the offset parameter
# of the function, while leaving the limit parameter at its default (100).
# Note that the playlist entries are identified with indices using zero based numbering.
# This simply means that the first index starts at 0 instead on 1 as we are accustomed in R.
# The first one hundred tracks of the playlist correspond to an index ranging from {0,1,2, ... , 99}.
# To get the next one hundred tracks in the playlist we need the indices ranging from {100, 101, 102, ... , 199}.
# This is achieved by setting the offset parameter 100 to fetch the next hundred tracks
# then setting the offset parameter to 200 and so on.
# We create an arbitrarily long vector of offset values starting with 0
offset_vector <- c(0,seq(from=100, to = 10000, by = 100))
# This vector is then used to create a dataframe we will use to iterate.
fetched_playlist <- data.frame(offset = offset_vector) %>%
# We now iterate through all the values of the offset vector using the map
# function of the purrr package and call the get_playlist_tracks() function.
mutate(results = map(offset,
~get_playlist_tracks(
playlist_id,
limit=100,
offset = .x),
)) %>%
# To bind each fetched list element together we select the results column and unnest it
select(results) %>%
unnest(results)
# We now have a dataframe containing all the tracks in the playlist
glimpse(fetched_playlist)
I could create a Rmarkdown file that uses this approach to find out which tempo or key dominates the playlist. This is btw. the use case I used your package for.
Hmm tried running this code exactly and I got the following error:
Error in `list_unchop()`:
! Can't combine `x[[1]]` <data.frame> and `x[[6]]` <list>.
Did you encounter anything similar?
``Hi @eledroos ,
Just tried the code again. You are right. Something in the purrr package might have changed.
This should work:
library(tidyverse)
library(purrr)
library(spotifyr)
# The get_playlist_tracks() function can only fetch 100 tracks each time it is called.
# This is a limitation of the Spotify API.
# We use the popular Lofi Hiphop Playlist as example. It has currently 300 entries.
playlist_id <- "0vvXsWCC9xrXsKd4FyS8kM"
# As we can see only the first 100 tracks are fetched
nrow(get_playlist_tracks(playlist_id))
# To get all the other tracks we need to increase the offset parameter
# of the function, while leaving the limit parameter at its default (100).
# Note that the playlist entries are identified with indices using zero based numbering.
# This simply means that the first index starts at 0 instead on 1 as we are accustomed in R.
# The first one hundred tracks of the playlist correspond to an index ranging from {0,1,2, ... , 99}.
# To get the next one hundred tracks in the playlist we need the indices ranging from {100, 101, 102, ... , 199}.
# This is achieved by setting the offset parameter 100 to fetch the next hundred tracks
# then setting the offset parameter to 200 and so on.
# We create an arbitrarily long vector of offset values starting with 0
offset_vector <- c(0,seq(from=100, to = 10000, by = 100))
# This vector is then used to create a dataframe we will use to iterate.
fetched_playlist <- tibble(offset = offset_vector) %>%
# We now iterate through all the values of the offset vector using the map
# function of the purrr package and call the get_playlist_tracks() function.
mutate(results = map(offset,
~get_playlist_tracks(
playlist_id,
limit=100,
offset = .x),
)) %>%
# we first have to drop all empty lists
filter(lengths(results) > 0) %>%
# To bind each fetched list element together we select the results column and unnest it
select(results) %>%
unnest(results)
# We now have a dataframe containing all the tracks in the playlist
glimpse(fetched_playlist)
That worked thanks!
Out of curiosity, if I now wanted to get the features of fetched_playlist
, would I repeat this procedure for get_track_audio_features()
?
to get the audio features you have to use the track id values from the unnested dataframe and plug them into get_track_audio_features()
purrr is great for this...