Spotipy Help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Spotipy Help (/thread-17660.html) |
Spotipy Help - HierroDuc1 - Apr-19-2019 Hello! I'm using Spotipy to try to get some data on a specific artist. I'm following the instructions on this link (https://medium.com/@RareLoot/extracting-spotify-data-on-your-favourite-artist-via-python-d58bc92a4330), but can't seem to get past getting the album names and URIs. Once I get to the part that says "Grab the songs from each album", I don't really understand why the errors occur. Fairly new to Python, so this might be a stupid mistake I'm making. This is what I have so far: >>> import spotipy >>> from spotipy.oauth2 import SpotifyClientCredentials >>> >>> client_id = "......." >>> client_secret = "......" >>> >>> client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) >>> sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) >>> >>> name = "National,The" >>> >>> result = sp.search(name) >>> result['tracks']['items'][0]['artists'] [{'external_urls': {'spotify': 'https://open.spotify.com/artist/2cCUtGK9sDU2EoElnk0GNB'}, 'href': 'https://api.spotify.com/v1/artists/2cCUtGK9sDU2EoElnk0GNB', 'id': '2cCUtGK9sDU2EoElnk0GNB', 'name': 'The National', 'type': 'artist', 'uri': 'spotify:artist:2cCUtGK9sDU2EoElnk0GNB'}] >>> >>> artist_uri = result['tracks']['items'][0]['artists'][0]['uri'] >>> sp_albums = sp.artist_albums(artist_uri, album_type='album') >>> >>> album_names = [] >>> album_uris = [] >>> for i in range(len(sp_albums['items'])): album_names.append(sp_albums['items'][i]['name']) album_uris.append(sp_albums['items'][i]['uri']) >>> album_names ['Boxer Live in Brussels', 'Sleep Well Beast', 'Trouble Will Find Me', 'High Violet (Expanded Edition)', 'High Violet', 'High Violet', 'High Violet', 'Boxer', 'Boxer', 'Alligator', 'Cherry Tree', 'Cherry Tree', 'Cherry Tree', 'Sad Songs for Dirty Lovers', 'Sad Songs for Dirty Lovers', 'Sad Songs for Dirty Lovers', 'The National', 'The National', 'The National'] >>> album_uris ['spotify:album:44QWVzSZGGPFyJkTsfjzWA', 'spotify:album:6zG9PHw8dlMLIyRE9TEGGk', 'spotify:album:2JhR4tjuc3MIKa8v2JaKze', 'spotify:album:36VJqCEgUg3nj0Eyxtc1av', 'spotify:album:59gXPxZ8CwFaeknPxtxXHZ', 'spotify:album:722zk0GySQPLbB0p2Rxxd6', 'spotify:album:6k54ZuG2Z49HQRbJdO65b5', 'spotify:album:3hvjTC9OSOrf6SFQmfqLNX', 'spotify:album:2pG7mDkQhia2OyGE6fbkmJ', 'spotify:album:4rwdrCccOqPlRjFZq0H1M1', 'spotify:album:2kDz8nWLPl3jmrO3xeFUXL', 'spotify:album:4ZjXzOFdtpcaCZdIxsLJky', 'spotify:album:12G4rtxl2ZFl1BVsKUomB9', 'spotify:album:0HIkf7pfkZ3IGmEYgcCXnO', 'spotify:album:7C1aHOdnNjVn6X09RRHgTa', 'spotify:album:5MXOch8nt9sOQ5mvUtCWB6', 'spotify:album:7lyQDI1JwwvqHZPfaYhOLA', 'spotify:album:4mr0nXLuoWv0AleieUrRkR', 'spotify:album:4Y9k8jM1INrE8iB6upO0Iq'] RE: Spotipy Help - Yoriz - Apr-19-2019 The code that you have shown is all the code that is working correctly. Please post your version of the code from the part that says "Grab the songs from each album" and the full error trace back that is given? RE: Spotipy Help - HierroDuc1 - Apr-19-2019 This is where the errors occur. It says spotify_albums has not been defined. And even if I change the spotify_albums = {} to be on top, then it says album is not defined. def albumSongs(uri): album = uri #assign album uri to a_name spotify_albums[album] = {} #Creates dictionary for that specific album #Create keys-values of empty lists inside nested dictionary for album spotify_albums[album]['album'] = [] #create empty list spotify_albums[album]['track_number'] = [] spotify_albums[album]['id'] = [] spotify_albums[album]['name'] = [] spotify_albums[album]['uri'] = [] tracks = sp.album_tracks(album) #pull data on album tracks for n in range(len(tracks['items'])): #for each song track spotify_albums[album]['album'].append(album_names[album_count]) #append album name tracked via album_count spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number']) spotify_albums[album]['id'].append(tracks['items'][n]['id']) spotify_albums[album]['name'].append(tracks['items'][n]['name']) spotify_albums[album]['uri'].append(tracks['items'][n]['uri']) spotify_albums = {} album_count = 0 for i in album_uris: #each album albumSongs(i) print("Album " + str(album_names[album_count]) + " songs has been added to spotify_albums dictionary") album_count+=1 #Updates album count once all tracks have been added RE: Spotipy Help - Yoriz - Apr-19-2019 Looks like the indentation of some of the lines of function albumSongs are wrongTry this def albumSongs(uri): album = uri #assign album uri to a_name spotify_albums[album] = {} #Creates dictionary for that specific album #Create keys-values of empty lists inside nested dictionary for album spotify_albums[album]['album'] = [] #create empty list spotify_albums[album]['track_number'] = [] spotify_albums[album]['id'] = [] spotify_albums[album]['name'] = [] spotify_albums[album]['uri'] = [] tracks = sp.album_tracks(album) #pull data on album tracks for n in range(len(tracks['items'])): #for each song track spotify_albums[album]['album'].append(album_names[album_count]) #append album name tracked via album_count spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number']) spotify_albums[album]['id'].append(tracks['items'][n]['id']) spotify_albums[album]['name'].append(tracks['items'][n]['name']) spotify_albums[album]['uri'].append(tracks['items'][n]['uri']) spotify_albums = {} album_count = 0 for i in album_uris: #each album albumSongs(i) print("Album " + str(album_names[album_count]) + " songs has been added to spotify_albums dictionary") album_count+=1 #Updates album count once all tracks have been added RE: Spotipy Help - HierroDuc1 - Apr-19-2019 It still doesn't work. >>> def albumSongs(uri): album = uri >>> spotify_albums[album] = {} Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> spotify_albums[album] = {} NameError: name 'spotify_albums' is not defined >>> |