I tried to get 50 songs for each genre and put them together in a data frame with the genre name, but I got following error.
Error : object 'res' not found
I know that the undefined object is causing the problem, but I don't know how to do with it. Could you shed some light on this?
**Language:R, Environment: Exploratory Public**
genres <- RETRY('GET', url = 'https://api.spotify.com/v1/recommendations/available-genre-seeds', query = list(access_token = get_spotify_access_token(), limit=150), quiet = TRUE) %>% content()
m <- do.call(rbind,lapply(genres$genre, function(x) if(is.null(x)) NA else c(x)))
genres_df <- as.data.frame(m)
colnames(genres_df) <- c("genre")
get_genre_track <- function(genre){
track_check <- RETRY('GET', url = paste0("https://api.spotify.com/v1/search?query=genre:",genre), query = list(type="track",limit = 50, offset = 0, access_token = get_spotify_access_token()), quiet = TRUE) %>% content()
track_count <- 50
df <- map_df(1:length(res$tracks$items), function(this_row) {
tryCatch({
this_track <- res$tracks$items[[this_row]]
name <- this_track$name
genre <- genre
list(name = name, genre = genre)
}, error = function(e){
NULL
})
})
}
tracks_df <- lapply(genres_df$genre, get_genre_track) %>% bind_rows()
tracks_df
}
(Required library installation and user information are omitted)
CodePudding user response:
In the script you've linked above, res
is defined as follows:
res <- RETRY('GET', url = paste0("https://api.spotify.com/v1/search?query=genre:",genre), query = list(type="track",limit = 50, offset = numoffset, access_token = get_spotify_access_token()), quiet = TRUE) %>% content()
Whereas in your code, you seem to have renamed that variable to track_check
:
track_check <- RETRY('GET', url = paste0("https://api.spotify.com/v1/search?query=genre:",genre), query = list(type="track",limit = 50, offset = 0, access_token = get_spotify_access_token()), quiet = TRUE) %>% content()
So don't refer to it as res$tracks$items
, but track_check$tracks$items
.