From d6488a99bd7813501832bb1fd093e44ab7a7b311 Mon Sep 17 00:00:00 2001 From: SBCrumb Date: Sun, 26 Oct 2025 15:42:05 -0400 Subject: [PATCH] nfo: improvments to processing --- api/routes.py | 91 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 28 deletions(-) diff --git a/api/routes.py b/api/routes.py index ac43d65..e9e6209 100644 --- a/api/routes.py +++ b/api/routes.py @@ -2317,9 +2317,9 @@ def register_routes(app, dependencies: dict): import subprocess import re - # Find all NFO files and check which ones are missing dateadded - # This is much faster than database-driven approach - missing_nfo_files = [] + # Separate tracking for TV and movie missing files + missing_tv_nfo_files = [] + missing_movie_nfo_files = [] # Find all NFO files in TV directory if os.path.exists(tv_path): @@ -2342,7 +2342,7 @@ def register_routes(app, dependencies: dict): if i % 1000 == 0 and i > 0: elapsed = time.time() - scan_start_time - print(f"📊 Progress: {i}/{len(tv_nfo_files)} TV NFO files checked in {elapsed:.1f}s, {len(missing_nfo_files)} missing found") + print(f"📊 Progress: {i}/{len(tv_nfo_files)} TV NFO files checked in {elapsed:.1f}s, {len(missing_tv_nfo_files)} missing found") # Timeout check if time.time() - scan_start_time > 180: # 3 minutes @@ -2358,7 +2358,7 @@ def register_routes(app, dependencies: dict): # If grep returns non-zero, dateadded is missing if grep_result.returncode != 0: - missing_nfo_files.append(nfo_file) + missing_tv_nfo_files.append(nfo_file) # Log specific files we're looking for if 'Hudson' in nfo_file and 'S08E05' in nfo_file: @@ -2383,9 +2383,9 @@ def register_routes(app, dependencies: dict): else: print(f"❌ TV path does not exist: {tv_path}") - print(f"📊 Found {len(missing_nfo_files)} NFO files missing dateadded elements") + print(f"📊 Found {len(missing_tv_nfo_files)} TV NFO files missing dateadded elements") - # Convert file paths to episode/movie information with direct NFO parsing + # Convert TV file paths to episode information with direct NFO parsing def extract_imdb_from_nfo_content(nfo_file, is_episode=True): """Extract IMDb ID directly from NFO file content - we already know the file exists""" imdb_id = "unknown" @@ -2459,7 +2459,7 @@ def register_routes(app, dependencies: dict): print(f"❌ No IMDb ID found in {os.path.basename(nfo_file)}") return "unknown" - for nfo_file in missing_nfo_files: + for nfo_file in missing_tv_nfo_files: try: # Parse episode info from file path # Example: /media/TV/tv/Hudson & Rex (2019) [imdb-tt9111220]/Season 08/Hudson & Rex (2019)-S08E05-Episode.nfo @@ -2522,7 +2522,7 @@ def register_routes(app, dependencies: dict): if i % 500 == 0 and i > 0: elapsed = time.time() - scan_start_time - print(f"📊 Progress: {i}/{len(movie_nfo_files)} Movie NFO files checked in {elapsed:.1f}s, {len(missing_items['movies'])} missing found") + print(f"📊 Progress: {i}/{len(movie_nfo_files)} Movie NFO files checked in {elapsed:.1f}s, {len(missing_movie_nfo_files)} missing found") # Timeout check if time.time() - scan_start_time > 300: # 5 minutes total @@ -2538,25 +2538,7 @@ def register_routes(app, dependencies: dict): # If grep returns non-zero, dateadded is missing if grep_result.returncode != 0: - # Extract movie info from file path - # Example: /media/Movies/movies/Knives Out (2019) [imdb-tt8946378]/movie.nfo - movie_dir = os.path.dirname(nfo_file) - movie_folder_name = os.path.basename(movie_dir) - - # Extract IMDb ID from NFO content - imdb_id = extract_imdb_from_nfo_content(nfo_file, is_episode=False) - - # Clean movie title (remove year and imdb parts) - clean_movie_title = re.sub(r'\s*\(\d{4}\)\s*\[imdb-[^]]+\]', '', movie_folder_name).strip() - - missing_items["movies"].append({ - "imdb_id": imdb_id, - "title": clean_movie_title, - "path": movie_dir, - "dateadded": None, - "nfo_path": nfo_file, - "reason": "NFO missing dateadded element (filesystem scan)" - }) + missing_movie_nfo_files.append(nfo_file) except subprocess.TimeoutExpired: print(f"⏰ Timeout checking {nfo_file}") @@ -2575,6 +2557,34 @@ def register_routes(app, dependencies: dict): else: print(f"❌ Movie path does not exist: {movie_path}") + # Process missing movie NFO files + print(f"📊 Found {len(missing_movie_nfo_files)} Movie NFO files missing dateadded elements") + + for nfo_file in missing_movie_nfo_files: + try: + # Extract movie info from file path + # Example: /media/Movies/movies/Knives Out (2019) [imdb-tt8946378]/movie.nfo + movie_dir = os.path.dirname(nfo_file) + movie_folder_name = os.path.basename(movie_dir) + + # Extract IMDb ID from NFO content + imdb_id = extract_imdb_from_nfo_content(nfo_file, is_episode=False) + + # Clean movie title (remove year and imdb parts) + clean_movie_title = re.sub(r'\s*\(\d{4}\)\s*\[imdb-[^]]+\]', '', movie_folder_name).strip() + + missing_items["movies"].append({ + "imdb_id": imdb_id, + "title": clean_movie_title, + "path": movie_dir, + "dateadded": None, + "nfo_path": nfo_file, + "reason": "NFO missing dateadded element (filesystem scan)" + }) + except Exception as e: + print(f"⚠️ Error parsing movie NFO file path {nfo_file}: {e}") + continue + # Database lookup to get actual dateadded values for missing items print("🔍 Looking up dateadded values from database for missing items...") @@ -2765,6 +2775,19 @@ def register_routes(app, dependencies: dict): dateadded_elem = ET.SubElement(root, "dateadded") dateadded_elem.text = nfo_dateadded + # Add source attribution (similar to what NFOGuard normally adds) + # Check if source element exists, if not create it + source_elem = root.find("source") + if source_elem is None: + source_elem = ET.SubElement(root, "source") + source_elem.text = f"NFOGuard repair - {episode.get('source', 'database')}" + + # Add addedby attribution if not present + addedby_elem = root.find("addedby") + if addedby_elem is None: + addedby_elem = ET.SubElement(root, "addedby") + addedby_elem.text = "NFOGuard" + # Write back to file tree.write(nfo_path, encoding='utf-8', xml_declaration=True) @@ -2851,6 +2874,18 @@ def register_routes(app, dependencies: dict): dateadded_elem = ET.SubElement(root, "dateadded") dateadded_elem.text = nfo_dateadded + # Add source attribution (similar to what NFOGuard normally adds) + source_elem = root.find("source") + if source_elem is None: + source_elem = ET.SubElement(root, "source") + source_elem.text = f"NFOGuard repair - {movie.get('source', 'database')}" + + # Add addedby attribution if not present + addedby_elem = root.find("addedby") + if addedby_elem is None: + addedby_elem = ET.SubElement(root, "addedby") + addedby_elem.text = "NFOGuard" + # Write back to file tree.write(nfo_path, encoding='utf-8', xml_declaration=True)