Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. albums <- GET(paste0('https://api.spotify.com/v1/artists/', artist_uri,'/albums')) %>% content
  2.  
  3. library(httr)
  4. library(stringr)
  5. library(lubridate)
  6.  
  7. #Roots uri
  8. artist_uri <- 78xUyw6FkVZrRAtziFdtdu
  9.  
  10. get_albums <- function(artist_uri) {
  11. albums <- GET(paste0('https://api.spotify.com/v1/artists/', artist_uri,'/albums')) %>% content
  12.  
  13. map_df(1:length(albums$items), function(x) {
  14. tmp <- albums$items[[x]]
  15.  
  16. data.frame(album_uri = str_replace(tmp$uri, 'spotify:album:', ''),
  17. album_name = str_replace_all(tmp$name, ''', ''),
  18. album_img = albums$items[[x]]$images[[1]]$url,
  19. stringsAsFactors = F) %>%
  20. mutate(album_release_date = GET(paste0('https://api.spotify.com/v1/albums/', str_replace(tmp$uri, 'spotify:album:', ''))) %>% content %>% .$release_date, # you need a separate call to on "albums" to get release date.
  21. album_release_year = ifelse(nchar(album_release_date) == 4, year(as.Date(album_release_date, '%Y')), year(as.Date(album_release_date, '%Y-%m-%d'))) # not all album_release_dates have months, so I created album_release year for sorting
  22. )
  23.  
  24.  
  25.  
  26. }) %>% filter(!duplicated(tolower(album_name))) %>% # Sometimes there are multiple versions (just with different capitalizations) of the same album
  27. arrange(album_release_year)
  28. }
  29.  
  30. album_info <- get_albums(artist_info$artist_uri)
  31.  
  32. View(album_info)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement