Nick Knauer
Nick Knauer

Reputation: 4243

Obtain Audio Attributes to All Spotify Songs By Artist

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

Upvotes: 1

Views: 959

Answers (2)

EricA
EricA

Reputation: 403

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 

Upvotes: 2

Hugh Rawlinson
Hugh Rawlinson

Reputation: 989

I'm not particularly familiar with HTTP requests in R, but if the following line relies on string concatenation...

URI = paste0('https://api.spotify.com/v1/audio-features', artistID)

you'll need a trailing backslash on the first argument so that the second parameter properly joins the URI.

Additionally though, the Audio Features endpoint takes a Spotify ID for a track, rather than an artist. What I would recommend you do is to grab the top 10 or so tracks for the artist you're interested in, and use the Get Several Audio Features endpoint to get the audio features for all of the tracks. That should give you a pretty good representation of the sound of an artist. You could take an average of the features if you need a smaller representation, but be aware that averaging will likely reduce the accuracy of the data.

Upvotes: 1

Related Questions