fix(audiobook_library): move path_filter to general library, remove "remove series from name"
This commit is contained in:
1 parent
db350414d4
commit
50a6d786f0
1 file changed
+21
-26
@@ -22,7 +22,7 @@ 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+))?$'
|
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"'\''")
|
||||||
@@ -71,7 +71,7 @@ def clean_genre(genre):
|
|||||||
genre = genre.strip()
|
genre = genre.strip()
|
||||||
genre = genre.lstrip()
|
genre = genre.lstrip()
|
||||||
if genre != genre_old:
|
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
|
return genre
|
||||||
|
|
||||||
def ffmetadata_escape(value: str) -> str:
|
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:
|
def normalize_name(name: str,series: str) -> str:
|
||||||
# Remove anything in (), [], {}
|
# Remove anything in (), [], {}
|
||||||
name_out = ""
|
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*[\(\[\{].*?[\)\]\}]\s*", " ", name).strip()
|
||||||
name = re.sub(r"\s+", " ", name)
|
name = re.sub(r"\s+", " ", name)
|
||||||
|
|
||||||
@@ -493,7 +496,7 @@ class Audiobook:
|
|||||||
self.changed = True
|
self.changed = True
|
||||||
|
|
||||||
self.artist = rel_path[0]
|
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):
|
def fix_composer_from_comment(self):
|
||||||
m = re.search(
|
m = re.search(
|
||||||
@@ -538,9 +541,9 @@ class Audiobook:
|
|||||||
|
|
||||||
def fix_name(self):
|
def fix_name(self):
|
||||||
if not self.name:
|
if not self.name:
|
||||||
self.name = normalize_name(self.bookPath.name)
|
self.name = normalize_name(self.bookPath.name,self.series)
|
||||||
self.changed = True
|
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
|
return
|
||||||
name = normalize_name(self.name, self.series)
|
name = normalize_name(self.name, self.series)
|
||||||
if name != self.name:
|
if name != self.name:
|
||||||
@@ -825,9 +828,9 @@ class Audiobook:
|
|||||||
|
|
||||||
class AudiobookLibrary:
|
class AudiobookLibrary:
|
||||||
def __init__(
|
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.sql_database_instance = self.SqlDatabase(sql_database_path)
|
||||||
self.dry_run = dry_run
|
self.dry_run = dry_run
|
||||||
logging.setLoggerClass(CustomLogger)
|
logging.setLoggerClass(CustomLogger)
|
||||||
@@ -1036,7 +1039,8 @@ 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
|
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}
|
dirs = {file.parent for file in media_files}
|
||||||
for directory in dirs:
|
for directory in dirs:
|
||||||
@@ -1058,10 +1062,10 @@ class AudiobookLibrary:
|
|||||||
for book in self.audiobooks:
|
for book in self.audiobooks:
|
||||||
book.fix_artist_dir()
|
book.fix_artist_dir()
|
||||||
|
|
||||||
def create_structured_directories(self):
|
def create_structured_directories(self, dry_run=False):
|
||||||
for book in self.audiobooks:
|
for book in self.audiobooks:
|
||||||
desired_path, desired_name = book.generate_dir_from_metadata()
|
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):
|
def fix_artists(self):
|
||||||
for book in self.audiobooks:
|
for book in self.audiobooks:
|
||||||
@@ -1093,18 +1097,10 @@ class AudiobookLibrary:
|
|||||||
book.fix_series()
|
book.fix_series()
|
||||||
|
|
||||||
def create_m4b_from_dir(
|
def create_m4b_from_dir(
|
||||||
self, path: Path = Path(""), name: str = "", keep_originals: bool = False
|
self, keep_originals: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
audiobooks = []
|
audiobooks = self.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
|
|
||||||
|
|
||||||
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:
|
||||||
logger.debug(f"Create m4b file for {audiobook.name}")
|
logger.debug(f"Create m4b file for {audiobook.name}")
|
||||||
|
|
||||||
@@ -1182,7 +1178,7 @@ def main():
|
|||||||
help="SQL Database Path",
|
help="SQL Database Path",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-fd"
|
"-fd",
|
||||||
"--fix-db",
|
"--fix-db",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Fix the Database",
|
help="Fix the Database",
|
||||||
@@ -1211,7 +1207,8 @@ 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(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()
|
||||||
|
|
||||||
@@ -1225,8 +1222,6 @@ def main():
|
|||||||
|
|
||||||
if args.create_m4b:
|
if args.create_m4b:
|
||||||
library.create_m4b_from_dir(
|
library.create_m4b_from_dir(
|
||||||
name=args.name_filter,
|
|
||||||
path=args.path_filter,
|
|
||||||
keep_originals=args.keep_originals,
|
keep_originals=args.keep_originals,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1241,7 +1236,7 @@ def main():
|
|||||||
library.sql_database_instance.close()
|
library.sql_database_instance.close()
|
||||||
|
|
||||||
if args.move:
|
if args.move:
|
||||||
library.create_structured_directories()
|
library.create_structured_directories(args.dry_run)
|
||||||
|
|
||||||
print("Fin")
|
print("Fin")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user