1

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
Community
  • 1
  • 1
nak5120
  • 4,089
  • 4
  • 35
  • 94
  • @Hack-R if you think of anything because I know you were able to solve the previous question. Thanks! – nak5120 Oct 19 '16 at 17:45
  • It probably means the library was not installed and loaded correctly, I tested this and dependencies are not being installed recursively. In a fresh R session, this should work `install.packages(c('httr','jsonlite','RSelenium','RCurl','XML','stringr','stringi','plyr'),dep=TRUE); devtools::install_github('rweyant/spotifyr')` – Silence Dogood Oct 19 '16 at 17:56
  • This is the error that comes up when I do this: Error in assign("client_redirect_uri", client_redirect_uri, envir = .GlobalEnv) : argument "client_redirect_uri" is missing, with no default – nak5120 Oct 19 '16 at 18:04

2 Answers2

2
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 
nak5120
  • 4,089
  • 4
  • 35
  • 94
EricA
  • 403
  • 2
  • 14
  • could you provide a sample token, it could be a random number but how would I apply that to this? I get an error saying: "Error in token$sign : $ operator is invalid for atomic vectors" – nak5120 Jan 04 '17 at 22:47
  • Also when I apply it the way you have it, it states: Error in structure(list(method = method, url = url, headers = keep_last(headers), : argument "token" is missing, with no default – nak5120 Jan 04 '17 at 22:48
  • httr::config(token = token) change this with your Oauth Authorization Code please...This Token here refers to the o-auth token you get post authenticating with the Spotify API. – EricA Jan 05 '17 at 15:13
  • thanks, I just edited the question to have a sample of what I have in my original dataset. If I run everything after "Revised with beginning section with API credentials:" and then your script with my API credentials it doesn't work. Would you be able to revise your answer so that it meets that. I am very new to this, which is why I am asking for this. Thanks for your feedback. – nak5120 Jan 05 '17 at 15:22
  • Replace with add_headers(Authorization = HeaderValue) Instead of httr::config(token = token) in the above code please – EricA Jan 05 '17 at 15:28
  • Also refresh your Tokens – EricA Jan 05 '17 at 15:29
  • Your Welcome Nick :-) – EricA Jan 05 '17 at 16:29
  • I added a follow-up question to your answer which goes alittle more in depth. If you have any thoughts, please let me know. Thanks! https://stackoverflow.com/questions/44958438/audio-features-for-spotify-tracks-on-an-artists-album – nak5120 Jul 06 '17 at 20:40
1

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.

Hugh Rawlinson
  • 979
  • 9
  • 29
  • I tried doing this in the original question and it gave me an output of "raw(0)". Do you know how to do it on a track level. Still having trouble getting the audio attributes. Thanks for the feeback. – nak5120 Oct 22 '16 at 16:01