style(audiobook_library): auto format from nvim

This commit is contained in:
lucas committed 2026-10-31 09:13:39 +01:00
1 parent f926ee03a2
commit 5c0ae62acb
1 file changed
+86 -47
@@ -17,7 +17,6 @@ from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, ID3NoHeaderError from mutagen.id3 import ID3, ID3NoHeaderError
logging.setLoggerClass(CustomLogger) logging.setLoggerClass(CustomLogger)
logger: CustomLogger = logging.getLogger(__name__) # type: ignore[assignment] logger: CustomLogger = logging.getLogger(__name__) # type: ignore[assignment]
@@ -30,17 +29,20 @@ metadata_fields = [
"grouping", "grouping",
"albumartist", "albumartist",
"description", "description",
"title" "title",
] ]
SEP = r"\s*[-_:,.]+\s*" SEP = r"\s*[-_:,.]+\s*"
series_patterns = [ series_patterns = [
rf'^(\d+){SEP}(.+)$', rf"^(\d+){SEP}(.+)$",
rf'^(.+?){SEP}(\d+)$', rf"^(.+?){SEP}(\d+)$",
rf'^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+)(?:{SEP}(.+))$' rf"^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+)(?:{SEP}(.+))$",
] ]
def ffmpeg_escape(path: Path) -> str: def ffmpeg_escape(path: Path) -> str:
return str(path.resolve()).replace("'", r"'\''") return str(path.resolve()).replace("'", r"'\''")
def clean_genre(genre): def clean_genre(genre):
if not genre: if not genre:
return "" return ""
@@ -88,6 +90,7 @@ def clean_genre(genre):
logger.debug(f"Update genre from {genre_old} to {genre}") logger.debug(f"Update genre from {genre_old} to {genre}")
return genre return genre
def ffmetadata_escape(value: str) -> str: def ffmetadata_escape(value: str) -> str:
return ( return (
value.replace("\\", "\\\\") value.replace("\\", "\\\\")
@@ -97,6 +100,7 @@ def ffmetadata_escape(value: str) -> str:
.replace("\n", "\\\n") .replace("\n", "\\\n")
) )
def get_chapters(media: Path) -> list[dict]: def get_chapters(media: Path) -> list[dict]:
result = subprocess.run( result = subprocess.run(
[ [
@@ -116,6 +120,7 @@ def get_chapters(media: Path) -> list[dict]:
data = json.loads(result.stdout) data = json.loads(result.stdout)
return data.get("chapters", []) return data.get("chapters", [])
def get_metadata_all(file: Path) -> dict[str, str]: def get_metadata_all(file: Path) -> dict[str, str]:
metadata = {} metadata = {}
try: try:
@@ -152,6 +157,8 @@ def get_metadata_fields(file: Path, fields) -> dict[str, str]:
print(f"Error getting metadata for {file}: {error}") print(f"Error getting metadata for {file}: {error}")
return metadata return metadata
def get_duration_ms(media: Path) -> int: def get_duration_ms(media: Path) -> int:
duration = subprocess.check_output( duration = subprocess.check_output(
[ [
@@ -169,6 +176,7 @@ def get_duration_ms(media: Path) -> int:
return int(float(duration) * 1000) return int(float(duration) * 1000)
def write_metadata(file, fields, values, dry_run=False): def write_metadata(file, fields, values, dry_run=False):
logger.debug(f"Writing metadata for {file}") logger.debug(f"Writing metadata for {file}")
changes = { changes = {
@@ -196,6 +204,7 @@ def write_metadata(file, fields, values, dry_run=False):
except Exception as e: except Exception as e:
logger.exception(f"Error writing metadata for {file}: {e}") logger.exception(f"Error writing metadata for {file}: {e}")
def normalize_name(name: str, series: str) -> str: def normalize_name(name: str, series: str) -> str:
# Remove anything in (), [], {} # Remove anything in (), [], {}
name_out = "" name_out = ""
@@ -217,44 +226,53 @@ def normalize_name(name: str,series: str) -> str:
else: else:
name_out = name name_out = name
pattern = rf'^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+){SEP}(.+)$' pattern = rf"^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+){SEP}(.+)$"
match = re.match(pattern, name, re.IGNORECASE) match = re.match(pattern, name, re.IGNORECASE)
if match: if match:
name_out1 = match.group(1).strip() name_out1 = match.group(1).strip()
name_out3 = match.group(3).strip() name_out3 = match.group(3).strip()
if score_match_jaccard(words(name_out1), words(series)) > score_match_jaccard(words(name_out3), words(series)): if score_match_jaccard(words(name_out1), words(series)) > score_match_jaccard(
words(name_out3), words(series)
):
return name_out3 return name_out3
else: else:
return name_out1 return name_out1
return name_out return name_out
def cleanup(text: str) -> str: def cleanup(text: str) -> str:
# Collapse repeated punctuation groups like ", ,", ". .", "! !", "? ?" # Collapse repeated punctuation groups like ", ,", ". .", "! !", "? ?"
while True: while True:
new_text = re.sub(r'([.,!?;:-])(?:\s+\1)+', r'\1', text) new_text = re.sub(r"([.,!?;:-])(?:\s+\1)+", r"\1", text)
if new_text == text: if new_text == text:
break break
text = new_text text = new_text
# Optional: normalize multiple spaces # Optional: normalize multiple spaces
text = re.sub(r'\s{2,}', ' ', text) text = re.sub(r"\s{2,}", " ", text)
return text.strip() return text.strip()
def get_empty_keys(data: dict) -> str: def get_empty_keys(data: dict) -> str:
empty_keys = [str(key) for key, value in data.items() if not value] empty_keys = [str(key) for key, value in data.items() if not value]
return f"Empty: {', '.join(empty_keys)}" if empty_keys else "" return f"Empty: {', '.join(empty_keys)}" if empty_keys else ""
def get_codec_bitrate(file: Path) -> tuple[str, str]: def get_codec_bitrate(file: Path) -> tuple[str, str]:
info = json.loads( info = json.loads(
subprocess.check_output( subprocess.check_output(
[ [
"ffprobe", "ffprobe",
"-v", "error", "-v",
"-select_streams", "a:0", "error",
"-show_entries", "stream=codec_name,bit_rate", "-select_streams",
"-of", "json", "a:0",
"-show_entries",
"stream=codec_name,bit_rate",
"-of",
"json",
str(file), str(file),
], ],
text=True, text=True,
@@ -311,7 +329,8 @@ class Audiobook:
self.files_extra = [ self.files_extra = [
file file
for file in self.bookPath.glob("*") if file.suffix.lower() not in AUDIO_SUFFIXES for file in self.bookPath.glob("*")
if file.suffix.lower() not in AUDIO_SUFFIXES
] ]
self.firstFile = self.get_first_file() self.firstFile = self.get_first_file()
self.size = sum( self.size = sum(
@@ -323,7 +342,9 @@ class Audiobook:
self.singleFile = self.fileConunt == 1 self.singleFile = self.fileConunt == 1
# Metadata # Metadata
self.load_metadata() self.load_metadata()
logger.info(f"Found {self.bookPath.name}, {self.fileConunt} files, {get_empty_keys(self.metadata)}") logger.info(
f"Found {self.bookPath.name}, {self.fileConunt} files, {get_empty_keys(self.metadata)}"
)
self.changed = False self.changed = False
def __repr__(self): def __repr__(self):
@@ -428,9 +449,7 @@ class Audiobook:
self.metadata = get_metadata_all(self.get_first_file()) self.metadata = get_metadata_all(self.get_first_file())
self.metadata_original = self.metadata.copy() self.metadata_original = self.metadata.copy()
self.write_fields_from_metadata() self.write_fields_from_metadata()
logger.trace( logger.trace(f"Load metadata {self.metadata}")
f"Load metadata {self.metadata}"
)
def fix_artist_dir(self): def fix_artist_dir(self):
rel_path = self.bookPath.relative_to(self.library_root_path).parts rel_path = self.bookPath.relative_to(self.library_root_path).parts
@@ -486,7 +505,7 @@ class Audiobook:
elif self.artist != artist: elif self.artist != artist:
logger.warning( logger.warning(
f"{red_tag("Action Required")} artist {artist} found not matching {self.artist} for {self.bookPath}" f"{red_tag('Action Required')} artist {artist} found not matching {self.artist} for {self.bookPath}"
) )
self.action_required = True self.action_required = True
else: else:
@@ -503,12 +522,15 @@ class Audiobook:
if not self.artist or force_artist_from_path: if not self.artist or force_artist_from_path:
self.changed = True self.changed = True
self.artist = rel_path[0] self.artist = rel_path[0]
logger.info(f"[Updated] artist {self.artist} found in path for {self.bookPath}") logger.info(
f"[Updated] artist {self.artist} found in path for {self.bookPath}"
)
def fix_composer_from_comment(self): def fix_composer_from_comment(self):
m = re.search( m = re.search(
r"(?:read\s+by:?|" r"(?:read\s+by:?|"
r"spoken\s+by:?|" r"spoken\s+by:?|"
r"erzähler:?|"
r"narrated\s+by:?|" r"narrated\s+by:?|"
r"gesprochen\s+von:?|" r"gesprochen\s+von:?|"
r"gelesen\s+von)\s+([^\n.,;]+)", r"gelesen\s+von)\s+([^\n.,;]+)",
@@ -550,7 +572,9 @@ class Audiobook:
if not self.name: if not self.name:
self.name = normalize_name(self.bookPath.name, self.series) self.name = normalize_name(self.bookPath.name, self.series)
self.changed = True self.changed = True
logger.info(f"[Updated] name {self.name} found in path for {self.bookPath}")# logger.info(
f"[Updated] name {self.name} found in path for {self.bookPath}"
) #
return return
name = normalize_name(self.name, self.series) name = normalize_name(self.name, self.series)
if name != self.name: if name != self.name:
@@ -589,7 +613,9 @@ class Audiobook:
self.series_and_num = rel_path[1] + number_str self.series_and_num = rel_path[1] + number_str
self.series = rel_path[1] self.series = rel_path[1]
self.changed = True self.changed = True
logger.info(f"[Updated] series{and_number} found for {self.name} to {self.series_and_num}") logger.info(
f"[Updated] series{and_number} found for {self.name} to {self.series_and_num}"
)
return return
def fix_genre(self): def fix_genre(self):
@@ -628,13 +654,13 @@ class Audiobook:
self.pdf_file = new_file self.pdf_file = new_file
if self.bookPath != desired_path: if self.bookPath != desired_path:
logger.debug(f"Moving {self.bookPath} to {desired_path}") logger.debug(f"Moving {self.bookPath} to {desired_path}")
remaining = list(self.bookPath.iterdir()) remaining = list(self.bookPath.iterdir())
if not remaining: if not remaining:
self.bookPath.rmdir() self.bookPath.rmdir()
else: else:
logger.warning(f"{red_tag('Action Required')} old path not empty after move: " logger.warning(
f"{red_tag('Action Required')} old path not empty after move: "
f"{self.bookPath}: {remaining}" f"{self.bookPath}: {remaining}"
) )
self.bookPath = desired_path self.bookPath = desired_path
@@ -655,8 +681,15 @@ class Audiobook:
series = self.series_and_num.split("#") series = self.series_and_num.split("#")
number = f"{series[1]} - " number = f"{series[1]} - "
series = series[0].rstrip() series = series[0].rstrip()
desired_path = self.library_root_path / self.artist.replace("/", "_") / series.replace("/", "_") / f"{number}{name}".replace("/", "_") desired_path = (
desired_name_stem = self.artist + " - " + self.name + composer + self.bookPath.suffix self.library_root_path
/ self.artist.replace("/", "_")
/ series.replace("/", "_")
/ f"{number}{name}".replace("/", "_")
)
desired_name_stem = (
self.artist + " - " + self.name + composer + self.bookPath.suffix
)
desired_name_stem = desired_name_stem.replace("/", "_") desired_name_stem = desired_name_stem.replace("/", "_")
return desired_path, desired_name_stem return desired_path, desired_name_stem
@@ -666,11 +699,11 @@ class Audiobook:
logger.warning(f"Action required, resolve before creating m4b file") logger.warning(f"Action required, resolve before creating m4b file")
return return
if output_file_final.exists(): if output_file_final.exists():
logger.info(f"{red_tag("SKIP")} {output_file_final}") logger.info(f"{red_tag('SKIP')} {output_file_final}")
logger.info("Book already present") logger.info("Book already present")
return return
logger.info(f"{blue_tag("create")} {output_file_final}") logger.info(f"{blue_tag('create')} {output_file_final}")
with tempfile.TemporaryDirectory(dir=self.library_root_path.parent) as tmp: with tempfile.TemporaryDirectory(dir=self.library_root_path.parent) as tmp:
tmpdir = Path(tmp) tmpdir = Path(tmp)
@@ -717,9 +750,13 @@ class Audiobook:
for key, value in self.metadata.items(): for key, value in self.metadata.items():
if key == "title": if key == "title":
mf.write(f"{ffmetadata_escape(key)}={ffmetadata_escape(self.metadata["album"])}\n") mf.write(
f"{ffmetadata_escape(key)}={ffmetadata_escape(self.metadata['album'])}\n"
)
else: else:
mf.write(f"{ffmetadata_escape(key)}={ffmetadata_escape(value)}\n") mf.write(
f"{ffmetadata_escape(key)}={ffmetadata_escape(value)}\n"
)
for info in file_info: for info in file_info:
media = info["media"] media = info["media"]
media_name = info["media"].name media_name = info["media"].name
@@ -818,15 +855,13 @@ class Audiobook:
self.changed = False self.changed = False
self.size = Path(output_file_final).stat().st_size self.size = Path(output_file_final).stat().st_size
logger.info( logger.info(
f"{green_tag("SUCCESS")} - {self.bookPath.name} - delete media files" f"{green_tag('SUCCESS')} - {self.bookPath.name} - delete media files"
) )
else: else:
logger.error( logger.error(
f"{red_tag("ERROR")} - {self.bookPath.name} - Code: {result.returncode}, Stderr: {result.stderr}" f"{red_tag('ERROR')} - {self.bookPath.name} - Code: {result.returncode}, Stderr: {result.stderr}"
) )
def write_metadata(self, keys=None, dry_run=False): def write_metadata(self, keys=None, dry_run=False):
if self.action_required: if self.action_required:
logger.warning(f"Action required, resolve before writing metadata file") logger.warning(f"Action required, resolve before writing metadata file")
@@ -840,7 +875,9 @@ class Audiobook:
keys.append(key) keys.append(key)
self.metadata_original = self.metadata.copy() self.metadata_original = self.metadata.copy()
values = [self.metadata[key] for key in keys] values = [self.metadata[key] for key in keys]
if (not keys or not values) and self.metadata_original.get("title") != self.title: if (not keys or not values) and self.metadata_original.get(
"title"
) != self.title:
logger.trace(f"Nothing to write to file for {self.name}") logger.trace(f"Nothing to write to file for {self.name}")
return return
if keys: if keys:
@@ -865,7 +902,12 @@ class Audiobook:
class AudiobookLibrary: class AudiobookLibrary:
def __init__( def __init__(
self, library_path: Path, sql_database_path: Path, dry_run: bool, path_filter:str="", logfilename: str = "audiobook_library.log" self,
library_path: Path,
sql_database_path: Path,
dry_run: bool,
path_filter: str = "",
logfilename: str = "audiobook_library.log",
): ):
self.path_filter = path_filter self.path_filter = path_filter
self.sql_database_instance = self.SqlDatabase(sql_database_path) self.sql_database_instance = self.SqlDatabase(sql_database_path)
@@ -1013,7 +1055,7 @@ class AudiobookLibrary:
for file in book.get("audioFiles"): for file in book.get("audioFiles"):
comment = file.get("metaTags").get("tagComment") comment = file.get("metaTags").get("tagComment")
if comment and len(comment) < 40: if comment and len(comment) < 40:
logger.debug(f"Fix short comment for {book.get("title")}") logger.debug(f"Fix short comment for {book.get('title')}")
comment = "" comment = ""
file["metaTags"]["tagComment"] = comment file["metaTags"]["tagComment"] = comment
@@ -1076,8 +1118,9 @@ class AudiobookLibrary:
media_files = [ media_files = [
file file
for file in self.libraryPath.rglob("*") for file in self.libraryPath.rglob("*")
if file.is_file() and file.suffix.lower() in AUDIO_SUFFIXES and \ if file.is_file()
self.path_filter in str(file) and file.suffix.lower() in AUDIO_SUFFIXES
and self.path_filter in str(file)
] ]
dirs = {file.parent for file in media_files} dirs = {file.parent for file in media_files}
for directory in dirs: for directory in dirs:
@@ -1137,9 +1180,7 @@ class AudiobookLibrary:
for book in self.audiobooks: for book in self.audiobooks:
book.fix_series() book.fix_series()
def create_m4b_from_dir( def create_m4b_from_dir(self, keep_originals: bool = False) -> None:
self, keep_originals: bool = False
) -> None:
audiobooks = self.audiobooks audiobooks = self.audiobooks
jobs = [audiobook for audiobook in audiobooks if not audiobook.singleFile] jobs = [audiobook for audiobook in audiobooks if not audiobook.singleFile]
for audiobook in jobs: for audiobook in jobs:
@@ -1163,14 +1204,11 @@ class AudiobookLibrary:
logger.exception(f"Failed to create m4b for {audiobook.name}") logger.exception(f"Failed to create m4b for {audiobook.name}")
def main(): def main():
parser = argparse.ArgumentParser(description="Audiobook Directory Processing") parser = argparse.ArgumentParser(description="Audiobook Directory Processing")
parser.add_argument("library", type=Path, help="Library path") parser.add_argument("library", type=Path, help="Library path")
parser.add_argument( parser.add_argument(
"-ll", "-ll",
"--log-level", "--log-level",
@@ -1254,8 +1292,9 @@ def main():
logger.error(f"Database is not a file: {database_path}") logger.error(f"Database is not a file: {database_path}")
return return
library = AudiobookLibrary(library_dir, database_path, args.dry_run, library = AudiobookLibrary(
path_filter=args.path_filter) library_dir, database_path, args.dry_run, path_filter=args.path_filter
)
if args.fix_db: if args.fix_db:
library.sql_database_instance.init() library.sql_database_instance.init()