From 4f1dc2c15f4ea3c96ec5c66140642969f40f979b Mon Sep 17 00:00:00 2001 From: Broque Thomas Date: Sat, 24 Jan 2026 23:46:01 -0800 Subject: [PATCH] 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. --- core/watchlist_scanner.py | 4 +++- database/music_database.py | 22 ++++++++++++++++++++-- web_server.py | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/core/watchlist_scanner.py b/core/watchlist_scanner.py index 367ebddf..7b5f96e2 100644 --- a/core/watchlist_scanner.py +++ b/core/watchlist_scanner.py @@ -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) diff --git a/database/music_database.py b/database/music_database.py index 2645e2ec..e06b232b 100644 --- a/database/music_database.py +++ b/database/music_database.py @@ -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 ( 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: diff --git a/web_server.py b/web_server.py index 0476b16b..6448b616 100644 --- a/web_server.py +++ b/web_server.py @@ -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)