Use cache before Spotify auth checks

Allow cached Spotify search results to return even when Spotify is rate-limited or temporarily unavailable, and remove redundant rate-limit gating after auth checks.
This commit is contained in:
Antti Kettunen 2026-04-16 09:54:00 +03:00
parent 09a7465b22
commit 992be1a056

View file

@ -1105,11 +1105,10 @@ class SpotifyClient:
if Spotify is unavailable or returns an error. if Spotify is unavailable or returns an error.
""" """
cache = get_metadata_cache() cache = get_metadata_cache()
use_spotify = self.is_spotify_authenticated()
if use_spotify:
# Check Spotify cache
effective_limit = min(limit, 50) # Spotify API max is 50 effective_limit = min(limit, 50) # Spotify API max is 50
# Check Spotify cache first so cached data remains usable even when
# Spotify is temporarily unavailable or rate limited.
cached_results = cache.get_search_results('spotify', 'track', query, effective_limit) cached_results = cache.get_search_results('spotify', 'track', query, effective_limit)
if cached_results is not None: if cached_results is not None:
tracks = [] tracks = []
@ -1121,11 +1120,9 @@ class SpotifyClient:
if tracks: if tracks:
return tracks return tracks
# Skip Spotify if globally rate limited — fall through to fallback use_spotify = self.is_spotify_authenticated()
if self.is_rate_limited():
logger.debug(f"Spotify rate limited, skipping track search for: {query}") if use_spotify:
use_spotify = False
else:
try: try:
results = self.sp.search(q=query, type='track', limit=effective_limit) results = self.sp.search(q=query, type='track', limit=effective_limit)
tracks = [] tracks = []
@ -1163,10 +1160,8 @@ class SpotifyClient:
if Spotify is unavailable or returns an error. if Spotify is unavailable or returns an error.
""" """
cache = get_metadata_cache() cache = get_metadata_cache()
use_spotify = self.is_spotify_authenticated() # Check Spotify cache first so cached data remains usable even when
# Spotify is temporarily unavailable or rate limited.
if use_spotify:
# Check Spotify cache
cached_results = cache.get_search_results('spotify', 'artist', query, min(limit, 10)) cached_results = cache.get_search_results('spotify', 'artist', query, min(limit, 10))
if cached_results is not None: if cached_results is not None:
artists = [] artists = []
@ -1180,9 +1175,7 @@ class SpotifyClient:
artists.sort(key=lambda a: (0 if a.name.lower().strip() == query_lower else 1)) artists.sort(key=lambda a: (0 if a.name.lower().strip() == query_lower else 1))
return artists return artists
if self.is_rate_limited(): use_spotify = self.is_spotify_authenticated()
logger.debug(f"Spotify rate limited, skipping artist search for: {query}")
use_spotify = False
if use_spotify: if use_spotify:
try: try:
@ -1230,10 +1223,8 @@ class SpotifyClient:
if Spotify is unavailable or returns an error. if Spotify is unavailable or returns an error.
""" """
cache = get_metadata_cache() cache = get_metadata_cache()
use_spotify = self.is_spotify_authenticated() # Check Spotify cache first so cached data remains usable even when
# Spotify is temporarily unavailable or rate limited.
if use_spotify:
# Check Spotify cache
cached_results = cache.get_search_results('spotify', 'album', query, min(limit, 10)) cached_results = cache.get_search_results('spotify', 'album', query, min(limit, 10))
if cached_results is not None: if cached_results is not None:
albums = [] albums = []
@ -1245,12 +1236,9 @@ class SpotifyClient:
if albums: if albums:
return albums return albums
use_spotify = self.is_spotify_authenticated()
if use_spotify: if use_spotify:
# Skip Spotify if globally rate limited — fall through to fallback
if self.is_rate_limited():
logger.debug(f"Spotify rate limited, skipping album search for: {query}")
use_spotify = False
else:
try: try:
results = self.sp.search(q=query, type='album', limit=min(limit, 10)) results = self.sp.search(q=query, type='album', limit=min(limit, 10))
albums = [] albums = []