From f87c9a34c5ed86c0064d72f46647d29184052b2f Mon Sep 17 00:00:00 2001 From: djairoh Date: Thu, 6 Feb 2025 20:41:39 +0100 Subject: [PATCH] moved from list to dict for export --- backerupper.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/backerupper.py b/backerupper.py index 0db8016..880ab61 100755 --- a/backerupper.py +++ b/backerupper.py @@ -30,6 +30,7 @@ def get_user_id(db: str, name: str) -> str: print(f"Failed to read id from {db}: {e}") return '' + class ExportService(): @staticmethod def exp(db: str, user_id: str): @@ -39,43 +40,45 @@ class ExportService(): @staticmethod - def get_annotations(db: str, user_id: str) -> list: + def get_annotations(db: str, user_id: str) -> dict: """Fetches all annotations belonging to the user with id from the provided sql3 navidrome database . input: db: path to a navidrome sql3 database. This database must contain at least the annotation and media_file tables. user_id: the user_id to fetch annotations from. output: - list of annotations in the format (path, playCount, rating, starred). + dict of annotations in the format {path: (playCount, rating, starred)}. """ + annos = {} try: conn = sqlite3.connect(db) cursor = conn.cursor() cursor.execute(f"""SELECT path,play_count,rating,starred FROM annotation INNER JOIN media_file ON item_id=id WHERE user_id='{user_id}' AND item_type='media_file'""") - annos = cursor.fetchall() + for p,pc,r,s in cursor.fetchall(): + annos[p] = (pc,r,s) conn.close() return annos except sqlite3.OperationalError as e: print(f"Failed to read data from {db}: {e}") - return [] + return annos @staticmethod - def write_annotations(annos: list): + def write_annotations(annos: dict): """Writes a list of annotations to id3v2 tags. The tags used are TXXX=FNVD_, where is one of PlayCount, Rating, Starred. input: - annos: list of annotations. Format must be (path, playCount, rating, starred). + annos: dict of annotations. Format must be {path: (playCount, rating, starred)}. """ count = 0 - for a in annos: + for k,v in annos.items(): try: - file = id3.ID3(a[0]) - file.add(TXXX(desc="FNVD_PlayCount", text=str(a[1]))) - file.add(TXXX(desc="FNVD_Rating", text=str(a[2]))) - file.add(TXXX(desc="FNVD_Starred", text=str(a[3]))) + file = id3.ID3(k) + file.add(TXXX(desc="FNVD_PlayCount", text=str(v[0]))) + file.add(TXXX(desc="FNVD_Rating", text=str(v[1]))) + file.add(TXXX(desc="FNVD_Starred", text=str(v[2]))) file.save() count += 1 except MutagenError as e: