使用Spotipy我试图通过提供艺术家和专辑的名称来列出曲目。
这应该是非常直接的,但我不知道如何获得albumID以获得跟踪列表。我认为它会是这样的:
sp.album_tracks(q = "album:" + album, type = "album")
......只有那些不起作用。
这是我到目前为止所得到的。它将成功获得所选艺术家的专辑(在此硬编码为#34; Phosgore"除了他们只有三张专辑之外没有特别的原因,我不想被词典淹没):
#!/usr/bin/python
# -*- coding: utf-8 -*-
# shows album from trackname
import sys
import spotipy
def get_albums_from_artist_name(name):
results = sp.search(q = "artist:" + name, type = "artist")
items = results["artists"]["items"]
if len(items) == 0:
return None
else:
d = items[0]
# get artistID and artist name from dict
artID = d["id"] # 3Cf1GbbU9uHlS3veYiAK2x
aName = d["name"] # Phosgore
artistUri = "spotify:artist:" + artID
results = sp.artist_albums(artistUri, album_type = "album")
albums = results["items"]
while results["next"]:
results = sp.next(results)
albums.extend(results["items"])
unique = set() # ignore duplicate albums
for album in albums:
name = album["name"]
if not name in unique:
unique.add(name) # unique is a set
print ("\nAlbums by %s:\n" %(aName))
unique = list(unique)
for i in range(0, len(unique)):
print unique[i]
# let's return a list instead of a set
return list(unique)
#------------------------------------------------
def get_tracks_from_album(album):
tracks = []
# results = sp.album_tracks(q = "album:" + album, type = "album")
# don't know how to get album id
# list tracks here
sp = spotipy.Spotify()
sp.trace = False
ask = "Phosgore"
artistAlbums = get_albums_from_artist_name(ask)
get_tracks_from_album("Pestbringer")
答案 0 :(得分:3)
获取相册的uri
并将其传递给.album_tracks()
方法:
import spotipy
sp = spotipy.Spotify()
sp.trace = False
# find album by name
album = "Pestbringer"
results = sp.search(q = "album:" + album, type = "album")
# get the first album uri
album_id = results['albums']['items'][0]['uri']
# get album tracks
tracks = sp.album_tracks(album_id)
for track in tracks['items']:
print(track['name'])
打印:
Embrace Our Gift
Here Comes the Pain
Pestbringer
Dein Licht
Aggression Incarnate
Countdown to Destruction
Nightmare
Noise Monsters
Lobotomy
The Holy Inquisition
Tote Musikanten