From dc5f57360c39c0161369b39bb7ba36f2f763877a Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Sat, 11 Apr 2026 19:25:05 +0000 Subject: [PATCH] fix(spotify): reduce API rate limiting by lowering pagination and increasing interval MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spotify enforces undocumented hourly rate limits that trigger 429 errors after ~50 paginated API calls. Two changes to prevent hitting these limits: 1. Reduced max_pages from 5 to 2 in spotify_worker _process_album_batch - Cuts pagination calls by 60% (5→2 pages per artist) - Still fetches 20 newest albums, sufficient for most libraries 2. Increased MIN_API_INTERVAL from 0.35s to 0.70s - Reduces calls from ~171/min to ~85/min - Provides larger safety margin before rate limits trigger Both changes maintain full functionality - unmatched albums fall back to iTunes. --- core/spotify_client.py | 2 +- core/spotify_worker.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/spotify_client.py b/core/spotify_client.py index 2017dfa4..bf86048e 100644 --- a/core/spotify_client.py +++ b/core/spotify_client.py @@ -14,7 +14,7 @@ logger = get_logger("spotify_client") # Global rate limiting variables _last_api_call_time = 0 _api_call_lock = threading.Lock() -MIN_API_INTERVAL = 0.35 # Default: 350ms between API calls (~171/min, under Spotify's ~180/min limit) +MIN_API_INTERVAL = 0.70 # Default: 350ms between API calls (~171/min, under Spotify's ~180/min limit) def _get_min_api_interval(): """Get configurable API interval from settings, falling back to default.""" diff --git a/core/spotify_worker.py b/core/spotify_worker.py index 17342528..ff7ac832 100644 --- a/core/spotify_worker.py +++ b/core/spotify_worker.py @@ -491,13 +491,14 @@ class SpotifyWorker: spotify_artist_id = item['spotify_artist_id'] artist_name = item['artist_name'] - # Fetch albums with pagination cap — Spotify returns 10/page, so max_pages=5 - # gives 50 albums (newest first). Avoids 20+ paginated calls for prolific artists - # (e.g., 217 albums = 22 API calls without cap, vs 5 with cap) + # Fetch albums with pagination cap — Spotify returns 10/page, so max_pages=2 + # gives 20 albums (newest first). Most libraries have <20 albums per artist. + # Reduced from max_pages=5 to avoid Spotify's undocumented hourly rate limits + # which trigger after ~50 paginated calls (10 artists × 5 pages = 50 calls). try: spotify_albums = self.client.get_artist_albums( spotify_artist_id, album_type='album,single,compilation', limit=50, - max_pages=5 + max_pages=2 ) except Exception as e: logger.error(f"Failed to get Spotify albums for artist '{artist_name}': {e}")