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:
parent
09a7465b22
commit
992be1a056
1 changed files with 77 additions and 89 deletions
|
|
@ -1105,11 +1105,10 @@ class SpotifyClient:
|
|||
if Spotify is unavailable or returns an error.
|
||||
"""
|
||||
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
|
||||
|
||||
# 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)
|
||||
if cached_results is not None:
|
||||
tracks = []
|
||||
|
|
@ -1121,11 +1120,9 @@ class SpotifyClient:
|
|||
if tracks:
|
||||
return tracks
|
||||
|
||||
# Skip Spotify if globally rate limited — fall through to fallback
|
||||
if self.is_rate_limited():
|
||||
logger.debug(f"Spotify rate limited, skipping track search for: {query}")
|
||||
use_spotify = False
|
||||
else:
|
||||
use_spotify = self.is_spotify_authenticated()
|
||||
|
||||
if use_spotify:
|
||||
try:
|
||||
results = self.sp.search(q=query, type='track', limit=effective_limit)
|
||||
tracks = []
|
||||
|
|
@ -1163,10 +1160,8 @@ class SpotifyClient:
|
|||
if Spotify is unavailable or returns an error.
|
||||
"""
|
||||
cache = get_metadata_cache()
|
||||
use_spotify = self.is_spotify_authenticated()
|
||||
|
||||
if use_spotify:
|
||||
# Check Spotify cache
|
||||
# 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', 'artist', query, min(limit, 10))
|
||||
if cached_results is not None:
|
||||
artists = []
|
||||
|
|
@ -1180,9 +1175,7 @@ class SpotifyClient:
|
|||
artists.sort(key=lambda a: (0 if a.name.lower().strip() == query_lower else 1))
|
||||
return artists
|
||||
|
||||
if self.is_rate_limited():
|
||||
logger.debug(f"Spotify rate limited, skipping artist search for: {query}")
|
||||
use_spotify = False
|
||||
use_spotify = self.is_spotify_authenticated()
|
||||
|
||||
if use_spotify:
|
||||
try:
|
||||
|
|
@ -1230,10 +1223,8 @@ class SpotifyClient:
|
|||
if Spotify is unavailable or returns an error.
|
||||
"""
|
||||
cache = get_metadata_cache()
|
||||
use_spotify = self.is_spotify_authenticated()
|
||||
|
||||
if use_spotify:
|
||||
# Check Spotify cache
|
||||
# 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', 'album', query, min(limit, 10))
|
||||
if cached_results is not None:
|
||||
albums = []
|
||||
|
|
@ -1245,12 +1236,9 @@ class SpotifyClient:
|
|||
if albums:
|
||||
return albums
|
||||
|
||||
use_spotify = self.is_spotify_authenticated()
|
||||
|
||||
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:
|
||||
results = self.sp.search(q=query, type='album', limit=min(limit, 10))
|
||||
albums = []
|
||||
|
|
|
|||
Loading…
Reference in a new issue