【问题标题】:New to using Spotipy and Python3, I want to use new_releases() to print Artist name and the artist album刚开始使用 Spotipy 和 Python3,我想使用 new_releases() 打印艺术家姓名和艺术家专辑
【发布时间】:2021-03-05 02:54:37
【问题描述】:

到目前为止,我可以使用我选择的人打印出所有专辑

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'])

我试图通过这样做来为 new_releases() 做一个类似的过程

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

但这会在test = newReleases['items'] 行引发错误。如果有人熟悉 Spotipy 并且知道如何从 new_releases() 返回诸如发布日期、艺术家姓名、专辑名称之类的信息,我将不胜感激。

【问题讨论】:

    标签: python-3.x api spotify spotipy


    【解决方案1】:

    我有点困惑,因为the documentationnew_releases 方法返回一个列表。无论如何,它是一个包含列表的单项字典。

    但是,该列表包含的字典似乎有点笨拙,所以我理解您为什么要问这个问题。

    您可以利用collections.namedtuple 数据结构更容易查看相关信息。我并没有声称这是转换这些数据的最佳方法,但在我看来这是一种不错的方法。

    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'])) 
    

    这会产生以下命名元组列表:

    [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')]
    

    如果您想在此列表中查看与第 11 张专辑相关的艺术家,您可以这样做:

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

    编辑:在对此答案的评论中,OP 也要求获取专辑封面。

    请查看辅助函数,并在下面稍微修改代码:

    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'])) 
    

    结果:

    [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')]
    

    【讨论】:

    • 非常感谢!最好的方法与否我现在可以完美地工作!我想做的最后一件事是尝试为每个新版本获取专辑封面。有什么想法吗?
    • @Rogicar:干杯!对于专辑封面,我使用requests 模块合并了一些快速图片下载代码。请查看编辑后的答案。
    猜你喜欢
    • 2012-02-15
    • 1970-01-01
    • 2013-01-24
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多