Need Help Getting ISRC Data from Spotify API
Hey everyone! I’m trying to build something that pulls all the music data for a specific artist from Spotify’s API. I want to get everything like album info, song details, and most importantly the ISRC numbers for each track.
I managed to write some code that gets basic stuff like album names, song titles, how long each track is, and Spotify URLs. But I can’t figure out how to also grab the ISRC codes. When I test a single song request manually, I can see the ISRC in the response, but my automated script doesn’t pick it up.
What I’m trying to collect:
- Album info (title, release date, type)
- Song info (name, length, track position)
- ISRC codes when they exist
- Any other useful metadata
Has anyone done something like this before? I’d really appreciate any tips on how to modify my approach to include ISRC data.
import requests
import json
import os
import pandas as pd
# API credentials
auth_token = 'your_token_here'
# Spotify endpoints
api_base = "https://api.spotify.com/v1/artists/1A2B3C4D5E6F7G8H9I0J/albums"
request_headers = {
"Authorization": f"Bearer {auth_token}"
}
# Get all albums for artist
def fetch_artist_albums(musician_id):
album_list = []
endpoint = api_base.format(musician_id)
while endpoint:
api_response = requests.get(endpoint, headers=request_headers)
response_data = api_response.json()
album_list.extend(response_data.get('items', []))
endpoint = response_data.get('next')
return album_list
# Get all songs from an album
def fetch_album_songs(release_id):
song_list = []
endpoint = f"https://api.spotify.com/v1/albums/{release_id}/tracks"
while endpoint:
api_response = requests.get(endpoint, headers=request_headers)
response_data = api_response.json()
song_list.extend(response_data.get('items', []))
endpoint = response_data.get('next')
return song_list
# Main execution
musician_id = "1A2B3C4D5E6F7G8H9I0J"
album_collection = fetch_artist_albums(musician_id)
# Process all songs
complete_tracklist = []
for release in album_collection:
release_tracks = fetch_album_songs(release['id'])
for song in release_tracks:
song['release'] = release
complete_tracklist.extend(release_tracks)
# Structure final output
final_output = []
for release in album_collection:
release_info = {
"release": release,
"songs": []
}
for song in complete_tracklist:
if song['release']['id'] == release['id']:
release_info['songs'].append(song)
final_output.append(release_info)
# Save to desktop
desktop_file = os.path.join(os.path.expanduser("~"), "Desktop", "artist_data.json")
with open(desktop_file, 'w', encoding='utf-8') as file:
json.dump(final_output, file, ensure_ascii=False, indent=4)
print(f"Data saved to: {desktop_file}")
The code runs fine and creates the files, but I’m missing the ISRC information. Any ideas on what I might be doing wrong?