Spotify API: Randomness in search

Viewed 340

For my thesis I'm trying to scrape random Spotify data from the API.

I am looking for US Music from specific years and genres, which are defined in my API search.

However, in order to further expand my data set, I would like to add randomness to my search in the API, for example by adding a random wildcard (based on this post: Spotify API: get random songs).

Here is my code without the wildcard:

for(j in 1:length(YEAR)){
for(h in 1:length(shortlist)){
for (i in 1:100){
  temporary_url[i] = paste0("https://api.spotify.com/v1/search?query=", 
                          "year:", YEAR[j], "+", # I'm looking for 2014:2019 tracks
                          "genre:", shortlist[h], # list of 6 genres that I'm interested in
                          "&type=", TYPE, # tracks instead of artists
                          "&market=", MARKET,  # US Market
                          "&limit=", LIMIT, # maximum allowed tracks per request
                          "&offset=", sample(c(1:999), 1), # create random offset for a little bit randomness
                          "&include_external=audio")
}
  df_url <- c(df_url, temporary_url)
  temporary_url <- NA
}
}

To this moment, I have not been successful at adding the randomness. Once I add a random by using a random vowel in the search. When I do this, the API returns a 404 error. If I don't do this I get a 200 OK message, indicating my search was successful.

My approach so far was to store a vector (with e.g. %25a%25', 'a%25', ...) and use the sample function to add one of the string to an URL. This before the "year:" part of my function. This returns the error.

Does anybody have some advice on how I can improve my current API URL to have randomness and not return an 404 error?

Thank you so much in advance!

1 Answers

I had the same question a few months ago before a developed Randify, a random Spotify playlist generator. In order to get the random songs I tried multiple things, a few that worked ok where the following:

  • Use the API function Search with the q parameter "%25a%25" or using any other random letter instead of a. That will get you the songs that have an a in their name. You can then set the offset parameter to a random number and you will get one of the 1000 songs that you get with the search function. The problem with that is that the search function will sort the music by how many people listen to the songs and will only show popular music.
  • In order to solve the "popular music" problem I thought that the best option was to get a songs database and using the same search function, add the song name as the q parameter, therefore the search will be more specific and will find non-popular results. The database I used was MusicBrainz, which has up to 30 million songs. I still used a random offset parameter and had to check if the song was in Spotify (not all songs in the database are in Spotify) before I added it to the playlist. It worked quite good and I'm really proud of the outcome.
Related