mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 17:23:53 +00:00
* - Added scope, 'playlist-read-private', to examples that access user playlists using the spotipy api: current_user_playlists() (fixes #591) * Fix example to use user_playlists again Co-authored-by: Stephane Bruckert <stephane.bruckert@gmail.com>
35 lines
1003 B
Python
35 lines
1003 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'])
|
|
|
|
results = sp.playlist(playlist['id'], fields="tracks,next")
|
|
tracks = results['tracks']
|
|
show_tracks(tracks)
|
|
|
|
while tracks['next']:
|
|
tracks = sp.next(tracks)
|
|
show_tracks(tracks)
|