diff --git a/database/music_database.py b/database/music_database.py index 8fa40672..6d23b7c2 100644 --- a/database/music_database.py +++ b/database/music_database.py @@ -107,6 +107,9 @@ class SimilarArtist: similarity_rank: int # 1-10, where 1 is most similar occurrence_count: int # How many watchlist artists share this similar artist last_updated: datetime + image_url: Optional[str] = None # Cached artist image + genres: Optional[List[str]] = None # Cached genres + popularity: int = 0 # Cached popularity score @dataclass class DiscoveryTrack: @@ -1054,8 +1057,16 @@ class MusicDatabase: cursor.execute("ALTER TABLE similar_artists ADD COLUMN last_featured TIMESTAMP") logger.info("Added last_featured column to similar_artists table for hero cycling") + # Migration: Add cached metadata columns to avoid API calls on every page load + if 'image_url' not in columns: + cursor.execute("ALTER TABLE similar_artists ADD COLUMN image_url TEXT") + cursor.execute("ALTER TABLE similar_artists ADD COLUMN genres TEXT") + cursor.execute("ALTER TABLE similar_artists ADD COLUMN popularity INTEGER DEFAULT 0") + cursor.execute("ALTER TABLE similar_artists ADD COLUMN metadata_updated_at TIMESTAMP") + logger.info("Added image_url, genres, popularity, metadata_updated_at columns to similar_artists for hero caching") + except Exception as e: - logger.error(f"Error adding last_featured column to similar_artists: {e}") + logger.error(f"Error adding columns to similar_artists: {e}") # Don't raise - this is a migration, database can still function def _fix_watchlist_spotify_id_nullable(self, cursor): @@ -5309,6 +5320,24 @@ class MusicDatabase: logger.error(f"Error updating similar artist iTunes ID: {e}") return False + def update_similar_artist_metadata(self, similar_artist_id: int, image_url: str = None, + genres: list = None, popularity: int = None) -> bool: + """Cache artist metadata (image, genres, popularity) to avoid repeated API calls""" + try: + with self._get_connection() as conn: + cursor = conn.cursor() + genres_json = json.dumps(genres) if genres else None + cursor.execute(""" + UPDATE similar_artists + SET image_url = ?, genres = ?, popularity = ?, metadata_updated_at = CURRENT_TIMESTAMP + WHERE id = ? + """, (image_url, genres_json, popularity or 0, similar_artist_id)) + conn.commit() + return cursor.rowcount > 0 + except Exception as e: + logger.error(f"Error updating similar artist metadata: {e}") + return False + def has_fresh_similar_artists(self, source_artist_id: str, days_threshold: int = 30, require_itunes: bool = True, require_spotify: bool = False, profile_id: int = 1) -> bool: """ Check if we have cached similar artists that are still fresh (