在 R 中访问多个艺术家的 Spotify API

2024-04-01

我在 Spotify 的开发者应用程序部分创建了客户端 ID 和密钥。我正在参考这份文件,并希望通过选择多名艺术家来扩展它。

https://www.r-bloggers.com/the-eurovision-2016-song-contest-in-an-r-shiny-app/ https://www.r-bloggers.com/the-eurovision-2016-song-contest-in-an-r-shiny-app/

在此示例中,他们仅选择 1 个艺术家 ID,但这存在一些漏洞,

1) 由于我在任何地方都找不到艺术家 ID 的术语表关键字,因此如何获取艺术家姓名而不是艺术家 ID?

2) 如何选择多位艺术家而不是一位艺术家。

例如,我如何选择弗兰克·辛纳屈和埃尔维斯·普雷斯利?

代码如下:

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

“弗兰克·西纳特拉”

如果您询问如何通过艺术家姓名查询 API,相关endpoint https://developer.spotify.com/web-api/artist-endpoints/是按姓名搜索艺术家,这将允许您找到 ID(因为 API 是基于 ID 而不是搜索功能,所以您需要它):

/v1/search?type=artist

The endpoint https://developer.spotify.com/web-api/artist-endpoints/获得几位艺术家的方法是:

/v1/artists?ids={ids}

他们实际上提供了一个很好的 jsFiddle 示例,根据名称进行(单个)艺术家搜索:

http://jsfiddle.net/JMPerez/0u0v7e1b/ http://jsfiddle.net/JMPerez/0u0v7e1b/

此搜索文档页面显示了他们如何构建搜索 URI:

https://developer.spotify.com/web-api/search-item/ 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

The 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 位以上的艺术家执行此操作。

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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 R 中访问多个艺术家的 Spotify API 的相关文章

  • ggplot:如何限制条形图中的输出,以便仅显示最频繁出现的情况?

    我几个小时以来一直在寻找这个简单的东西 但没有结果 我有一个数据框 其中一列为变量 国家 地区 我想要两件事以下 绘制最常见的国家 地区 最常见的位于顶部 找到部分解决方案EDIT找到完整的解决方案 gt gt 重点问题是根据频率限制条形图
  • 将维基百科中的表格加载到 R 中

    我正在尝试从以下 URL 将最高法院法官表加载到 R 中 https en wikipedia org wiki List of Justices of the Supreme Court of the United States http
  • 如何将旋转的 NetCDF 转换回正常的纬度/经度网格?

    我有一个带有旋转坐标的 NetCDF 文件 我需要将其转换为正常的纬度 经度坐标 经度为 180到180 纬度为 90到90 library ncdf4 nc open dat nf 对于尺寸 它显示 1 5 variables exclu
  • R - 基于列名称的子集

    我的数据框有超过 120 列 变量 我想根据列名称创建子集 例如 我想创建一个子集 其中列名称包含字符串 心情 这可能吗 我一般用 SubData lt myData grep whatIWant colnames myData 我很清楚
  • 计算 R 行中的非零条目数

    我有以下类型的数据 mode1 mode2 mode3 1 8 1 0 2 0 0 0 3 6 5 4 4 1 2 3 5 1 1 1 数据使用dput structure list mode1 c 8L 0L 6L 1L 1L mode2
  • StatET调试工具

    我想我只是很密集 但我似乎无法弄清楚如何在 Eclipse 中的 R 中使用调试工具 StatET 插件 有人有关于这个主题的任何提示或教程吗 StatET 2 00 现在对高级 可视化调试提供实验性支持 需要 Eclipse 3 6 或
  • 在 Google Colab 上的 R 笔记本中安装 python 库

    我正在尝试在 Google Colab 上的 R 笔记本中安装 python 库 为此我使用 reticulate 包 library reticulate py install pandas 但我得到的结果是这个错误 Error coul
  • 使用 Instagram Basic Display API 时出现“无效平台应用程序”错误

    我正在尝试使用 Instagram Basic 显示 API 但是当我发布授权代码以获取访问令牌时 我不断收到以下错误 error type OAuthException 代码 400 error message 平台应用无效 我正在遵循此
  • 我如何查看 quantmod 包中所有可用的数据系列?

    如何显示可用的所有报价 数据系列的列表 例如使用雅虎的 getSymbols 我不知道有什么办法 TTR包有一个功能 stockSymbols 下载 NYSE AMEX 和 NASDAQ 的所有当前代码 它试图将它们采用雅虎可接受的格式 但
  • 无法将“gather”输出的列名称更改为默认名称以外的任何名称

    我正在尝试使用gather in the tidyr包 但我无法更改默认名称的输出列名称 例如 df data frame time 1 100 a 1 100 b 101 200 df long df gt gather foo bar
  • 将整个包传递给雪簇

    我正在尝试并行化 使用snow parLapply 一些依赖于包 即除snow 调用函数中引用的对象parLapply必须使用显式传递给集群clusterExport 有没有办法将整个包传递到集群 而不必显式命名每个函数 包括用户函数调用的
  • R:改变堆积条形图的颜色

    library ggplot2 df2 lt data frame supp rep c VC OJ each 3 dose rep c D0 5 D1 D2 2 len c 6 8 15 33 4 2 10 29 5 head df2 g
  • 安装 2.15 后 ggplot2 中的 alpha 通道不起作用

    更新到 R 2 15 后 ggplot 中的 alpha 通道似乎不再起作用 plot rnorm 100 rnorm 100 bg cc000055 pch 21 工作得很好但是 qplot rnorm 100 rnorm 100 col
  • 将 Excel 文件读入 R 并锁定单元格

    我有一个 Excel 电子表格要读入 R 它受密码保护并锁定了单元格 我可以使用 excel link 导入受密码保护的文件 但我不知道如何解锁 取消保护单元格 excel link 给了我这个错误 gt
  • 更改绘图区域背景颜色

    我想使用我们公司的颜色在 R 中制作一个图表 这意味着所有图表的背景应为浅蓝色 但绘图区域应为白色 我正在寻找答案 发现绘制一个矩形就可以完成这项工作 几乎 然而 绘图区域现在是白色的 并且图形不再可见 这可能吗 getSymbols SP
  • 如何从 R 中的多列中获取计数?

    我有一个包含三列的数据框 State1 State2 State3 有没有一种方法可以使用所有三列 最好使用 dplyr 并且没有显式循环 来获取一个数据帧中每个状态的计数 我只知道如何做一栏 df gt group by State1 g
  • 识别包含字符串的行的最快方法[重复]

    这个问题在这里已经有答案了 我有一个字符串数据框 尺寸为 30 列 x 500 万行 我想识别包含任何预定义字符串列表的行 有没有比下面我的 apply any 方法更快的方法 这是一个可重现的示例 请注意 此示例中的字符串是随机数 但在我
  • 使用非标准评估公式

    我正在创建一个使用的包非标准评价 http adv r had co nz Computing on the language html跟踪列的含义 该包在函数之间传递数据框 这些函数执行同一组列的各种操作 非标准评估对此非常有用 my s
  • tidyverse - 将命名向量转换为 data.frame/tibble 的首选方法

    使用tidyverse我经常面临将命名向量转换为向量的挑战data frame tibble列是向量的名称 执行此操作的首选 tidyverse 方式是什么 编辑 这与 this https github com hadley dplyr
  • Spotify 登录错误 INVALID_CLIENT:无效的重定向 URI android

    我正在制作一个包含 Spotify 集成的应用程序 我点击了此链接https developer spotify com technologies spotify android sdk tutorial https developer s

随机推荐