在 R 中为多位艺术家访问 Spotify API
Accessing Spotify API for Multiple Artists in R
我已经在 Spotify 的开发者应用部分创建了客户端 ID 和密钥。我正在引用此文档并希望通过选择多个艺术家来扩展它。
https://www.r-bloggers.com/the-eurovision-2016-song-contest-in-an-r-shiny-app/
在这个例子中,他们只选择了 1 个艺术家 ID,但这有一些漏洞,
1) 你如何获取艺术家姓名而不是艺术家 ID,因为我在任何地方都找不到艺术家 ID 的词汇表键?
2) 你如何选择多个艺术家而不是一个。
例如,我该如何选择 Frank Sinatra 和 Elvis Presley?
代码如下:
library(httr)
clientID = '#############################'
secret = '########################'
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
## Frank Sinatra spotify artist ID
artistID = '1Mxqyy3pSjf8kZZL4QVxS0'
HeaderValue = paste0('Bearer ', mytoken)
URI = paste0('https://api.spotify.com/v1/artists/', artistID)
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)
Artist
我的目标是获取关于每首艺术家歌曲的所有属性,例如可跳舞性等。
任何帮助都会很棒,谢谢!
如果您询问如何从您在问题中创建的响应对象中获取艺术家姓名,您可以这样做:
> Artist$name
"Frank Sinatra"
如果您问如何按艺术家姓名查询 API,相关 endpoint 是艺术家按姓名搜索,这样您就可以找到 ID(因为 API 基于 ID 而不是搜索功能,因此您需要它):
/v1/search?type=artist
获得几位艺术家的 endpoint 是:
/v1/artists?ids={ids}
他们实际上提供了一个很好的 jsFiddle 示例,该示例是基于名称的(单个)艺术家搜索:
http://jsfiddle.net/JMPerez/0u0v7e1b/
此搜索文档页面展示了他们如何构建搜索 URI:
https://developer.spotify.com/web-api/search-item/
综合起来
这是在 R 中基于艺术家姓名进行搜索的完整示例。假设我们要搜索书呆子说唱之王 YTCracker:
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
Artist
对象具有 ID、URI、流派、关注者数量和其他足以探索该艺术家、他们的专辑和相关艺术家的其余 Spotify API 的信息等等等等:
$artists
$artists$href
[1] "https://api.spotify.com/v1/search?query=ytcracker&offset=0&limit=20&type=artist"
$artists$items
$artists$items[[1]]
$artists$items[[1]]$external_urls
$artists$items[[1]]$external_urls$spotify
[1] "https://open.spotify.com/artist/1x82Mu3wakMkldMW5kEiP4"
$artists$items[[1]]$followers
$artists$items[[1]]$followers$href
NULL
$artists$items[[1]]$followers$total
[1] 2386
$artists$items[[1]]$genres
$artists$items[[1]]$genres[[1]]
[1] "nerdcore"
$artists$items[[1]]$href
[1] "https://api.spotify.com/v1/artists/1x82Mu3wakMkldMW5kEiP4"
$artists$items[[1]]$id
[1] "1x82Mu3wakMkldMW5kEiP4"
$artists$items[[1]]$images
$artists$items[[1]]$images[[1]]
$artists$items[[1]]$images[[1]]$height
[1] 640
$artists$items[[1]]$images[[1]]$url
[1] "https://i.scdn.co/image/da66c87cfb6f7e400979b53f12c0b0cb029424a8"
$artists$items[[1]]$images[[1]]$width
[1] 640
$artists$items[[1]]$images[[2]]
$artists$items[[1]]$images[[2]]$height
[1] 300
$artists$items[[1]]$images[[2]]$url
[1] "https://i.scdn.co/image/4055cb39744f85c94838841eecc74563b94a00d1"
$artists$items[[1]]$images[[2]]$width
[1] 300
$artists$items[[1]]$images[[3]]
$artists$items[[1]]$images[[3]]$height
[1] 64
$artists$items[[1]]$images[[3]]$url
[1] "https://i.scdn.co/image/d46121137f358701b55dcdd03ba9a632ea7e4879"
$artists$items[[1]]$images[[3]]$width
[1] 64
$artists$items[[1]]$name
[1] "YTCracker"
$artists$items[[1]]$popularity
[1] 31
$artists$items[[1]]$type
[1] "artist"
$artists$items[[1]]$uri
[1] "spotify:artist:1x82Mu3wakMkldMW5kEiP4"
$artists$items[[2]]
$artists$items[[2]]$external_urls
$artists$items[[2]]$external_urls$spotify
[1] "https://open.spotify.com/artist/2RnAr4QItmKW4vFKip3m8v"
$artists$items[[2]]$followers
$artists$items[[2]]$followers$href
NULL
$artists$items[[2]]$followers$total
[1] 36
$artists$items[[2]]$genres
list()
$artists$items[[2]]$href
[1] "https://api.spotify.com/v1/artists/2RnAr4QItmKW4vFKip3m8v"
$artists$items[[2]]$id
[1] "2RnAr4QItmKW4vFKip3m8v"
$artists$items[[2]]$images
list()
$artists$items[[2]]$name
[1] "YTCracker & Hairetsu"
$artists$items[[2]]$popularity
[1] 9
$artists$items[[2]]$type
[1] "artist"
$artists$items[[2]]$uri
[1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v"
$artists$limit
[1] 20
$artists$`next`
NULL
$artists$offset
[1] 0
$artists$previous
NULL
$artists$total
[1] 2
请注意,我对搜索结果的大小设置了可选限制。另请注意,您可以使用相同的搜索结果来查询诸如专辑封面图像之类的内容,如 jsFiddle 示例中所示,如果愿意的话。
多位艺人
好的,现在我根据我的搜索获得了 YTCracker 的 ID,并从您的问题中获得了 Sinatra 的 ID,我想同时查询两位艺术家的 API。我可以这样做如下。只要您有 ID,您也可以为 2 个以上的艺术家提供此 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)
$artists
$artists[[1]]
$artists[[1]]$external_urls
$artists[[1]]$external_urls$spotify
[1] "https://open.spotify.com/artist/2RnAr4QItmKW4vFKip3m8v"
$artists[[1]]$followers
$artists[[1]]$followers$href
NULL
$artists[[1]]$followers$total
[1] 36
$artists[[1]]$genres
list()
$artists[[1]]$href
[1] "https://api.spotify.com/v1/artists/2RnAr4QItmKW4vFKip3m8v"
$artists[[1]]$id
[1] "2RnAr4QItmKW4vFKip3m8v"
$artists[[1]]$images
list()
$artists[[1]]$name
[1] "YTCracker & Hairetsu"
$artists[[1]]$popularity
[1] 9
$artists[[1]]$type
[1] "artist"
$artists[[1]]$uri
[1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v"
$artists[[2]]
$artists[[2]]$external_urls
$artists[[2]]$external_urls$spotify
[1] "https://open.spotify.com/artist/1Mxqyy3pSjf8kZZL4QVxS0"
$artists[[2]]$followers
$artists[[2]]$followers$href
NULL
$artists[[2]]$followers$total
[1] 1204440
$artists[[2]]$genres
$artists[[2]]$genres[[1]]
[1] "adult standards"
$artists[[2]]$genres[[2]]
[1] "brill building pop"
$artists[[2]]$genres[[3]]
[1] "christmas"
$artists[[2]]$genres[[4]]
[1] "easy listening"
$artists[[2]]$genres[[5]]
[1] "jazz christmas"
$artists[[2]]$genres[[6]]
[1] "lounge"
$artists[[2]]$genres[[7]]
[1] "mellow gold"
$artists[[2]]$genres[[8]]
[1] "soft rock"
$artists[[2]]$genres[[9]]
[1] "soul"
$artists[[2]]$genres[[10]]
[1] "swing"
$artists[[2]]$genres[[11]]
[1] "vocal jazz"
$artists[[2]]$href
[1] "https://api.spotify.com/v1/artists/1Mxqyy3pSjf8kZZL4QVxS0"
$artists[[2]]$id
[1] "1Mxqyy3pSjf8kZZL4QVxS0"
$artists[[2]]$images
$artists[[2]]$images[[1]]
$artists[[2]]$images[[1]]$height
[1] 650
$artists[[2]]$images[[1]]$url
[1] "https://i.scdn.co/image/fc4e0f474fb4c4cb83617aa884dc9fd9822d4411"
$artists[[2]]$images[[1]]$width
[1] 999
$artists[[2]]$images[[2]]
$artists[[2]]$images[[2]]$height
[1] 416
$artists[[2]]$images[[2]]$url
[1] "https://i.scdn.co/image/883de3e492364891543bc0313ffe516626778a16"
$artists[[2]]$images[[2]]$width
[1] 640
$artists[[2]]$images[[3]]
$artists[[2]]$images[[3]]$height
[1] 130
$artists[[2]]$images[[3]]$url
[1] "https://i.scdn.co/image/810f29651e05c9eae5a31f8151c2e05dc46390f9"
$artists[[2]]$images[[3]]$width
[1] 200
$artists[[2]]$images[[4]]
$artists[[2]]$images[[4]]$height
[1] 42
$artists[[2]]$images[[4]]$url
[1] "https://i.scdn.co/image/a283ecdc6252ae305c53d283846cae2e8b22e94c"
$artists[[2]]$images[[4]]$width
[1] 64
$artists[[2]]$name
[1] "Frank Sinatra"
$artists[[2]]$popularity
[1] 77
$artists[[2]]$type
[1] "artist"
$artists[[2]]$uri
[1] "spotify:artist:1Mxqyy3pSjf8kZZL4QVxS0"
我构建了 spotifyr
包就是为了做到这一点!
install.packages('spotifyr')
library(spotifyr)
library(tidyverse)
Sys.setenv(SPOTIFY_CLIENT_ID = '[YOUR_SPOTIFY_CLIENT_ID]')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '[YOUR_SPOTIFY_CLIENT_SECRET]')
artist_names <- c('Frank Sinatra', 'Elvis Presley')
artist_audio_features <- map_df(artist_names, function(artist) {
get_artist_audio_features(artist) %>%
mutate(artist_name = artist)
})
如果您有任何进一步的 questions/issues,请随时联系 github:https://github.com/charlie86/spotifyr
我已经在 Spotify 的开发者应用部分创建了客户端 ID 和密钥。我正在引用此文档并希望通过选择多个艺术家来扩展它。
https://www.r-bloggers.com/the-eurovision-2016-song-contest-in-an-r-shiny-app/
在这个例子中,他们只选择了 1 个艺术家 ID,但这有一些漏洞,
1) 你如何获取艺术家姓名而不是艺术家 ID,因为我在任何地方都找不到艺术家 ID 的词汇表键?
2) 你如何选择多个艺术家而不是一个。
例如,我该如何选择 Frank Sinatra 和 Elvis Presley?
代码如下:
library(httr)
clientID = '#############################'
secret = '########################'
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
## Frank Sinatra spotify artist ID
artistID = '1Mxqyy3pSjf8kZZL4QVxS0'
HeaderValue = paste0('Bearer ', mytoken)
URI = paste0('https://api.spotify.com/v1/artists/', artistID)
response2 = GET(url = URI, add_headers(Authorization = HeaderValue))
Artist = content(response2)
Artist
我的目标是获取关于每首艺术家歌曲的所有属性,例如可跳舞性等。
任何帮助都会很棒,谢谢!
如果您询问如何从您在问题中创建的响应对象中获取艺术家姓名,您可以这样做:
> Artist$name
"Frank Sinatra"
如果您问如何按艺术家姓名查询 API,相关 endpoint 是艺术家按姓名搜索,这样您就可以找到 ID(因为 API 基于 ID 而不是搜索功能,因此您需要它):
/v1/search?type=artist
获得几位艺术家的 endpoint 是:
/v1/artists?ids={ids}
他们实际上提供了一个很好的 jsFiddle 示例,该示例是基于名称的(单个)艺术家搜索:
http://jsfiddle.net/JMPerez/0u0v7e1b/
此搜索文档页面展示了他们如何构建搜索 URI:
https://developer.spotify.com/web-api/search-item/
综合起来
这是在 R 中基于艺术家姓名进行搜索的完整示例。假设我们要搜索书呆子说唱之王 YTCracker:
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
Artist
对象具有 ID、URI、流派、关注者数量和其他足以探索该艺术家、他们的专辑和相关艺术家的其余 Spotify API 的信息等等等等:
$artists $artists$href [1] "https://api.spotify.com/v1/search?query=ytcracker&offset=0&limit=20&type=artist" $artists$items $artists$items[[1]] $artists$items[[1]]$external_urls $artists$items[[1]]$external_urls$spotify [1] "https://open.spotify.com/artist/1x82Mu3wakMkldMW5kEiP4" $artists$items[[1]]$followers $artists$items[[1]]$followers$href NULL $artists$items[[1]]$followers$total [1] 2386 $artists$items[[1]]$genres $artists$items[[1]]$genres[[1]] [1] "nerdcore" $artists$items[[1]]$href [1] "https://api.spotify.com/v1/artists/1x82Mu3wakMkldMW5kEiP4" $artists$items[[1]]$id [1] "1x82Mu3wakMkldMW5kEiP4" $artists$items[[1]]$images $artists$items[[1]]$images[[1]] $artists$items[[1]]$images[[1]]$height [1] 640 $artists$items[[1]]$images[[1]]$url [1] "https://i.scdn.co/image/da66c87cfb6f7e400979b53f12c0b0cb029424a8" $artists$items[[1]]$images[[1]]$width [1] 640 $artists$items[[1]]$images[[2]] $artists$items[[1]]$images[[2]]$height [1] 300 $artists$items[[1]]$images[[2]]$url [1] "https://i.scdn.co/image/4055cb39744f85c94838841eecc74563b94a00d1" $artists$items[[1]]$images[[2]]$width [1] 300 $artists$items[[1]]$images[[3]] $artists$items[[1]]$images[[3]]$height [1] 64 $artists$items[[1]]$images[[3]]$url [1] "https://i.scdn.co/image/d46121137f358701b55dcdd03ba9a632ea7e4879" $artists$items[[1]]$images[[3]]$width [1] 64 $artists$items[[1]]$name [1] "YTCracker" $artists$items[[1]]$popularity [1] 31 $artists$items[[1]]$type [1] "artist" $artists$items[[1]]$uri [1] "spotify:artist:1x82Mu3wakMkldMW5kEiP4" $artists$items[[2]] $artists$items[[2]]$external_urls $artists$items[[2]]$external_urls$spotify [1] "https://open.spotify.com/artist/2RnAr4QItmKW4vFKip3m8v" $artists$items[[2]]$followers $artists$items[[2]]$followers$href NULL $artists$items[[2]]$followers$total [1] 36 $artists$items[[2]]$genres list() $artists$items[[2]]$href [1] "https://api.spotify.com/v1/artists/2RnAr4QItmKW4vFKip3m8v" $artists$items[[2]]$id [1] "2RnAr4QItmKW4vFKip3m8v" $artists$items[[2]]$images list() $artists$items[[2]]$name [1] "YTCracker & Hairetsu" $artists$items[[2]]$popularity [1] 9 $artists$items[[2]]$type [1] "artist" $artists$items[[2]]$uri [1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v" $artists$limit [1] 20 $artists$`next` NULL $artists$offset [1] 0 $artists$previous NULL $artists$total [1] 2
请注意,我对搜索结果的大小设置了可选限制。另请注意,您可以使用相同的搜索结果来查询诸如专辑封面图像之类的内容,如 jsFiddle 示例中所示,如果愿意的话。
多位艺人
好的,现在我根据我的搜索获得了 YTCracker 的 ID,并从您的问题中获得了 Sinatra 的 ID,我想同时查询两位艺术家的 API。我可以这样做如下。只要您有 ID,您也可以为 2 个以上的艺术家提供此 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)
$artists $artists[[1]] $artists[[1]]$external_urls $artists[[1]]$external_urls$spotify [1] "https://open.spotify.com/artist/2RnAr4QItmKW4vFKip3m8v" $artists[[1]]$followers $artists[[1]]$followers$href NULL $artists[[1]]$followers$total [1] 36 $artists[[1]]$genres list() $artists[[1]]$href [1] "https://api.spotify.com/v1/artists/2RnAr4QItmKW4vFKip3m8v" $artists[[1]]$id [1] "2RnAr4QItmKW4vFKip3m8v" $artists[[1]]$images list() $artists[[1]]$name [1] "YTCracker & Hairetsu" $artists[[1]]$popularity [1] 9 $artists[[1]]$type [1] "artist" $artists[[1]]$uri [1] "spotify:artist:2RnAr4QItmKW4vFKip3m8v" $artists[[2]] $artists[[2]]$external_urls $artists[[2]]$external_urls$spotify [1] "https://open.spotify.com/artist/1Mxqyy3pSjf8kZZL4QVxS0" $artists[[2]]$followers $artists[[2]]$followers$href NULL $artists[[2]]$followers$total [1] 1204440 $artists[[2]]$genres $artists[[2]]$genres[[1]] [1] "adult standards" $artists[[2]]$genres[[2]] [1] "brill building pop" $artists[[2]]$genres[[3]] [1] "christmas" $artists[[2]]$genres[[4]] [1] "easy listening" $artists[[2]]$genres[[5]] [1] "jazz christmas" $artists[[2]]$genres[[6]] [1] "lounge" $artists[[2]]$genres[[7]] [1] "mellow gold" $artists[[2]]$genres[[8]] [1] "soft rock" $artists[[2]]$genres[[9]] [1] "soul" $artists[[2]]$genres[[10]] [1] "swing" $artists[[2]]$genres[[11]] [1] "vocal jazz" $artists[[2]]$href [1] "https://api.spotify.com/v1/artists/1Mxqyy3pSjf8kZZL4QVxS0" $artists[[2]]$id [1] "1Mxqyy3pSjf8kZZL4QVxS0" $artists[[2]]$images $artists[[2]]$images[[1]] $artists[[2]]$images[[1]]$height [1] 650 $artists[[2]]$images[[1]]$url [1] "https://i.scdn.co/image/fc4e0f474fb4c4cb83617aa884dc9fd9822d4411" $artists[[2]]$images[[1]]$width [1] 999 $artists[[2]]$images[[2]] $artists[[2]]$images[[2]]$height [1] 416 $artists[[2]]$images[[2]]$url [1] "https://i.scdn.co/image/883de3e492364891543bc0313ffe516626778a16" $artists[[2]]$images[[2]]$width [1] 640 $artists[[2]]$images[[3]] $artists[[2]]$images[[3]]$height [1] 130 $artists[[2]]$images[[3]]$url [1] "https://i.scdn.co/image/810f29651e05c9eae5a31f8151c2e05dc46390f9" $artists[[2]]$images[[3]]$width [1] 200 $artists[[2]]$images[[4]] $artists[[2]]$images[[4]]$height [1] 42 $artists[[2]]$images[[4]]$url [1] "https://i.scdn.co/image/a283ecdc6252ae305c53d283846cae2e8b22e94c" $artists[[2]]$images[[4]]$width [1] 64 $artists[[2]]$name [1] "Frank Sinatra" $artists[[2]]$popularity [1] 77 $artists[[2]]$type [1] "artist" $artists[[2]]$uri [1] "spotify:artist:1Mxqyy3pSjf8kZZL4QVxS0"
我构建了 spotifyr
包就是为了做到这一点!
install.packages('spotifyr')
library(spotifyr)
library(tidyverse)
Sys.setenv(SPOTIFY_CLIENT_ID = '[YOUR_SPOTIFY_CLIENT_ID]')
Sys.setenv(SPOTIFY_CLIENT_SECRET = '[YOUR_SPOTIFY_CLIENT_SECRET]')
artist_names <- c('Frank Sinatra', 'Elvis Presley')
artist_audio_features <- map_df(artist_names, function(artist) {
get_artist_audio_features(artist) %>%
mutate(artist_name = artist)
})
如果您有任何进一步的 questions/issues,请随时联系 github:https://github.com/charlie86/spotifyr