Despite proper authorization and the user being correctly added as a tester, I’m getting this response from the audio-features endpoint:
I’d appreciate any help in resolving this issue. Thank you!
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# --- Configuration ---
CLIENT_ID = "my client id is here"
CLIENT_SECRET = "my client secret is here"
SCOPE = "user-top-read playlist-modify-public playlist-modify-private user-library-read user-read-private"
BPM_MIN = 120
BPM_MAX = 160
PLAYLIST_NAME = "Running Vibes"
TIME_RANGE = "long_term"
TRACK_LIMIT = 50
# --- Authenticate with Spotify ---
auth_manager = SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope=SCOPE,
cache_path=".cache"
)
sp = spotipy.Spotify(auth_manager=auth_manager)
# --- Get User Info ---
user_info = sp.current_user()
print("Current user info:", user_info)
print("Logged in as:", user_info.get("email", "[no email in scope]"))
print("Granted scopes:", auth_manager.scope)
# --- Fetch Top Tracks ---
print("Fetching top tracks...")
top_tracks = sp.current_user_top_tracks(limit=TRACK_LIMIT, time_range=TIME_RANGE)
valid_tracks = [track for track in top_tracks["items"] if track["id"] and track["type"] == "track"]
track_ids = [track["id"] for track in valid_tracks]
# --- Analyze BPM ---
print("Analyzing BPM...")
audio_features = []
for i in range(0, len(track_ids), 100😞
batch = track_ids[i:i + 100]
try:
print(f"Requesting audio features for batch {i // 100 + 1}: {batch}")
features = sp.audio_features(batch)
if features:
audio_features.extend(features)
else:
print(f"No features returned for batch {i // 100 + 1}")
except spotipy.exceptions.SpotifyException as e:
print(f"Failed to fetch audio features for batch {i // 100 + 1}: {e}")
print(f"Audio features fetched: {len(audio_features)} items")
for feat in audio_features:
if not feat:
print("⚠️ Missing feature (None)")
else:
print(f"🎵 {feat['id']} - Tempo: {feat.get('tempo', 'N/A')}")
# --- Filter Tracks by BPM ---
running_tracks = [track_ids[i] for i, feat in enumerate(audio_features)
if feat and BPM_MIN <= feat["tempo"] <= BPM_MAX]
print(f"Found {len(running_tracks)} tracks in BPM range {BPM_MIN}-{BPM_MAX}")
# --- Create or Get Playlist ---
user_id = user_info["id"]
playlists = sp.current_user_playlists()["items"]
playlist_id = next((pl["id"] for pl in playlists if pl["name"] == PLAYLIST_NAME), None)
if not playlist_id:
print(f"Creating new playlist: {PLAYLIST_NAME}")
playlist = sp.user_playlist_create(user=user_id, name=PLAYLIST_NAME, public=False)
playlist_id = playlist["id"]
else:
print(f"Updating existing playlist: {PLAYLIST_NAME}")
sp.playlist_replace_items(playlist_id, [])
# --- Add Tracks to Playlist ---
if running_tracks:
print("Adding tracks to playlist...")
sp.playlist_add_items(playlist_id, running_tracks)
else:
print("No tracks matched your BPM range.")
print("Done! Check your Spotify playlists.")