Getting track ids from mulitple albums from same artist using .json output from sp.albums in Spotipy

410 views Asked by At

I know there's multiple ways to do this - and I've successfully gotten all of the track ids from a single artist using a user playlist discography. But I'd like to come up with a Python program that doesn't rely on a preexisting user playlist.

What I'm trying to do is to extract the track ids from the albums' data in the .json file created when multiple albums are passed to sp.albums in Spotipy.

I have this list of album ids which I'm passing to sp.albums, which are all of the records from one of my favorite bands, Fugazi.

 album_ids = ['6ZyP2KPr2AkAbUWbsHmzEg','2bU6BaHfovn3rvxxxHSkWd','3tnzZhTTjRTExYc9odG0dt','1hatewdVYKH0xqEkdOLQLb','3kPxFCpws7ZyzI1cMME4dV','3zBQ2UZzCzgCnIkFYMOWlY','3QpmoxpWCacHLsZMhLn1ag','3ZnF1cPxlqB48RyLiecDnv','21wERoyBas2JCRCgysPDJX']

I'm then passing this list to sp.albums:

fugazi_songs = sp.albums(album_ids)

The .json file for fugazi_songs is way too long to post here.

But here's the code from the very beginning of it, that has the data from the first song from the first album in it:

{"albums": [{
"album_type": "album",
"artists": [{
    "external_urls": {
        "spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
    },
    "href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
    "id": "62sC6lUEWRjbFqXpMmOk4G",
    "name": "Fugazi",
    "type": "artist",
    "uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
}],
"available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
"copyrights": [{
    "text": "Dischord Records",
    "type": "C"
}, {
    "text": "Dischord Records",
    "type": "P"
}],
"external_ids": {
    "upc": "0643859181027"
},
"external_urls": {
    "spotify": "https://open.spotify.com/album/6ZyP2KPr2AkAbUWbsHmzEg"
},
"genres": [],
"href": "https://api.spotify.com/v1/albums/6ZyP2KPr2AkAbUWbsHmzEg",
"id": "6ZyP2KPr2AkAbUWbsHmzEg",
"images": [{
    "height": 640,
    "url": "https://i.scdn.co/image/ab67616d0000b2737c6b218a0797b44ace3f237b",
    "width": 640
}, {
    "height": 300,
    "url": "https://i.scdn.co/image/ab67616d00001e027c6b218a0797b44ace3f237b",
    "width": 300
}, {
    "height": 64,
    "url": "https://i.scdn.co/image/ab67616d000048517c6b218a0797b44ace3f237b",
    "width": 64
}],
"label": "Dischord Records",
"name": "First Demo",
"popularity": 32,
"release_date": "2014-11-18",
"release_date_precision": "day",
"total_tracks": 11,
"tracks": {
    "href": "https://api.spotify.com/v1/albums/6ZyP2KPr2AkAbUWbsHmzEg/tracks?offset=0&limit=50",
    "items": [{
        "artists": [{
            "external_urls": {
                "spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
            },
            "href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
            "id": "62sC6lUEWRjbFqXpMmOk4G",
            "name": "Fugazi",
            "type": "artist",
            "uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
        }],
        "available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
        "disc_number": 1,
        "duration_ms": 190459,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/4PKl6Tt8YQaJIJmlnwELi7"
        },
        "href": "https://api.spotify.com/v1/tracks/4PKl6Tt8YQaJIJmlnwELi7",
        "id": "4PKl6Tt8YQaJIJmlnwELi7",
        "is_local": false,
        "name": "Waiting Room - Demo",
        "preview_url": "https://p.scdn.co/mp3-preview/d9d339d1c8426d37d8a8ed30a8eb6e4a10ee84d3?cid=ff4c1e3515504e8397510b8d452c0eab",
        "track_number": 1,
        "type": "track",
        "uri": "spotify:track:4PKl6Tt8YQaJIJmlnwELi7"
    },

Here's the code for the first song from the second album, that comes right after the end of the first album's data:

{
    "album_type": "album",
    "artists": [{
        "external_urls": {
            "spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
        },
        "href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
        "id": "62sC6lUEWRjbFqXpMmOk4G",
        "name": "Fugazi",
        "type": "artist",
        "uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
    }],
    "available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
    "copyrights": [{
        "text": "Dischord Records",
        "type": "C"
    }, {
        "text": "Dischord Records",
        "type": "P"
    }],
    "external_ids": {
        "upc": "0718751962026"
    },
    "external_urls": {
        "spotify": "https://open.spotify.com/album/3tnzZhTTjRTExYc9odG0dt"
    },
    "genres": [],
    "href": "https://api.spotify.com/v1/albums/3tnzZhTTjRTExYc9odG0dt",
    "id": "3tnzZhTTjRTExYc9odG0dt",
    "images": [{
        "height": 640,
        "url": "https://i.scdn.co/image/ab67616d0000b273528034c2cfe590178ac6657a",
        "width": 640
    }, {
        "height": 300,
        "url": "https://i.scdn.co/image/ab67616d00001e02528034c2cfe590178ac6657a",
        "width": 300
    }, {
        "height": 64,
        "url": "https://i.scdn.co/image/ab67616d00004851528034c2cfe590178ac6657a",
        "width": 64
    }],
    "label": "Dischord Records",
    "name": "Instrument Soundtrack",
    "popularity": 53,
    "release_date": "1999",
    "release_date_precision": "year",
    "total_tracks": 18,
    "tracks": {
        "href": "https://api.spotify.com/v1/albums/3tnzZhTTjRTExYc9odG0dt/tracks?offset=0&limit=50",
        "items": [{
            "artists": [{
                "external_urls": {
                    "spotify": "https://open.spotify.com/artist/62sC6lUEWRjbFqXpMmOk4G"
                },
                "href": "https://api.spotify.com/v1/artists/62sC6lUEWRjbFqXpMmOk4G",
                "id": "62sC6lUEWRjbFqXpMmOk4G",
                "name": "Fugazi",
                "type": "artist",
                "uri": "spotify:artist:62sC6lUEWRjbFqXpMmOk4G"
            }],
            "available_markets": ["AD", "AE", "AL", "AR", "AT", "AU", "BA", "BE", "BG", "BH", "BO", "BR", "BY", "CA", "CH", "CL", "CO", "CR", "CY", "CZ", "DE", "DK", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FR", "GB", "GR", "GT", "HK", "HN", "HR", "HU", "ID", "IE", "IL", "IN", "IS", "IT", "JO", "JP", "KW", "KZ", "LB", "LI", "LT", "LU", "LV", "MA", "MC", "MD", "ME", "MK", "MT", "MX", "MY", "NI", "NL", "NO", "NZ", "OM", "PA", "PE", "PH", "PL", "PS", "PT", "PY", "QA", "RO", "RS", "RU", "SA", "SE", "SG", "SI", "SK", "SV", "TH", "TN", "TR", "TW", "UA", "US", "UY", "VN", "XK", "ZA"],
            "disc_number": 1,
            "duration_ms": 227000,
            "explicit": false,
            "external_urls": {
                "spotify": "https://open.spotify.com/track/7dcMZLssGtsCJq9VdI22XC"
            },
            "href": "https://api.spotify.com/v1/tracks/7dcMZLssGtsCJq9VdI22XC",
            "id": "7dcMZLssGtsCJq9VdI22XC",
            "is_local": false,
            "name": "Pink Frosty Demo",
            "preview_url": "https://p.scdn.co/mp3-preview/8dd834d95f6d4d74a36d71f24be422212545c881?cid=ff4c1e3515504e8397510b8d452c0eab",
            "track_number": 1,
            "type": "track",
            "uri": "spotify:track:7dcMZLssGtsCJq9VdI22XC"
        },

Parsing the fugazi_songs .json file shows me this hierarchy that points to an individual track id:

albums.item.tracks.items.item.artists.item.id

I'm using a for loop to try and get all of the track ids from every song that's in the .json file using this path.

track_ids = []
for i in range(len(fugazi_songs['albums'])):
track_ids.append(fugazi_songs['albums'][i]['tracks']['items'][i]['id'])

The output only gives me the track_ids for one album.

track_ids
['4PKl6Tt8YQaJIJmlnwELi7', '44ETQCGE31ypmEc5ut3WMe','18CrxbPSUzPlibPB7jjsbi','2iNaUrp1SRBFoJnJqhbwS8','3SdaSSuc4prTO9IKFZEvxd','21ObFu22NhjfhXJm51Idij','2jKNbuegISV7YPdvXu3vaB','7r5rLLUes9m2bKvdmESzBP','6JPcT2uMbQzmdp6VJFa9bA']

I'm sure the solution is easy - but I've spent hours trying to figure this out.

How can I get all of the tracks for multiple albums that are collected in a .json file that's generated from sp.albums?

Thank you so much for taking the time to read this and I appreciate your input!

1

There are 1 answers

1
D-E-N On

if i understand your code and data right, you are not getting all songs from one album, but the first song, of the first album, the second song from the second album and so on (please check it out). I think you have to change your code to this:

track_ids = []
for album_id in range(len(fugazi_songs['albums'])):
    for track_id in range(len(fugazi_songs['albums'][album_id]['tracks']['items']))
        track_ids.append(fugazi_songs['albums'][album_id]['tracks']['items'][track_id]['id'])