python Spotify -获取播放列表曲目名称

z5btuh9x  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(107)

search endpoint上,可以检索playlist数据,例如:

def search_playlist():

    results = sp.search(q='doom metal', type='playlist')

如果我在代码中添加以下内容:

items = results['playlists']['items'][0]['tracks']
    print (items)

我得到:
{u'total': 349, u'href': u'https://api.spotify.com/v1/users/handiofiblood/playlists/71CdtOFANPpdboCh6e8lHr/tracks'}
但是,如果这个endpoint上的id代表的是playlist,而不是tracks本身,我如何访问曲目名称

enxuqcxy

enxuqcxy1#

https://github.com/plamere/spotipy/blob/master/examples/user_playlists_contents.py

def show_tracks(results):
for i, item in enumerate(tracks['items']):
    track = item['track']
    print("   %d %32.32s %s" % (i, track['artists'][0]['name'], track['name']))

playlists = sp.user_playlists(username)
    for playlist in playlists['items']:
        if playlist['owner']['id'] == username:
            print()
            print(playlist['name'])
            print('  total tracks', playlist['tracks']['total'])
            results = sp.user_playlist(username, playlist['id'], fields="tracks,next")
            tracks = results['tracks']
            show_tracks(tracks)
            while tracks['next']:
                tracks = sp.next(tracks)
                show_tracks(tracks)

相关问题