fix: Implement proper episode data lookup for NFO updates
Local Docker Build (Dev) / build-dev (push) Successful in 29s

Root cause: NFO files were missing dateadded/aired because update logic
wasn't actually fetching episode data - just passing None values.

Fixed by implementing proper data lookup chain:
1. Check NFOGuard database for existing episode data
2. Query Sonarr API for import date (preferred for dateadded)
3. Get episode airdate from Sonarr
4. Fallback to using airdate as dateadded if no import date

Added _get_episode_dates_from_apis() method that follows the same
logic as regular episode processing, ensuring NFO updates get
proper dateadded and aired values instead of just basic metadata.

This should resolve the missing NFOGuard fields in episode NFO files.
This commit is contained in:
2025-09-26 09:12:43 -04:00
parent dc648a4963
commit d8525a3ec0
2 changed files with 52 additions and 4 deletions
+1 -1
View File
@@ -1 +1 @@
1.9.6 1.9.7
+51 -3
View File
@@ -189,9 +189,15 @@ class TVProcessor:
existing_nfo = self.nfo_manager.find_episode_nfo_matching_video(season_dir, season_num, episode_num) existing_nfo = self.nfo_manager.find_episode_nfo_matching_video(season_dir, season_num, episode_num)
if existing_nfo: if existing_nfo:
# Force processing of this episode to update NFO with NFOGuard data # Found NFO file - need to fetch episode data and update it
episode_dates[(season_num, episode_num)] = (None, None, "nfo_update_required") # Try to get data from APIs (same as missing episodes)
nfo_episodes_found += 1 aired, dateadded = self._get_episode_dates_from_apis(imdb_id, season_num, episode_num)
if aired or dateadded:
episode_dates[(season_num, episode_num)] = (aired, dateadded, "nfo_update_required")
nfo_episodes_found += 1
else:
# No data available from APIs, skip this episode
_log("WARNING", f"No episode data available for S{season_num:02d}E{episode_num:02d} - skipping NFO update")
if nfo_episodes_found > 0: if nfo_episodes_found > 0:
_log("INFO", f"Found {nfo_episodes_found} episodes with existing NFO files to update (preserving names)") _log("INFO", f"Found {nfo_episodes_found} episodes with existing NFO files to update (preserving names)")
@@ -262,6 +268,48 @@ class TVProcessor:
return episode_dates return episode_dates
def _get_episode_dates_from_apis(self, imdb_id: str, season_num: int, episode_num: int) -> tuple[Optional[str], Optional[str]]:
"""Get episode dates using proper fallback chain: database → Sonarr import → airdate"""
aired = None
dateadded = None
# First check if it's already in database
existing_ep = self.db.get_episode_date(imdb_id, season_num, episode_num)
if existing_ep and existing_ep.get('dateadded'):
_log("DEBUG", f"Found episode S{season_num:02d}E{episode_num:02d} in database")
return existing_ep.get('aired'), existing_ep.get('dateadded')
# Try Sonarr API for import date (preferred)
if self.sonarr.enabled:
series = self.sonarr.series_by_imdb(imdb_id)
if series:
series_id = series.get("id")
if series_id:
episodes = self.sonarr.episodes_for_series(series_id)
for ep in episodes:
if ep.get("episodeNumber") == episode_num and ep.get("seasonNumber") == season_num:
# Get import date (preferred for dateadded)
if ep.get("episodeFile", {}).get("dateAdded"):
dateadded = ep["episodeFile"]["dateAdded"]
_log("DEBUG", f"Got Sonarr import date for S{season_num:02d}E{episode_num:02d}: {dateadded}")
# Get airdate
if ep.get("airDateUtc"):
aired = ep["airDateUtc"]
_log("DEBUG", f"Got Sonarr airdate for S{season_num:02d}E{episode_num:02d}: {aired}")
elif ep.get("airDate"):
aired = ep["airDate"] + "T00:00:00Z"
_log("DEBUG", f"Got Sonarr airdate (no time) for S{season_num:02d}E{episode_num:02d}: {aired}")
break
# Fallback: use airdate as dateadded if no import date available
if not dateadded and aired:
dateadded = aired
_log("DEBUG", f"Using airdate as dateadded fallback for S{season_num:02d}E{episode_num:02d}")
return aired, dateadded
def _parse_date_to_iso(self, date_str: str) -> Optional[str]: def _parse_date_to_iso(self, date_str: str) -> Optional[str]:
"""Parse date string to ISO format""" """Parse date string to ISO format"""
if not date_str: if not date_str: