pythonspotifyspotipy

Spotify - get playlist track names


On search endpoint, one can retrieve playlist data, for instance:

def search_playlist():

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

If I add this to the code:

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

I get:

{u'total': 349, u'href': u'https://api.spotify.com/v1/users/handiofiblood/playlists/71CdtOFANPpdboCh6e8lHr/tracks'}

But how do I access track names, given the fact that the id at this endpoint stands for the playlist and not the tracks themselves?


Solution

  • 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']))
    

    and

        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)