def get_user_playlists(spotipy): ''' Returns set of all artists found within the current Spotify user's playlists. ''' def show_tracks(results): ''' helper function for get_user_playlists() ''' for i, item in enumerate(tracks['items']): track = item['track'] playlist_artists_list.append(track['artists'][0]['name']) playlist_artists_list = [] offset = 0 user_id = spotipy.current_user()['id'] playlists = spotipy.user_playlists(user_id) for playlist in playlists['items']: owner = playlist['owner']['id'] results = spotipy.user_playlist(owner, playlist['id'], fields="tracks,next") tracks = results['tracks'] show_tracks(tracks) while tracks['next']: tracks = spotipy.next(tracks) return set(playlist_artists_list)
def get_id_from_playlist(spotipy, user_id, name_playlist): ''' Returns spotify playlist ID, given a user_id and playlist name. ''' offset = 0 playlists = spotipy.user_playlists(user_id) user_playlists = {} # This will stored the users playlists for playlist in playlists['items']: if playlist['name'] == name_playlist: return playlist['id'] return 'Could not find id of new playlist'
try: spotipy.trace = False plistInput = input('Criar [1] ou Escolher Playlist [2]: ') if plistInput == '1': #cria playlist nova pName = input('Nome da playlist :') playlists = spotipy.user_playlist_create(user=sp_username, name=pName, public=False) print('playlist ' + pName + ' criada') playlist_id = playlists['id'] elif plistInput == '2': #exibe playlists do usuario para seleção offset = 1 all_playlists = [] while True: playlists = spotipy.user_playlists(sp_username, offset=offset) for i, item in enumerate(playlists['items']): #print("%d %s" %(i, item['name'])) offset = offset + i all_playlists.append(item) if len(playlists['items']) == 0: break for i, item in enumerate(all_playlists): print("%d %s" % (i, item['name'])) pNumber = input("Please choose playlist number: ") playlist = all_playlists[int(pNumber)] playlist_id = playlist['id'] #inicializa variavel answer answer = 's'