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

Obtain Audio Attributes to All Spotify Songs By Artist

这是以下问题的后续问题:

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

所以这就是我们到目前为止所做的,作为上一个问题的结果:

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

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

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

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)

如何提取多位艺术家的歌曲及其属性?

这是 link 音频功能:

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

这是我的尝试:

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

响应:

raw(0)

https://github.com/rweyant/spotifyr

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

set_credentials(client_id=CLIENTID,client_secret=CLIENTSECRET)

错误:找不到函数"set_credentials"

任何帮助都很好,谢谢!

已修改开头部分 API 凭据:

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

我不是特别熟悉 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. 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 端点可获取所有曲目的音频功能。这应该可以很好地代表艺术家的声音。如果您需要更小的表示,您可以取特征的平均值,但请注意,取平均值可能会降低数据的准确性。

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