When I try to run this spotipy function it freezes and I don't understand why

50 Views Asked by At

The function:

def get_album_tracks(sp, album_id):
    try:
        album_tracks = sp.album_tracks(album_id)
        tracks = album_tracks["items"]

        # Create a list to hold track details
        track_details = []
        for track in tracks:
            track_info = {
                "track_name": track["name"],
                "track_id": track["id"],
                "artists": [artist["name"] for artist in track["artists"]],
            }
            track_details.append(track_info)

        return track_details

    except SpotifyException as e:
        print(f"Spotify API error {e.code}: {e.msg}")
        return None

I call the function with the album_id of selected ambient works from Aphex Twin

album_tracks = get_album_tracks(sp, "7aNclGRxTysfh6z0d8671k")
print(album_tracks)

I tried to catch errors but it does not print out errors. The search_album function works great.

1

There are 1 best solutions below

0
On

You missing tracks['items']

This is demo code

import spotipy
from spotipy.oauth2 import SpotifyOAuth

Client_ID   = "*************** your client ID ***************"
Client_Secret = "*********** your client secret ***********"
Redirect_Uri = "*********** Callback URL ***********"
album_id = '7aNclGRxTysfh6z0d8671k'

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id = Client_ID,
    client_secret= Client_Secret,
    redirect_uri=Redirect_Uri,
    scope='user-library-read'))

def Get_albums():
    tracks = sp.album_tracks(album_id)
    track_details = []
    for track in tracks['items']:
        track_info = {
            "track_name": track["name"],
            "track_id": track["id"],
            "artists": [artist["name"] for artist in track["artists"]],
        }
        track_details.append(track_info)
    return track_details

track_details = Get_albums()
for track in track_details:
    print(track)

This is result

enter image description here

You can use Web API directly instead of using 'spotipy'

import requests

CLIENT_ID = '*************** your client ID ***************'
CLIENT_SECRET = '*********** your client secret ***********'

AUTH_URL = 'https://accounts.spotify.com/api/token'
BASE_URL = 'https://api.spotify.com/v1/albums/'
album_id = '7aNclGRxTysfh6z0d8671k'

# POST
auth_response = requests.post(AUTH_URL, {
    'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
})

# convert the response to JSON
auth_response_data = auth_response.json()

# save the access token
access_token = auth_response_data['access_token']

# print(access_token)

headers = {
    'Authorization': 'Bearer {token}'.format(token=access_token)
}

# actual GET request with proper header
r = requests.get(BASE_URL + album_id, headers=headers)

r = r.json()

track_details = []
for track in r['tracks']['items']:
    track_info = {
        "track_name": track["name"],
        "track_id": track["id"],
        "artists": [artist["name"] for artist in track["artists"]],
    }
    track_details.append(track_info)

for track in track_details:
    print(track)

result enter image description here