使用 Spotify 中 sp.albums 的 .json 输出从同一艺术家的多个专辑中获取曲目 ID
Posted
技术标签:
【中文标题】使用 Spotify 中 sp.albums 的 .json 输出从同一艺术家的多个专辑中获取曲目 ID【英文标题】:Getting track ids from mulitple albums from same artist using .json output from sp.albums in Spotipy 【发布时间】:2021-01-16 11:02:22 【问题描述】:我知道有多种方法可以做到这一点 - 我已经使用用户播放列表唱片成功从一位艺术家那里获得了所有曲目 ID。但我想提出一个不依赖于预先存在的用户播放列表的 Python 程序。
我要做的是从 .json 文件中的专辑数据中提取曲目 ID,该文件是在将多个专辑传递给 Spitipy 中的 sp.albums 时创建的。
我有这张专辑 ID 列表,我要传递给 sp.albums,这是我最喜欢的乐队之一 Fugazi 的所有唱片。
album_ids = ['6ZyP2KPr2AkAbUWbsHmzEg','2bU6BaHfovn3rvxxxHSkWd','3tnzZhTTjRTExYc9odG0dt','1hatewdVYKH0xqEkdOLQLb','3kPxFCpws7ZyzI1cMME4dV','3zBQ2UZzCzgCnIkFYMOWlY','3QpmoxpWCacHLsZMhLn1ag','3ZnF1cPxlqB48RyLiecDnv','21wERoyBas2JCRCgysPDJX']
然后我将此列表传递给 sp.albums:
fugazi_songs = sp.albums(album_ids)
fugazi_songs 的 .json 文件太长,无法在此处发布。
但这是一开始的代码,其中包含第一张专辑中第一首歌的数据:
"albums": [
"album_type": "album",
"artists": [
"external_urls":
"spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
,
"href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
"id": "62sC6lUEWRjbFqXpMmOk4G",
"name": "Fugazi",
"type": "artist",
"uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
],
"available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
"copyrights": [
"text": "Dischord Records",
"type": "C"
,
"text": "Dischord Records",
"type": "P"
],
"external_ids":
"upc": "0643859181027"
,
"external_urls":
"spotify": "https://open.spotify.com/album/6ZyP2KPr2AkAbUWbsHmzEg"
,
"genres": [],
"href": "https://api.spotify.com/v1/albums/6ZyP2KPr2AkAbUWbsHmzEg",
"id": "6ZyP2KPr2AkAbUWbsHmzEg",
"images": [
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b2737c6b218a0797b44ace3f237b",
"width": 640
,
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e027c6b218a0797b44ace3f237b",
"width": 300
,
"height": 64,
"url": "https://i.scdn.co/image/ab67616d000048517c6b218a0797b44ace3f237b",
"width": 64
],
"label": "Dischord Records",
"name": "First Demo",
"popularity": 32,
"release_date": "2014-11-18",
"release_date_precision": "day",
"total_tracks": 11,
"tracks":
"href": "https://api.spotify.com/v1/albums/6ZyP2KPr2AkAbUWbsHmzEg/tracks?offset=0&limit=50",
"items": [
"artists": [
"external_urls":
"spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
,
"href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
"id": "62sC6lUEWRjbFqXpMmOk4G",
"name": "Fugazi",
"type": "artist",
"uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
],
"available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
"disc_number": 1,
"duration_ms": 190459,
"explicit": false,
"external_urls":
"spotify": "https://open.spotify.com/track/4PKl6Tt8YQaJIJmlnwELi7"
,
"href": "https://api.spotify.com/v1/tracks/4PKl6Tt8YQaJIJmlnwELi7",
"id": "4PKl6Tt8YQaJIJmlnwELi7",
"is_local": false,
"name": "Waiting Room - Demo",
"preview_url": "https://p.scdn.co/mp3-preview/d9d339d1c8426d37d8a8ed30a8eb6e4a10ee84d3?cid=ff4c1e3515504e8397510b8d452c0eab",
"track_number": 1,
"type": "track",
"uri": "spotify:track:4PKl6Tt8YQaJIJmlnwELi7"
,
这是第二张专辑中第一首歌曲的代码,紧随第一张专辑数据的末尾:
"album_type": "album",
"artists": [
"external_urls":
"spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
,
"href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
"id": "62sC6lUEWRjbFqXpMmOk4G",
"name": "Fugazi",
"type": "artist",
"uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
],
"available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
"copyrights": [
"text": "Dischord Records",
"type": "C"
,
"text": "Dischord Records",
"type": "P"
],
"external_ids":
"upc": "0718751962026"
,
"external_urls":
"spotify": "https://open.spotify.com/album/3tnzZhTTjRTExYc9odG0dt"
,
"genres": [],
"href": "https://api.spotify.com/v1/albums/3tnzZhTTjRTExYc9odG0dt",
"id": "3tnzZhTTjRTExYc9odG0dt",
"images": [
"height": 640,
"url": "https://i.scdn.co/image/ab67616d0000b273528034c2cfe590178ac6657a",
"width": 640
,
"height": 300,
"url": "https://i.scdn.co/image/ab67616d00001e02528034c2cfe590178ac6657a",
"width": 300
,
"height": 64,
"url": "https://i.scdn.co/image/ab67616d00004851528034c2cfe590178ac6657a",
"width": 64
],
"label": "Dischord Records",
"name": "Instrument Soundtrack",
"popularity": 53,
"release_date": "1999",
"release_date_precision": "year",
"total_tracks": 18,
"tracks":
"href": "https://api.spotify.com/v1/albums/3tnzZhTTjRTExYc9odG0dt/tracks?offset=0&limit=50",
"items": [
"artists": [
"external_urls":
"spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
,
"href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
"id": "62sC6lUEWRjbFqXpMmOk4G",
"name": "Fugazi",
"type": "artist",
"uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
],
"available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
"disc_number": 1,
"duration_ms": 227000,
"explicit": false,
"external_urls":
"spotify": "https://open.spotify.com/track/7dcMZLssGtsCJq9VdI22XC"
,
"href": "https://api.spotify.com/v1/tracks/7dcMZLssGtsCJq9VdI22XC",
"id": "7dcMZLssGtsCJq9VdI22XC",
"is_local": false,
"name": "Pink Frosty Demo",
"preview_url": "https://p.scdn.co/mp3-preview/8dd834d95f6d4d74a36d71f24be422212545c881?cid=ff4c1e3515504e8397510b8d452c0eab",
"track_number": 1,
"type": "track",
"uri": "spotify:track:7dcMZLssGtsCJq9VdI22XC"
,
解析 fugazi_songs .json 文件向我展示了这个指向单个轨道 ID 的层次结构:
albums.item.tracks.items.item.artists.item.id
我正在使用 for 循环尝试从使用此路径的 .json 文件中的每首歌曲中获取所有曲目 ID。
track_ids = []
for i in range(len(fugazi_songs['albums'])):
track_ids.append(fugazi_songs['albums'][i]['tracks']['items'][i]['id'])
输出只给了我一张专辑的 track_ids。
track_ids
['4PKl6Tt8YQaJIJmlnwELi7', '44ETQCGE31ypmEc5ut3WMe','18CrxbPSUzPlibPB7jjsbi','2iNaUrp1SRBFoJnJqhbwS8','3SdaSSuc4prTO9IKFZEvxd','21ObFu22NhjfhXJm51Idij','2jKNbuegISV7YPdvXu3vaB','7r5rLLUes9m2bKvdmESzBP','6JPcT2uMbQzmdp6VJFa9bA']
我确信解决方案很简单 - 但我已经花了几个小时试图解决这个问题。
如何获取从 sp.albums 生成的 .json 文件中收集的多个专辑的所有曲目?
非常感谢您抽出宝贵时间阅读本文,感谢您的意见!
【问题讨论】:
【参考方案1】:如果我正确理解您的代码和数据,您不会从一张专辑中获取所有歌曲,而是从第一张专辑中获取第一首歌曲,从第二张专辑中获取第二首歌曲,依此类推(请查看)。我认为您必须将代码更改为:
track_ids = []
for album_id in range(len(fugazi_songs['albums'])):
for track_id in range(len(fugazi_songs['albums'][album_id]['tracks']['items']))
track_ids.append(fugazi_songs['albums'][album_id]['tracks']['items'][track_id]['id'])
【讨论】:
这很有道理——我什至没有意识到这就是我写的原始 for 循环中发生的事情。作为一个新的 Python 程序员,这对我有很大帮助。谢谢你的帮助!!!以上是关于使用 Spotify 中 sp.albums 的 .json 输出从同一艺术家的多个专辑中获取曲目 ID的主要内容,如果未能解决你的问题,请参考以下文章
在 Apple Music 中打开/搜索 Spotify 曲目(使用 Applescript)
如何在 Kotlin 中使用 PKCE 实现 Spotify 授权代码
如何在 Spotify 上播放专辑中的曲目,以便之后使用 Spotify App Remote SDK for Android 播放专辑的下一首曲目?