Rogicar
Rogicar

Reputation: 7

New to using Spotipy and Python3, I want to use new_releases() to print Artist name and the artist album

So far I was able to print out all albums by a person of my choosing using this

spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(client_id, client_secret))
results = spotify.artist_albums(posty_uri, album_type='album')
albums = results['items']
while results['next']:
    results = spotify.next(results)
    albums.extend(results['items'])
    
for album in albums:        
        print(album['name'])

I was trying to do a similar process for new_releases() by doing this

newReleases = spotify.new_releases()
test = newReleases['items']

but this throws me an error on the line test = newReleases['items']. If anyone is familiar with Spotipy and knows how to return things like release date, artist name, album name from new_releases() I would greatly appreciate it.

Upvotes: 0

Views: 185

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169424

I'm a little confused because the documentation says that the new_releases method returns a list. In any event, it is a one-item dictionary which contains a list.

However that list contains dictionaries which seem a bit unwieldy, so I understand why you're asking this question.

You can make use of the collections.namedtuple data structure to make it easier to see the relevant information. I don't claim that this is the best way to transform this data, but it seems to me a decent way.

import collecdtions as co 

# namedtuple data structure that will be easier to understand and use 
Album = co.namedtuple(typename='Album',field_names=['album_name',
                                                    'artist_name',
                                                    'release_date'])
newReleases2 = [] # couldn't think of a better name

for album in newReleases['albums']['items']: 
    artist_sublist = [] 
    for artist in album['artists']: 
        artist_sublist.append(artist['name'])
    newReleases2.append(Album(album_name=album['name'],
                              artist_name=artist_sublist,
                              release_date=album['release_date'])) 

This results in the following list of namedtuples:

[Album(album_name='Only Wanna Be With You (Pokémon 25 Version)', artist_name=['Post Malone'], release_date='2021-02-25'),
 Album(album_name='AP (Music from the film Boogie)', artist_name=['Pop Smoke'], release_date='2021-02-26'),
 Album(album_name='Like This', artist_name=['2KBABY', 'Marshmello'], release_date='2021-02-26'),
 Album(album_name='Go Big (From The Amazon Original Motion Picture Soundtrack Coming 2 America)', artist_name=['YG', 'Big Sean'], release_date='2021-02-26'),
 Album(album_name='Here Comes The Shock', artist_name=['Green Day'], release_date='2021-02-21'),
 Album(album_name='Spaceman', artist_name=['Nick Jonas'], release_date='2021-02-25'),
 Album(album_name='Life Support', artist_name=['Madison Beer'], release_date='2021-02-26'),
 Album(album_name="Drunk (And I Don't Wanna Go Home)", artist_name=['Elle King', 'Miranda Lambert'], release_date='2021-02-26'),
 Album(album_name='PROBLEMA', artist_name=['Daddy Yankee'], release_date='2021-02-26'),
 Album(album_name='Leave A Little Love', artist_name=['Alesso', 'Armin van Buuren'], release_date='2021-02-26'),
 Album(album_name='Rotate', artist_name=['Becky G', 'Burna Boy'], release_date='2021-02-22'),
 Album(album_name='BED', artist_name=['Joel Corry', 'RAYE', 'David Guetta'], release_date='2021-02-26'),
 Album(album_name='A N N I V E R S A R Y (Deluxe)', artist_name=['Bryson Tiller'], release_date='2021-02-26'),
 Album(album_name='Little Oblivions', artist_name=['Julien Baker'], release_date='2021-02-26'),
 Album(album_name='Money Long (feat. 42 Dugg)', artist_name=['DDG', 'OG Parker'], release_date='2021-02-26'),
 Album(album_name='El Madrileño', artist_name=['C. Tangana'], release_date='2021-02-26'),
 Album(album_name='Skegee', artist_name=['JID'], release_date='2021-02-23'),
 Album(album_name='Coyote Cry', artist_name=['Ian Munsick'], release_date='2021-02-26'),
 Album(album_name='Rainforest', artist_name=['Noname'], release_date='2021-02-26'),
 Album(album_name='The American Negro', artist_name=['Adrian Younge'], release_date='2021-02-26')]

If you wanted to see the artist(s) associated with the 11th album in this list, you could do this:

In [62]: newReleases2[10].artist_name                                                               
Out[62]: ['Becky G', 'Burna Boy']

Edit: in a comment on this answer, OP requested getting album cover as well.

Please see helper function, and slightly modified code below:

import os
import requests

def download_album_cover(url):
    # helper function to download album cover 
    # using code from: https://stackoverflow.com/a/13137873/42346
    download_path = os.getcwd() + os.sep + url.rsplit('/', 1)[-1] 
    r = requests.get(url, stream=True) 
    if r.status_code == 200: 
        with open(download_path, 'wb') as f: 
            for chunk in r.iter_content(1024): 
                f.write(chunk) 
        return download_path 

# modified data structure
Album = co.namedtuple(typename='Album',field_names=['album_name',
                                                    'album_cover',
                                                    'artist_name',
                                                    'release_date'])

