diff --git a/SUMMARY.md b/SUMMARY.md index 621eccb..a5f315e 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -293,6 +293,40 @@ sonarr-nfo-cache | [2025-09-14T09:37:00-04:00] INFO: Batched movie webhook... --- +--- + +### 🆕 v0.6.7 Webhook Episode Import History Fix (September 2025) + +**🎯 Critical Webhook Processing Fix:** +- **Problem**: Webhook episodes used current time instead of actual Sonarr import history +- **Issue**: `2025-09-14T12:40:07+00:00` showed webhook time (UTC) instead of real import time (local) +- **Root Cause**: `_get_webhook_episode_date()` wasn't querying Sonarr import history like manual scans + +**⚡ Enhanced Webhook Episode Processing:** +- **Sonarr History Lookup**: Webhooks now query Sonarr import history for real import dates +- **Local Timezone Conversion**: Import dates converted to container timezone (Eastern Time) +- **Proper Source Attribution**: Episodes now show `sonarr:history.import` instead of `webhook:new_download` when history exists +- **Fallback Logic**: Only uses current time when no Sonarr history found (true new downloads) + +**📋 Expected Results:** +```xml + +2025-09-14T12:40:07+00:00 + + + +2025-09-14T08:40:07-04:00 + +``` + +**🛠️ Technical Implementation:** +- **Enhanced Logic**: Webhooks now follow same import history priority as manual scans +- **Timezone Consistency**: All import dates converted to local timezone before NFO generation +- **Better Logging**: Shows whether import history was found or current time used as fallback +- **Upgrade Handling**: Episodes downloaded multiple times now preserve original import dates + +--- + **Last Updated:** September 14, 2025 -**Version:** v0.6.6 +**Version:** v0.6.7 **Status:** Enhanced Production Ready \ No newline at end of file diff --git a/VERSION b/VERSION index 05e8a45..2228cad 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.6.6 +0.6.7 diff --git a/nfoguard.py b/nfoguard.py index ca47523..819b4ac 100644 --- a/nfoguard.py +++ b/nfoguard.py @@ -857,15 +857,11 @@ class TVProcessor: else: _log("DEBUG", f"No database entry found for IMDb={imdb_id}, S{season_num:02d}E{episode_num:02d}") - # Step 2: This is a new download (webhook triggered) - use current time in local timezone - local_tz = _get_local_timezone() - current_time = datetime.now(local_tz).isoformat(timespec="seconds") - _log("INFO", f"New episode download detected via webhook - using current time: {current_time}") - - # Step 3: Try to get air date for reference + # Step 2: Try to get real import date from Sonarr history (even for webhooks) aired = None + import_date = None - # Try Sonarr metadata first + # Try Sonarr metadata first for air date if series_metadata and "episodes" in series_metadata: for episode in series_metadata["episodes"]: if episode.get("seasonNumber") == season_num and episode.get("episodeNumber") == episode_num: @@ -873,20 +869,32 @@ class TVProcessor: if aired: break - # Fallback to Sonarr API if no metadata - if not aired and self.sonarr.enabled: + # Try Sonarr API for both air date and import history + if self.sonarr.enabled: try: series = self.sonarr.series_by_imdb(imdb_id) if series: episodes = self.sonarr.episodes_for_series(series["id"]) for ep in episodes: if ep.get("seasonNumber") == season_num and ep.get("episodeNumber") == episode_num: - aired = ep.get("airDateUtc") + if not aired: # Only get aired if we don't have it from metadata + aired = ep.get("airDateUtc") + # Get import history for this episode + import_date = self.sonarr.get_episode_import_history(ep["id"]) + if import_date: + # Convert import date to local timezone + local_import_date = convert_utc_to_local(import_date) + _log("INFO", f"Found Sonarr import history for webhook S{season_num:02d}E{episode_num:02d}: {local_import_date}") + return aired, local_import_date, "sonarr:history.import" break except Exception as e: - _log("DEBUG", f"Error getting air date from Sonarr: {e}") + _log("DEBUG", f"Error getting episode data from Sonarr: {e}") + + # Step 3: Last resort - use current time (webhook triggered but no history found) + local_tz = _get_local_timezone() + current_time = datetime.now(local_tz).isoformat(timespec="seconds") + _log("INFO", f"No Sonarr import history found - using current time for webhook: {current_time}") - # Return: aired date for reference, current time as import date return aired, current_time, "webhook:new_download"