created add_genres to allow batch add of artist/genre pairs, alongside add_genre for singletons

This commit is contained in:
codey 2025-04-22 15:04:46 -04:00
parent 6bc982f668
commit 40fa51af36

View File

@ -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:
"""