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!