From 7f0ad270091d9417850d1205752ced6a1bf640a0 Mon Sep 17 00:00:00 2001 From: SBCrumb Date: Tue, 28 Oct 2025 20:07:08 -0400 Subject: [PATCH] update autfix --- api/routes.py | 98 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 36 deletions(-) diff --git a/api/routes.py b/api/routes.py index fe9fe77..2330585 100644 --- a/api/routes.py +++ b/api/routes.py @@ -2264,29 +2264,43 @@ async def lookup_episode(imdb_id: str, season: int, episode: int, dependencies: # AUTO-FIX: Update NFO file with missing dateadded element try: nfo_manager = dependencies.get("nfo_manager") - if nfo_manager and result.get('video_path'): - video_path = result['video_path'] - print(f"DEBUG: Auto-fixing NFO for episode {imdb_id} S{season:02d}E{episode:02d} at {video_path}") + config = dependencies.get("config") + if nfo_manager and config: + print(f"DEBUG: Auto-fixing NFO for episode {imdb_id} S{season:02d}E{episode:02d}") - # Create NFO with dateadded element + # Find the series directory using the same logic NFOGuard uses from pathlib import Path - video_file = Path(video_path) - if video_file.exists(): - # Use NFO manager to update the episode NFO file - nfo_manager.create_episode_nfo( - video_file.parent, - season, - episode, - imdb_id, - dateadded, - result.get('air_date'), - source=f"NFOGuard auto-fix via Emby lookup - {result.get('source', 'database')}" - ) - print(f"SUCCESS: Auto-fixed NFO file for {imdb_id} S{season:02d}E{episode:02d}") + tv_library_path = Path(config.tv_library_path) + + # Look for series directory with IMDb ID pattern + import glob + pattern = f"*[imdb-{imdb_id}]*" + series_matches = list(tv_library_path.glob(f"**/{pattern}")) + + if series_matches: + series_dir = series_matches[0] # Take first match + season_dir = series_dir / config.get_season_dir_name(season) + + if season_dir.exists(): + print(f"DEBUG: Found season directory: {season_dir}") + + # Use NFO manager to update the episode NFO file + nfo_manager.create_episode_nfo( + season_dir, + season, + episode, + imdb_id, + dateadded, + result.get('aired'), + source=f"NFOGuard auto-fix via Emby lookup - {result.get('source', 'database')}" + ) + print(f"SUCCESS: Auto-fixed NFO file for {imdb_id} S{season:02d}E{episode:02d}") + else: + print(f"WARNING: Season directory not found: {season_dir}") else: - print(f"WARNING: Video file not found for auto-fix: {video_path}") + print(f"WARNING: Series directory not found for {imdb_id} in {tv_library_path}") else: - print(f"DEBUG: Auto-fix skipped - nfo_manager: {nfo_manager is not None}, video_path: {result.get('video_path', 'None')}") + print(f"DEBUG: Auto-fix skipped - nfo_manager: {nfo_manager is not None}, config: {config is not None}") except Exception as fix_error: print(f"WARNING: Auto-fix failed for {imdb_id} S{season:02d}E{episode:02d}: {fix_error}") # Don't fail the lookup if auto-fix fails @@ -2350,27 +2364,39 @@ async def lookup_movie(imdb_id: str, dependencies: dict): # AUTO-FIX: Update NFO file with missing dateadded element try: nfo_manager = dependencies.get("nfo_manager") - if nfo_manager and result.get('video_path'): - video_path = result['video_path'] - print(f"DEBUG: Auto-fixing NFO for movie {imdb_id} at {video_path}") + config = dependencies.get("config") + if nfo_manager and config: + print(f"DEBUG: Auto-fixing NFO for movie {imdb_id}") - # Create NFO with dateadded element + # Find the movie directory using the same logic NFOGuard uses from pathlib import Path - video_file = Path(video_path) - if video_file.exists(): - # Use NFO manager to update the movie NFO file - nfo_manager.create_movie_nfo( - video_file.parent, - imdb_id, - dateadded, - result.get('released'), - source=f"NFOGuard auto-fix via Emby lookup - {result.get('source', 'database')}" - ) - print(f"SUCCESS: Auto-fixed NFO file for movie {imdb_id}") + movie_library_path = Path(config.movie_library_path) + + # Look for movie directory with IMDb ID pattern + pattern = f"*[imdb-{imdb_id}]*" + movie_matches = list(movie_library_path.glob(f"**/{pattern}")) + + if movie_matches: + movie_dir = movie_matches[0] # Take first match + + if movie_dir.exists(): + print(f"DEBUG: Found movie directory: {movie_dir}") + + # Use NFO manager to update the movie NFO file + nfo_manager.create_movie_nfo( + movie_dir, + imdb_id, + dateadded, + result.get('released'), + source=f"NFOGuard auto-fix via Emby lookup - {result.get('source', 'database')}" + ) + print(f"SUCCESS: Auto-fixed NFO file for movie {imdb_id}") + else: + print(f"WARNING: Movie directory not found: {movie_dir}") else: - print(f"WARNING: Video file not found for auto-fix: {video_path}") + print(f"WARNING: Movie directory not found for {imdb_id} in {movie_library_path}") else: - print(f"DEBUG: Auto-fix skipped - nfo_manager: {nfo_manager is not None}, video_path: {result.get('video_path', 'None')}") + print(f"DEBUG: Auto-fix skipped - nfo_manager: {nfo_manager is not None}, config: {config is not None}") except Exception as fix_error: print(f"WARNING: Auto-fix failed for movie {imdb_id}: {fix_error}") # Don't fail the lookup if auto-fix fails