fix(audiobook_library): move path_filter to general library, remove "remove series from name"

This commit is contained in:
Lucas Vivo authored and Lucas Media committed 2026-10-30 10:27:41 +01:00
1 parent 3b70209bc2
commit 38ad019edd
1 file changed
+21 -26
@@ -22,7 +22,7 @@ SEP = r"\s*[-_:,.]+\s*"
series_patterns = [
rf'^(\d+){SEP}(.+)$',
rf'^(.+?){SEP}(\d+)$',
rf'^(.+?)(?:{SEP}?(?:Book|Band|Volume)\s+(\d+))?$'
rf'^(.+?){SEP}(?:Book|Band|Volume)\s+(\d+)(?:{SEP}(.+))$'
]
def ffmpeg_escape(path: Path) -> str:
return str(path.resolve()).replace("'", r"'\''")
@@ -71,7 +71,7 @@ def clean_genre(genre):
genre = genre.strip()
genre = genre.lstrip()
if genre != genre_old:
logger.debug(f"Update genre from {genre_old} --->>> {genre}")
logger.debug(f"Update genre from {genre_old} to {genre}")
return genre
def ffmetadata_escape(value: str) -> str:
@@ -198,7 +198,10 @@ def write_metadata(file, fields, values,dry_run=False):
def normalize_name(name: str,series: str) -> str:
# Remove anything in (), [], {}
name_out = ""
name = re.sub(series,"",name)
#name = re.sub(
# rf"^(?:{re.escape(series)}){SEP}|{SEP}(?:{re.escape(series)})$",
# "", name, flags=re.IGNORECASE, ).strip()
name = re.sub(r"\s*[\(\[\{].*?[\)\]\}]\s*", " ", name).strip()
name = re.sub(r"\s+", " ", name)
@@ -493,7 +496,7 @@ class Audiobook:
self.changed = True
self.artist = rel_path[0]
logger.info(f"[Updated] artist found in path for {rel_path}")
logger.info(f"[Updated] artist {self.artist} found in path for {self.bookPath}")
def fix_composer_from_comment(self):
m = re.search(
@@ -538,9 +541,9 @@ class Audiobook:
def fix_name(self):
if not self.name:
self.name = normalize_name(self.bookPath.name)
self.name = normalize_name(self.bookPath.name,self.series)
self.changed = True
logger.info(f"[Updated] 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:
@@ -825,9 +828,9 @@ class Audiobook:
class AudiobookLibrary:
def __init__(
self, library_path: Path, sql_database_path: Path, dry_run: bool, 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)
self.dry_run = dry_run
logging.setLoggerClass(CustomLogger)
@@ -1036,7 +1039,8 @@ class AudiobookLibrary:
media_files = [
file
for file in self.libraryPath.rglob("*")
if file.is_file() and file.suffix.lower() in AUDIO_SUFFIXES
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:
@@ -1058,10 +1062,10 @@ class AudiobookLibrary:
for book in self.audiobooks:
book.fix_artist_dir()
def create_structured_directories(self):
def create_structured_directories(self, dry_run=False):
for book in self.audiobooks:
desired_path, desired_name = book.generate_dir_from_metadata()
book.move(desired_path, desired_name)
book.move(desired_path, desired_name, dry_run)
def fix_artists(self):
for book in self.audiobooks:
@@ -1093,18 +1097,10 @@ class AudiobookLibrary:
book.fix_series()
def create_m4b_from_dir(
self, path: Path = Path(""), name: str = "", keep_originals: bool = False
self, keep_originals: bool = False
) -> None:
audiobooks = []
if path:
audiobooks = self.find_path_contains(path)
if name:
audiobooks.extend(self.find_name_contains(name))
if not name and not path:
audiobooks = self.audiobooks
audiobooks = self.audiobooks
jobs = [audiobook for audiobook in audiobooks if not audiobook.singleFile]
for audiobook in jobs:
logger.debug(f"Create m4b file for {audiobook.name}")
@@ -1182,7 +1178,7 @@ def main():
help="SQL Database Path",
)
parser.add_argument(
"-fd"
"-fd",
"--fix-db",
action="store_true",
help="Fix the Database",
@@ -1211,7 +1207,8 @@ def main():
logger.error(f"Database is not a file: {database_path}")
return
library = AudiobookLibrary(library_dir, database_path, args.dry_run)
library = AudiobookLibrary(library_dir, database_path, args.dry_run,
path_filter=args.path_filter)
if args.fix_db:
library.sql_database_instance.init()
@@ -1225,8 +1222,6 @@ def main():
if args.create_m4b:
library.create_m4b_from_dir(
name=args.name_filter,
path=args.path_filter,
keep_originals=args.keep_originals,
)
@@ -1241,7 +1236,7 @@ def main():
library.sql_database_instance.close()
if args.move:
library.create_structured_directories()
library.create_structured_directories(args.dry_run)
print("Fin")