Search code examples
pythonspotipy

python spotipy playlists[items] is empty


Using the code below I can retrieve a playlists object for my user but the list for the items entry is empty. I have a couple hundred playlists so I must be missing something in this code.

 import spotipy
 import spotipy.util as util

 username='xxxxx'
 clientid='xxxxx'
 clientsecret='xxxxx'
 redirecturi='http://localhost'
 thescope='user-library-read'

 print("Requesting token...")
 token = util.prompt_for_user_token(username,scope=thescope,client_id=clientid,client_secret=clientsecret,redirect_uri=redirecturi)
 print("Token is %s" % token)
 if token:
     sp = spotipy.Spotify(auth=token)
     playlists = sp.user_playlists(username)
     print("Playlists are ", playlists)
 else:
     print "Can't get token for", username

And output is :

 Requesting token...
 Token is<token>
 ('Playlists are ', {u'items': [], u'next': None, u'href': u'https://api.spotify.com/v1/users/havanon/playlists?offset=0&limit=50', u'limit': 50, u'offset': 0, u'total': 0, u'previous': None})

Solution

  • This seems to work. It does not return playlist folders though. And I haven't yet found any spotipy mention of such.

     #!/usr/bin/env python2
     import sys
     import spotipy
     import spotipy.util as util
    
     username='xxx'
     clientid='xxx'
     clientsecret='xxx'
     redirecturi='http://localhost'
     thescope='playlist-read-private'
    
     token = util.prompt_for_user_token(username,scope=thescope,client_id=clientid,client_secret=clientsecret,redirect_uri=redirecturi)
     if token:
         sp = spotipy.Spotify(auth=token)
         playlists = sp.current_user_playlists()
         while playlists:
             for i, playlist in enumerate(playlists['items']):
                 print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'],  playlist['name']))
             if playlists['next']:
                 print("getting next 50")
                 playlists = sp.next(playlists)
             else:
                 playlists = None
     else:
         print ("Can't get token for", username)