Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_playlist_tracks() limit is set pretty low #174

Open
blechturm opened this issue Jan 1, 2022 · 9 comments
Open

get_playlist_tracks() limit is set pretty low #174

blechturm opened this issue Jan 1, 2022 · 9 comments
Assignees

Comments

@blechturm
Copy link

blechturm commented Jan 1, 2022

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!

@antaldaniel
Copy link
Collaborator

Documentation pull request or examples are more than welcome.

@antaldaniel antaldaniel reopened this Jan 1, 2022
@blechturm
Copy link
Author

I can try... Would be my first pull request tho 😅

Is a tidyverse and purrr centric example okay or should it be base-R?

@antaldaniel
Copy link
Collaborator

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.

@blechturm
Copy link
Author

blechturm commented Jan 3, 2022

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)

@blechturm
Copy link
Author

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.

@eledroos
Copy link

eledroos commented Apr 4, 2023

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?

@blechturm
Copy link
Author

blechturm commented Apr 4, 2023

``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)

@eledroos
Copy link

eledroos commented Apr 4, 2023

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()?

@blechturm
Copy link
Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants