force refetch similar artists when Spotify IDs missing
When Spotify is enabled after populating similar artists with only iTunes IDs, the freshness check now detects missing Spotify IDs and triggers a refetch. This fixes the Discover page not showing data when switching from iTunes-only mode.
This commit is contained in:
parent
3cb88669e3
commit
4f1dc2c15f
3 changed files with 26 additions and 4 deletions
|
|
@ -685,7 +685,9 @@ class WatchlistScanner:
|
|||
source_artist_id = watchlist_artist.spotify_artist_id or watchlist_artist.itunes_artist_id or str(watchlist_artist.id)
|
||||
try:
|
||||
# Check if we have fresh similar artists cached (< 30 days old)
|
||||
if self.database.has_fresh_similar_artists(source_artist_id, days_threshold=30):
|
||||
# If Spotify is authenticated, also require Spotify IDs to be present
|
||||
spotify_authenticated = self.spotify_client and self.spotify_client.is_spotify_authenticated()
|
||||
if self.database.has_fresh_similar_artists(source_artist_id, days_threshold=30, require_spotify=spotify_authenticated):
|
||||
logger.info(f"Similar artists for {watchlist_artist.artist_name} are cached and fresh, skipping MusicMap fetch")
|
||||
# Even if cached, backfill missing iTunes IDs (seamless dual-source support)
|
||||
self._backfill_similar_artists_itunes_ids(source_artist_id)
|
||||
|
|
|
|||
|
|
@ -3212,15 +3212,16 @@ class MusicDatabase:
|
|||
logger.error(f"Error updating similar artist iTunes ID: {e}")
|
||||
return False
|
||||
|
||||
def has_fresh_similar_artists(self, source_artist_id: str, days_threshold: int = 30, require_itunes: bool = True) -> bool:
|
||||
def has_fresh_similar_artists(self, source_artist_id: str, days_threshold: int = 30, require_itunes: bool = True, require_spotify: bool = False) -> bool:
|
||||
"""
|
||||
Check if we have cached similar artists that are still fresh (< days_threshold old).
|
||||
Check if we have cached similar artists that are still fresh (<days_threshold old).
|
||||
Also checks that similar artists have the required provider IDs.
|
||||
|
||||
Args:
|
||||
source_artist_id: The source artist ID to check
|
||||
days_threshold: Maximum age in days to consider fresh
|
||||
require_itunes: If True, also requires iTunes IDs to be present (for seamless provider switching)
|
||||
require_spotify: If True, also requires Spotify IDs to be present (for Spotify discovery)
|
||||
|
||||
Returns True if we have recent data with required IDs, False if data is stale, missing, or incomplete.
|
||||
"""
|
||||
|
|
@ -3264,6 +3265,23 @@ class MusicDatabase:
|
|||
logger.debug(f"Similar artists for {source_artist_id} missing iTunes IDs ({id_row['has_itunes']}/{id_row['total']}), will refetch")
|
||||
return False
|
||||
|
||||
# Check if we have Spotify IDs (for Spotify discovery)
|
||||
if require_spotify:
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) as total,
|
||||
SUM(CASE WHEN similar_artist_spotify_id IS NOT NULL AND similar_artist_spotify_id != '' THEN 1 ELSE 0 END) as has_spotify
|
||||
FROM similar_artists
|
||||
WHERE source_artist_id = ?
|
||||
""", (source_artist_id,))
|
||||
id_row = cursor.fetchone()
|
||||
|
||||
if id_row and id_row['total'] > 0:
|
||||
# If less than 50% have Spotify IDs, consider stale and refetch
|
||||
spotify_ratio = id_row['has_spotify'] / id_row['total']
|
||||
if spotify_ratio < 0.5:
|
||||
logger.debug(f"Similar artists for {source_artist_id} missing Spotify IDs ({id_row['has_spotify']}/{id_row['total']}), will refetch")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -17724,7 +17724,9 @@ def start_watchlist_scan():
|
|||
watchlist_scan_state['current_phase'] = 'fetching_similar_artists'
|
||||
source_artist_id = artist.spotify_artist_id or artist.itunes_artist_id or str(artist.id)
|
||||
|
||||
if database.has_fresh_similar_artists(source_artist_id, days_threshold=30):
|
||||
# If Spotify is authenticated, also require Spotify IDs to be present
|
||||
spotify_authenticated = spotify_client and spotify_client.is_spotify_authenticated()
|
||||
if database.has_fresh_similar_artists(source_artist_id, days_threshold=30, require_spotify=spotify_authenticated):
|
||||
print(f" Similar artists for {artist.artist_name} are cached and fresh")
|
||||
# Still backfill missing iTunes IDs
|
||||
scanner._backfill_similar_artists_itunes_ids(source_artist_id)
|
||||
|
|
|
|||
Loading…
Reference in a new issue