通过艺术家获取所有Spotify歌曲的音频属性

时间:2016-10-19 17:20:51

标签: r api spotify httr libspotify

这是一个后续问题:Accessing Spotify API for Multiple Artists in R

我的目标是从Spotify的API中提取多个艺术家,然后按艺术家的属性检索所有歌曲。

所以这就是我们迄今为止对前一个问题所做的工作:

检索有关艺术家的信息(不是按歌曲):

QLibrary

多位艺术家,如果您从上面的代码中了解艺术家ID。

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

如何使用属性提取多首艺术家歌曲?

这是音频功能的链接:

https://developer.spotify.com/web-api/get-several-audio-features/

这是我的尝试:

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)

回应:

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

https://github.com/rweyant/spotifyr

这是一个很好的参考,但即使在打开' httr'之后我也无法设置我的凭据。图书馆。

raw(0)

错误:无法找到功能" set_credentials"

任何帮助都很棒,谢谢!

使用API​​凭据的开头部分修改:

set_credentials(client_id=CLIENTID,client_secret=CLIENTSECRET)

2 个答案:

答案 0 :(得分: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 

答案 1 :(得分:1)

我对R中的HTTP请求并不是特别熟悉,但如果以下行依赖于字符串连接......

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

您需要在第一个参数上使用尾部反斜杠,以便第二个参数正确连接URI。

此外,the Audio Features endpoint takes a Spotify ID for a track, rather than an artist。我建议你做的是抓住你感兴趣的艺术家的前10首曲目,并使用Get Several Audio Features端点获取所有曲目的音频功能。这应该可以很好地代表艺术家的声音。如果需要较小的表示,可以采用平均功能,但要注意平均可能会降低数据的准确性。

相关问题