fix(spotify): reduce API rate limiting by lowering pagination and increasing interval
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.
This commit is contained in:
parent
6f5c06906a
commit
dc5f57360c
2 changed files with 6 additions and 5 deletions
|
|
@ -14,7 +14,7 @@ logger = get_logger("spotify_client")
|
||||||
# Global rate limiting variables
|
# Global rate limiting variables
|
||||||
_last_api_call_time = 0
|
_last_api_call_time = 0
|
||||||
_api_call_lock = threading.Lock()
|
_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():
|
def _get_min_api_interval():
|
||||||
"""Get configurable API interval from settings, falling back to default."""
|
"""Get configurable API interval from settings, falling back to default."""
|
||||||
|
|
|
||||||
|
|
@ -491,13 +491,14 @@ class SpotifyWorker:
|
||||||
spotify_artist_id = item['spotify_artist_id']
|
spotify_artist_id = item['spotify_artist_id']
|
||||||
artist_name = item['artist_name']
|
artist_name = item['artist_name']
|
||||||
|
|
||||||
# Fetch albums with pagination cap — Spotify returns 10/page, so max_pages=5
|
# Fetch albums with pagination cap — Spotify returns 10/page, so max_pages=2
|
||||||
# gives 50 albums (newest first). Avoids 20+ paginated calls for prolific artists
|
# gives 20 albums (newest first). Most libraries have <20 albums per artist.
|
||||||
# (e.g., 217 albums = 22 API calls without cap, vs 5 with cap)
|
# 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:
|
try:
|
||||||
spotify_albums = self.client.get_artist_albums(
|
spotify_albums = self.client.get_artist_albums(
|
||||||
spotify_artist_id, album_type='album,single,compilation', limit=50,
|
spotify_artist_id, album_type='album,single,compilation', limit=50,
|
||||||
max_pages=5
|
max_pages=2
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to get Spotify albums for artist '{artist_name}': {e}")
|
logger.error(f"Failed to get Spotify albums for artist '{artist_name}': {e}")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue