Last active
October 22, 2016 08:37
-
-
Save jamesthomson/682725e12271901c496902daec52417a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
country = "GB" | |
albumType = "album" | |
artist="Led Zeppelin" | |
#get artist id | |
url <- paste0("https://api.spotify.com/v1/search?q=", sub(" ", "%20", artist), "&type=artist") | |
search <- content(GET(url)) | |
search$artists$items[[1]] | |
artist_id = search$artists$items[[1]]$id | |
#user artis id to get list of albums ids | |
url <- paste0("https://api.spotify.com/v1/artists/", artist_id, "/albums?album_type=", albumType, "&limit=50&country=", country) | |
list <- content(GET(url)) | |
albums <- data.frame(artist = NULL, artist_id = NULL, album = NULL, album_id = NULL) | |
for (j in c(1:length(list$items))) { | |
temp <- data.frame(artist = artist, | |
artist_id = artist_id, | |
album = list$items[[j]]$name, | |
album_id = list$items[[j]]$id) | |
albums <- rbind(albums, temp) | |
} | |
#pick out original studio albums | |
studio_albums<-albums[c(34, 31, 28, 25, 21, 15, 14, 10, 7),] | |
#for each album get track ids | |
albumtracks <- data.frame(artist = NULL, artist_id = NULL, | |
album = NULL, album_id = NULL, track = NULL, track_id = NULL, | |
track_number = NULL, track_length = NULL, preview_url = NULL) | |
for (i in c(1:nrow(studio_albums))) { | |
url <- paste0("https://api.spotify.com/v1/albums/", studio_albums[i,4], "/tracks?limit=50") | |
list <- content(GET(url)) | |
for (j in c(1:length(list$items))) { | |
temp <- data.frame(artist = studio_albums[i,1], artist_id = studio_albums[i, 2], | |
album = studio_albums[i, 3], album_id = studio_albums[i, 4], | |
track = list$items[[j]]$name, track_id = list$items[[j]]$id, | |
track_number = list$items[[j]]$track_number, | |
track_length = format(.POSIXct(list$items[[j]]$duration_ms/1000, tz = "GMT"), "%M:%S"), | |
preview_url = ifelse(is.null(list$items[[j]]$preview_url), | |
"NO PREVIEW", list$items[[j]]$preview_url) | |
) | |
albumtracks <- rbind(albumtracks, temp) | |
} | |
} | |
albumtracks$track_id[1] | |
#for each track id get audio features. need to be a registered spotify dev with id and access key | |
clientID = 'xxxxxxxxxxxxxxxx' | |
secret = 'xxxxxxxxxxxxxxxxxxxxxx' | |
#get token | |
response = POST( | |
'https://accounts.spotify.com/api/token', | |
accept_json(), | |
authenticate(clientID, secret), | |
body = list(grant_type = 'client_credentials'), | |
encode = 'form', | |
verbose() | |
) | |
mytoken = content(response)$access_token | |
HeaderValue = paste0('Bearer ', mytoken) | |
#get features | |
audio_features<-data.frame( | |
danceability=NULL, energy=NULL, key=NULL, loudness=NULL, mode=NULL, speechiness=NULL, | |
acousticness=NULL, instrumentalness=NULL, liveness=NULL, valence=NULL, tempo=NULL, | |
duration_ms=NULL, time_signature=NULL | |
) | |
for(i in albumtracks$track_id){ | |
url <- paste0("https://api.spotify.com/v1/audio-features/", i) | |
list = content(GET(url = url, add_headers(Authorization = HeaderValue))) | |
temp<-data.frame( | |
danceability=list$danceability[1], | |
energy=list$energy[1], | |
key=list$key[1], | |
loudness=list$loudness[1], | |
mode=list$mode[1], | |
speechiness=list$speechiness[1], | |
acousticness=list$acousticness[1], | |
instrumentalness=list$instrumentalness[1], | |
liveness=list$liveness[1], | |
valence=list$valence[1], | |
tempo=list$tempo[1], | |
duration_ms=list$duration_ms[1], | |
time_signature=list$time_signature[1] | |
) | |
audio_features<-rbind(audio_features, temp) | |
} | |
#combine and save data | |
albumtrackfeatures<-cbind(albumtracks, audio_features) | |
zep_output<-list(albumtrackfeatures=albumtrackfeatures, audio_features=audio_features) | |
save(zep_output, file="zep_tracks.RData") | |
write.table(albumtrackfeatures, file="zep_tracks.csv", row.names=FALSE, col.names = FALSE, sep=",") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment