From 40fa51af36af09e070ad6ae84c4a37aa75ad42e9 Mon Sep 17 00:00:00 2001 From: codey Date: Tue, 22 Apr 2025 15:04:46 -0400 Subject: [PATCH] created add_genres to allow batch add of artist/genre pairs, alongside add_genre for singletons --- utils/radio_util.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/utils/radio_util.py b/utils/radio_util.py index 9438ae4..9473e14 100644 --- a/utils/radio_util.py +++ b/utils/radio_util.py @@ -185,7 +185,12 @@ class RadioUtil: params: tuple[str, str] = (artist, genre) res = await _db.execute_insert(query, params) if res: + logging.debug("Query executed successfully for %s/%s, committing", + artist, genre) + await _db.commit() return True + logging.debug("Failed to store artist/genre pair: %s/%s (res: %s)", + artist, genre, res) return False except Exception as e: logging.info("Failed to store artist/genre pair: %s/%s (%s)", @@ -193,6 +198,47 @@ class RadioUtil: traceback.print_exc() return False + async def add_genres(self, pairs: list[dict[str, str]]) -> bool: + """ + (BATCH) Add artist/genre pairings to DB + Expects list of dicts comprised of artist name (key), genre (value) + Args: + pairs (list[dict[str, str]]): Pairs of artist/genres to add, list of dicts + Returns: + bool + """ + try: + added_rows: int = 0 + async with sqlite3.connect(self.artist_genre_db_path, timeout=2) as _db: + for pair in pairs: + try: + artist, genre = pair + query: str = "INSERT OR IGNORE INTO artist_genre (artist, genre) VALUES(?, ?)" + params: tuple[str, str] = (artist, genre) + res = await _db.execute_insert(query, params) + if res: + logging.debug("add_genres: Query executed successfully for %s/%s", + artist, genre) + added_rows += 1 + else: + logging.debug("Failed to store artist/genre pair: %s/%s (res: %s)", + artist, genre, res) + except Exception as e: + logging.info("Failed to store artist/genre pair: %s/%s (%s)", + artist, genre, str(e)) + continue + if added_rows: + logging.info("add_genres: Committing %s rows", added_rows) + await _db.commit() + return True + logging.info("add_genres: Failed (No rows added)") + return False + except Exception as e: + logging.info("Failed to store artist/genre pairs: %s", + str(e)) + traceback.print_exc() + return False + async def get_genre(self, artist: str) -> str: """