# modified retrieval code
newReleases2 = []                                                                          

for album in newReleases['albums']['items']: 
    album_cover = download_album_cover(album['images'][0]['url']) 
    artist_sublist = []  
    for artist in album['artists']:  
        artist_sublist.append(artist['name']) 
    newReleases2.append(Album(album_name=album['name'], 
                              album_cover=album_cover, 
                              artist_name=artist_sublist, 
                              release_date=album['release_date'])) 

Result:

[Album(album_name='Scary Hours 2', album_cover='/home/adamcbernier/ab67616d0000b2738b20e4631fa15d3953528bbc', artist_name=['Drake'], release_date='2021-03-05'),
 Album(album_name='Boogie: Original Motion Picture Soundtrack', album_cover='/home/adamcbernier/ab67616d0000b27395e532805e8c97be7a551e3a', artist_name=['Various Artists'], release_date='2021-03-05'),
 Album(album_name='Hold On', album_cover='/home/adamcbernier/ab67616d0000b273f33d3618aca6b3cfdcd2fc43', artist_name=['Justin Bieber'], release_date='2021-03-05'),
 Album(album_name='Serotonin', album_cover='/home/adamcbernier/ab67616d0000b2737fb30ee0638c764d6f3247d2', artist_name=['girl in red'], release_date='2021-03-03'),
 Album(album_name='Leave The Door Open', album_cover='/home/adamcbernier/ab67616d0000b2736f9e6abbd6fa43ac3cdbeee0', artist_name=['Bruno Mars', 'Anderson .Paak', 'Silk Sonic'], release_date='2021-03-05'),
 Album(album_name='Real As It Gets (feat. EST Gee)', album_cover='/home/adamcbernier/ab67616d0000b273f0f6f6144929a1ff72001f5e', artist_name=['Lil Baby', 'EST Gee'], release_date='2021-03-04'),
 Album(album_name='Life’s A Mess II (with Clever & Post Malone)', album_cover='/home/adamcbernier/ab67616d0000b2732e8d23414fd0b81c35bdedea', artist_name=['Juice WRLD'], release_date='2021-03-05'),
 Album(album_name='slower', album_cover='/home/adamcbernier/ab67616d0000b273b742c96d78d9091ce4a1c5c1', artist_name=['Tate McRae'], release_date='2021-03-03'),
 Album(album_name='Sacrifice', album_cover='/home/adamcbernier/ab67616d0000b27398bfcce8be630dd5f2f346e4', artist_name=['Bebe Rexha'], release_date='2021-03-05'),
 Album(album_name='Poster Girl', album_cover='/home/adamcbernier/ab67616d0000b273503b16348e47bc3c1c823eba', artist_name=['Zara Larsson'], release_date='2021-03-05'),
 Album(album_name='Beautiful Mistakes (feat. Megan Thee Stallion)', album_cover='/home/adamcbernier/ab67616d0000b273787f41be59050c46f69db580', artist_name=['Maroon 5', 'Megan Thee Stallion'], release_date='2021-03-03'),
 Album(album_name='Pay Your Way In Pain', album_cover='/home/adamcbernier/ab67616d0000b273a1e1b4608e1e04b40113e6e1', artist_name=['St. Vincent'], release_date='2021-03-04'),
 Album(album_name='My Head is a Moshpit', album_cover='/home/adamcbernier/ab67616d0000b2733db806083e3b649f1d969a4e', artist_name=['Verzache'], release_date='2021-03-05'),
 Album(album_name='When You See Yourself', album_cover='/home/adamcbernier/ab67616d0000b27377253620f08397c998d18d78', artist_name=['Kings of Leon'], release_date='2021-03-05'),
 Album(album_name='Mis Manos', album_cover='/home/adamcbernier/ab67616d0000b273d7210e8d6986196b28d084ef', artist_name=['Camilo'], release_date='2021-03-04'),
 Album(album_name='Retumban2', album_cover='/home/adamcbernier/ab67616d0000b2738a79a82236682469aecdbbdf', artist_name=['Ovi'], release_date='2021-03-05'),
 Album(album_name='Take My Hand', album_cover='/home/adamcbernier/ab67616d0000b273b7839c3ba191de59f5d3a3d7', artist_name=['LP Giobbi'], release_date='2021-03-05'),
 Album(album_name="Ma' G", album_cover='/home/adamcbernier/ab67616d0000b27351b5ebb959c37913ac61b033', artist_name=['J Balvin'], release_date='2021-02-28'),
 Album(album_name='Aspen', album_cover='/home/adamcbernier/ab67616d0000b27387d1d17d16cf131765ce4be8', artist_name=['Young Dolph', 'Key Glock'], release_date='2021-03-05'),
 Album(album_name='Only The Family - Lil Durk Presents: Loyal Bros', album_cover='/home/adamcbernier/ab67616d0000b273a3df38e11e978b34b47583d0', artist_name=['Only The Family'], release_date='2021-03-05')]

Upvotes: 2

Related Questions