From cfe2ae0c9d4c0f9a15b72e3a26217be42a219eb4 Mon Sep 17 00:00:00 2001 From: Lucas Media Date: Thu, 5 Nov 2026 10:33:06 +0100 Subject: [PATCH] feat(sonarr_database): creata arg match_threshold continue loop in recheck episodes if names are the same --- media_editing/sonarr_database_tool.py | 28 +++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/media_editing/sonarr_database_tool.py b/media_editing/sonarr_database_tool.py index fb2a9af..09c542b 100755 --- a/media_editing/sonarr_database_tool.py +++ b/media_editing/sonarr_database_tool.py @@ -11,7 +11,6 @@ import requests weight_jaccard = 0.5 weight_fuzzy = 1 - weight_jaccard -MATCH_THRESHOLD = 0.85 from serverlibrary import ( VIDEO_SUFFIXES, @@ -432,7 +431,7 @@ def print_results( def best_episode_title_match(filename, episodes): search_words = words(normalize(filename)) - logger.debug(f"Search Words: {search_words}") + logger.trace(f"Search Words: {search_words}") best_score = 0.0 best_episode = None @@ -558,13 +557,15 @@ def translate_series_folder(episodes, series_path, media_data: dict, args): def reference_extra_episodes(episodes, series_path: Path, media_data: dict, args): + media_name = media_data["name"].split(" - ", 1)[0] + logger.info(f"Execute reference_extra_episodes for {media_data['name']}") extra_episodes_path = series_path / "Extra Episodes" for file in extra_episodes_path.glob("*"): if not file.is_file() or file.suffix.lower() not in VIDEO_SUFFIXES: continue logger.trace(f"Reference file: {file.name}") - filename = re.sub(media_data["name"], "", file.stem) + filename = re.sub(media_name, "", file.stem, flags=re.IGNORECASE) episode, score = best_episode_title_match( filename, episodes, @@ -572,7 +573,7 @@ def reference_extra_episodes(episodes, series_path: Path, media_data: dict, args if episode is None: continue - if score < MATCH_THRESHOLD: + if score < args.match_threshold: logger.warning( f"LOW CONFIDENCE: {filename} {score:.2f}: {normalize(episode.get('name'))}" ) @@ -580,7 +581,7 @@ def reference_extra_episodes(episodes, series_path: Path, media_data: dict, args safe_title = re.sub( r'[<>:"/\\|?*]', - "", + "_", episode["name"], ) @@ -593,13 +594,14 @@ def reference_extra_episodes(episodes, series_path: Path, media_data: dict, args ) target = file.parent.parent / f"Season {episode['season']:02d}" / new_name + if score < 1: logger.debug(f"{file.name} -> {new_name}, score={score:.3f}") elif score == 1: logger.trace(f"{file.name} -> {new_name}, score={score:.3f}") if args.dry_run: - logger.debug(f"{file.name} -> {target}") + logger.debug(f"{file.name} -> {target.relative_to(series_path)}") continue # logger.info(f"{file.name} -> {target}, score={score:.3f}") safe_rename(file, target, logger=logger) @@ -624,13 +626,13 @@ def recheck_current_episodes(episodes, series_path: Path, media_data: dict, args title, episodes, ) - quality = match["quality"] + quality = match["quality"] if match["quality"] is not None else "" season_episode = match["se"] if episode is None: continue - if score < MATCH_THRESHOLD: + if score < args.match_threshold: logger.warning( f"LOW CONFIDENCE: {title} {score:.2f}: {normalize(episode.get('name'))}" ) @@ -656,6 +658,9 @@ def recheck_current_episodes(episodes, series_path: Path, media_data: dict, args ) target = file.parent.parent / f"Season {episode['season']:02d}" / new_name + if target == file: + continue + if score < 1: logger.debug(f"{file.name} -> {new_name}, score={score:.3f}") elif score == 1: @@ -782,6 +787,13 @@ def main(): choices=["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], help="Set log level", ) + parser.add_argument( + "-mt", + "--match-threshold", + type=float, + default=0.85, + help="Set the threshold for a match to be considered positive", + ) parser.add_argument( "-db", "--database",