spotipy/examples/user_playlists_contents.py
Jack Dane 97c9917a02 Use the playlist_items function to retrieve tracks.
* It will request the same endpoint as sp.next with the same fields for consistency.
2024-05-06 10:43:21 +01:00

34 lines
999 B
Python

# Shows a user's playlists (need to be authenticated via oauth)
import spotipy
from spotipy.oauth2 import SpotifyOAuth
def show_tracks(results):
for i, item in enumerate(results['items']):
track = item['track']
print(
" %d %32.32s %s" %
(i, track['artists'][0]['name'], track['name']))
if __name__ == '__main__':
scope = 'playlist-read-private'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
playlists = sp.current_user_playlists()
user_id = sp.me()['id']
for playlist in playlists['items']:
if playlist['owner']['id'] == user_id:
print()
print(playlist['name'])
print(' total tracks', playlist['tracks']['total'])
tracks = sp.playlist_items(playlist['id'], fields="items,next", additional_types=('tracks', ))
show_tracks(tracks)
while tracks['next']:
tracks = sp.next(tracks)
show_tracks(tracks)