From 9be971f1b33a3f43cbdbc5765be5e6ffc7895f95 Mon Sep 17 00:00:00 2001 From: billypom on debian Date: Sun, 18 Aug 2024 16:07:21 -0400 Subject: [PATCH] TIT2 id3 tag import fixes --- utils/add_files_to_library.py | 12 ++++++------ utils/get_id3_tags.py | 9 ++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/utils/add_files_to_library.py b/utils/add_files_to_library.py index 0106f60..9459ea6 100644 --- a/utils/add_files_to_library.py +++ b/utils/add_files_to_library.py @@ -1,6 +1,7 @@ import DBA from configparser import ConfigParser from utils import get_id3_tags, id3_timestamp_to_datetime +import logging config = ConfigParser() config.read("config.ini") @@ -11,6 +12,7 @@ def add_files_to_library(files): files = list() of fully qualified paths to audio file(s) Returns a list of dictionaries of metadata """ + print("Running add_files_to_library.py") if not files: return [] extensions = config.get("settings", "extensions").split(",") @@ -19,13 +21,11 @@ def add_files_to_library(files): if any(filepath.lower().endswith(ext) for ext in extensions): filename = filepath.split("/")[-1] audio = get_id3_tags(filepath) - # print("add_files_to_library audio:") - # print(audio) - # Skip if no title is found (but should never happen - if "TIT2" not in audio: - continue - title = audio["TIT2"].text[0] + try: + title = audio["TIT2"].text[0] + except KeyError as e: + title = filename try: artist = audio["TPE1"].text[0] except KeyError: diff --git a/utils/get_id3_tags.py b/utils/get_id3_tags.py index cc6e335..e16487f 100644 --- a/utils/get_id3_tags.py +++ b/utils/get_id3_tags.py @@ -44,14 +44,13 @@ def get_id3_tags(file): # If 'TIT2' tag is not set, add it with a default value (title will be the filename without extension) title = os.path.splitext(os.path.basename(file))[0] - for key in list(audio.keys()): - if key == "TIT2": - audio[key].text[0] = title - break + list_of_id3_tags = list(audio.keys()) + if "TIT2" in list_of_id3_tags: + audio.save() + return audio else: tit2_tag = TIT2(encoding=3, text=[title]) audio["TIT2"] = tit2_tag - # Save the updated tags audio.save()