0

I am getting a list of my 50 top artists artistList. From this list I get the albums of each artist in artistAlbumsList, from which I then loop through the tracks of the artist's albums until I hit an explicit track in albumTracks.

(Spotify does not flag albums as explicit, only the tracks...this is why I have to loop through the tracks.)

If there is an explicit track, I flag the album as explicit and add the album to my new_albums list.

How come I am returning None? There are explicit tracks in all of these albums, so this is not the issue.

Additionally, if I instead try to call the update on the albums dictionary instead of new_albums, I get this error: AttributeError: 'NoneType' object has no attribute 'update'

import time
import spotipy
import spotipy.util as util
from config import spotify_token

def albumDrop():
    sp = spotipy.Spotify(auth=spotify_token)
    albums = {}
    new_albums = {}
    artistList = sp.current_user_top_artists(limit=50, offset=0, time_range='long_term')['items']
    for artist in artistList:
        print(artist["name"], artist["id"])
        artistAlbumsList = sp.artist_albums( artist["id"], album_type='album', limit=3)['items']
        for album in artistAlbumsList:
            print(album['release_date'], artist['name'], album['name'], album['external_urls']['spotify'], album['id'])
            albumTracks = sp.album_tracks(album['id'], limit=50)['items']
            for track in albumTracks:
                if track['explicit'] == True:
                    new_albums = albums.update({'{artistName} - {albumName}'.format(artistName = artist['name'], albumName = album['name']) : {'Release Date': album['release_date'], 'Artist Name': artist['name'], 'Album Name': album['name'], 'Album URI': album['external_urls']['spotify'], 'Album ID': album['id'], 'Explicit': track['explicit']}})
                    break
    return(new_albums)

if __name__ == "__main__":
    start = time.time()
    latestAlbums = albumDrop()
    end = time.time()
    print(latestAlbums)
    print(end - start)

Is there any faster way to do this also?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Alex
  • 486
  • 1
  • 7
  • 19

1 Answers1

2

albums.update or dict.update is an in-place operation which returns None.

You can use unpacking to create a new dict:

new_albums = {**albums, **{'{artistName} - {albumName}'.format(artistName = artist['name'], albumName = album['name']) : {'Release Date': album['release_date'], 'Artist Name': artist['name'], 'Album Name': album['name'], 'Album URI': album['external_urls']['spotify'], 'Album ID': album['id'], 'Explicit': track['explicit']}}}
Chris
  • 29,127
  • 3
  • 28
  • 51
  • 1
    Yes you can :) In such a case just `return(albums)` instead of `new_albums` – Chris Nov 09 '19 at 07:11
  • using just ```albums.update``` instead of ```new_albums = albums.update``` no longer returns ```None```. I do not wish to have to createra new dictionary, just update the current one. – Alex Nov 09 '19 at 07:12