简体   繁体   中英

spotipy spotify python api get all of an artist's releases

i want to use the spotify api to get all releases by an artist. I think there are two ways I could do this, by curling the api url, or using the Spotipy python api ( https://spotipy.readthedocs.io/en/latest/ ). I've been using the spotipy api but am having trouble getting all releases for artists with a large discography like New Order for example (spotify:artist:0yNLKJebCb8Aueb54LYya3)

If I make a request like this :

token = util.oauth2.SpotifyClientCredentials(client_id="5a**********0478", client_secret="06c0a2b*******2aabe893")
cache_token = token.get_access_token()
spotify = spotipy.Spotify(cache_token)
newOrderURI = "spotify:artist:0yNLKJebCb8Aueb54LYya3"
artistAlbums = spotify.artist_albums(newOrderURI)

The value of artistAlbums looks like this:

{
"href": "https: //api.spotify.com/v1/artists/0yNLKJebCb8Aueb54LYya3/albums?offset=0&limit=20&include_groups=album,single,compilation,appears_on",
"items": [
    {
        "album_group": "album",
        "album_type": "album",
        "artists": [
            {
                "external_urls": {
                    "spotify": "https://open.spotify.com/artist/0yNLKJebCb8Aueb54LYya3"
                },
                "href": "https://api.spotify.com/v1/artists/0yNLKJebCb8Aueb54LYya3",
                "id": "0yNLKJebCb8Aueb54LYya3",
                "name": "New Order",
                "type": "artist",
                "uri": "spotify:artist:0yNLKJebCb8Aueb54LYya3"
            }
        ],
        "available_markets": [
            "CA",
            "US"
        ],
        "external_urls": {
            "spotify": "https://open.spotify.com/album/3tVxsOv2aTeaF3zsuWsd3e"
        },
        "href": "https://api.spotify.com/v1/albums/3tVxsOv2aTeaF3zsuWsd3e",
        "id": "3tVxsOv2aTeaF3zsuWsd3e",
        "images": [
            {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b27376ca654fa945d476a2a4d5cb",
                "width": 640
            },
            {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e0276ca654fa945d476a2a4d5cb",
                "width": 300
            },
            {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d0000485176ca654fa945d476a2a4d5cb",
                "width": 64
            }
        ],
        "name": "∑(No,12k,Lg,17Mif) New Order + Liam Gillick: So it goes.. (Live at MIF)",
        "release_date": "2019-07-12",
        "release_date_precision": "day",
        "total_tracks": 18,
        "type": "album",
        "uri": "spotify:album:3tVxsOv2aTeaF3zsuWsd3e"
    },
    {
        "album_group": "album",
        "album_type": "album",
        "artists": [
            {
                "external_urls": {
                    "spotify": "https://open.spotify.com/artist/0yNLKJebCb8Aueb54LYya3"
                },
                "href": "https://api.spotify.com/v1/artists/0yNLKJebCb8Aueb54LYya3",
                "id": "0yNLKJebCb8Aueb54LYya3",
                "name": "New Order",
                "type": "artist",
                "uri": "spotify:artist:0yNLKJebCb8Aueb54LYya3"
            }
        ],
        "available_markets": [
            "JP"
        ],
        "external_urls": {
            "spotify": "https://open.spotify.com/album/3tC3rPZOFRoTCFcvDegigj"
        },
        "href": "https://api.spotify.com/v1/albums/3tC3rPZOFRoTCFcvDegigj",
        "id": "3tC3rPZOFRoTCFcvDegigj",
        "images": [
            {
                "height": 640,
                "url": "https://i.scdn.co/image/ab67616d0000b273c841b10966ce9d95bef0a43e",
                "width": 640
            },
            {
                "height": 300,
                "url": "https://i.scdn.co/image/ab67616d00001e02c841b10966ce9d95bef0a43e",
                "width": 300
            },
            {
                "height": 64,
                "url": "https://i.scdn.co/image/ab67616d00004851c841b10966ce9d95bef0a43e",
                "width": 64
            }
        ],
        "name": "Music Complete",
        "release_date": "2015",
        "release_date_precision": "year",
        "total_tracks": 5,
        "type": "album",
        "uri": "spotify:album:3tC3rPZOFRoTCFcvDegigj"
    }
],
"limit": 20,
"next": "https://api.spotify.com/v1/artists/0yNLKJebCb8Aueb54LYya3/albums?offset=20&limit=20&include_groups=album,single,compilation,appears_on",
"offset": 0,
"previous": None,
"total": 174

}

I removed alot of entries from the items[] list so it would be easier to read in this question, but basically each item entry is an album/release, but one request does not have every release from the artist. The 'next' string at the end makes me think that going to that would give me the rest of the artis's albums, but I don't think its something I can make a request on using the spotipy python library.

SO my question, is how can I use the spotipy library to get every release from an artist as fast as possible? any help is appreciated. thank you

got it:

    while results['next']:
    results = spotify.next(results)
    albums.extend(results['items'])

for album in albums:
    print(album['name'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM