This is a follow-up question to: Accessing Spotify API for Multiple Artists in R
My goal here is to extract multiple artists from Spotify's API and then retrieve all of the songs by artist with their attributes.
So this is what we have done so far as a result to the previous question:
Retrieve info about the artist (not by song):
artistName = 'ytcracker'
HeaderValue = paste0('Bearer ', mytoken)
URI = paste0('https://api.spotify.com/v1/search?query=', artistName,'&offset=0&limit=20&type=artist')
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)
Artist
Multiple Artists if you know artist ID from the code above.
URI = paste0('https://api.spotify.com/v1/artists?ids=', Artist$artists$items[[2]]$id,",", '1Mxqyy3pSjf8kZZL4QVxS0')
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artists = content(response2)
How do I extract multiple artists songs with their attributes?
This is the link for audio-features:
https://developer.spotify.com/web-api/get-several-audio-features/
This is my attempt:
artistID = '1Mxqyy3pSjf8kZZL4QVxS0'
HeaderValue = paste0('Bearer ', mytoken)
URI = paste0('https://api.spotify.com/v1/audio-features', artistID)
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)
Artist
Response:
raw(0)
https://github.com/rweyant/spotifyr
This is a good reference but I can't set my credentials even after opening the 'httr' library.
set_credentials(client_id=CLIENTID,client_secret=CLIENTSECRET)
Error: could not find function "set_credentials"
Any help is great, thanks!
Revised with beginning section with API credentials:
clientID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
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
getFeatures<-function(spotify_ID,token){
req <- httr::GET(paste0("https://api.spotify.com/v1/audio-features/",spotify_ID), add_headers(Authorization = HeaderValue))
json1<-httr::content(req)
dados=data.frame(id=json1$id,
danceability=json1$danceability,
energy=json1$energy,
key=json1$key,
loudness=json1$loudness,
mode=json1$mode,
speechiness=json1$speechiness,
acousticness=json1$acousticness,
instrumentalness=json1$instrumentalness,
liveness=json1$liveness,
valence=json1$valence,
tempo=json1$tempo,
duration_ms=json1$duration_ms,
time_signature=json1$time_signature,
uri=json1$uri,
analysis_url=json1$analysis_url,stringsAsFactors = F)
return(dados)
}
KanyeFatherStretch <- getFeatures("4KW1lqgSr8TKrvBII0Brf8")
Try this if it helps