feat(audiobook_library): improve logging, add actions variable to choose what metadata to fix

This commit is contained in:
lucas committed 2026-11-04 07:53:28 +01:00
1 parent add30d5f90
commit e591602d18
1 file changed
+45 -15
@@ -89,9 +89,10 @@ metadata_fields = [
]
SEP = r"\s*[-_:,.]+\s*"
series_patterns = [
rf"^(.+?){SEP}(?:Book|Band|Volume|Vol)\s+(\d+)(?:{SEP}(.+))$",
rf"^(.+?){SEP}(?:Book|Band|Volume|Vol)\s+(\d+)$",
rf"^(\d+){SEP}(.+)$",
rf"^(.+?){SEP}(\d+)$",
rf"^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+)(?:{SEP}(.+))$",
]
@@ -1132,10 +1133,12 @@ class AudiobookLibrary:
def init(self):
if not self.connect():
return False
logger.info("Database initialized successfully")
return self.load_database_data()
def load_database_data(self) -> bool:
logger.info("Load Database data")
if not self.db:
raise ValueError
self.tables = self.db.execute(
@@ -1189,6 +1192,7 @@ class AudiobookLibrary:
# commit once after all updates
def fix_genres(self):
logger.info("Fix genres in database")
for book in self.books:
genres = book.get("genres")
if not genres or not any(g.strip() for g in genres):
@@ -1211,13 +1215,13 @@ class AudiobookLibrary:
logger.trace(f"Too long or too short: {genre}")
genre = genre.strip()
genre = genre.lstrip()
new_genres.append(genre)
if genre:
new_genres.append(genre)
elif isinstance(genre, list):
if len(genres) > 1:
genres = [g.strip() for g in genre if g]
logger.trace(f"REPR: {repr(genres)}")
logger.trace(f"GENRES: {genres}")
new_genres.extend(genre)
genre_buf = genre
if len(genre) > 1:
genre_buf = [g.strip() for g in genre if g]
new_genres.extend(genre_buf)
new_genres = sorted(set(new_genres))
logger.trace(f"{new_genres}")
@@ -1237,6 +1241,7 @@ class AudiobookLibrary:
book["genres"] = new_genres
def fix_comments(self):
logger.info("Fix comments in database")
for book in self.books:
for file in book.get("audioFiles"):
comment = file.get("metaTags").get("tagComment")
@@ -1606,6 +1611,23 @@ def main():
help="Create structured directories for all books: /artist/series/book(read by X)/file.m4b",
)
actions = [
"artis_dir",
"artists",
"composers",
"series",
"names",
"genres",
"comments",
"titles",
]
parser.add_argument(
"--actions",
nargs="+",
choices=actions,
default=actions,
)
args = parser.parse_args()
if "--sql-database" in sys.argv:
args.fix_db = True
@@ -1626,14 +1648,22 @@ def main():
library.api_instance.load_books()
library.api_instance.embed_metadata_from_file("~/embed_ids.txt")
#library.api_instance.embed_metadata_for_all_books(args)
library.fix_artist_dir()
library.fix_artists(args.force_artist_from_path)
library.fix_composers()
library.fix_series()
library.fix_names()
library.fix_comments()
library.fix_genres()
library.fix_titles()
if "artist_dir" in args.actions:
library.fix_artist_dir()
if "artists" in args.actions:
library.fix_artists(args.force_artist_from_path)
if "composers" in args.actions:
library.fix_composers()
if "series" in args.actions:
library.fix_series()
if "names" in args.actions:
library.fix_names()
if "comments" in args.actions:
library.fix_comments()
if "genres" in args.actions:
library.fix_genres()
if "titles" in args.actions:
library.fix_titles()
if args.fix_db:
if not database_path.is_file():