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