fix: webhook episode processing now uses real Sonarr import history

Critical Fix for Episode Import Dates:
- Webhook episodes now query Sonarr import history instead of using current time
- Fix _get_webhook_episode_date() to follow same logic as manual scans
- Convert import dates from UTC to local timezone for NFO dateadded fields
- Episodes now show 'sonarr:history.import' source instead of 'webhook:new_download'

Before: <dateadded>2025-09-14T12:40:07+00:00</dateadded> (current webhook time)
After:  <dateadded>2025-09-14T08:40:07-04:00</dateadded> (real import time in local timezone)

This fixes the issue where episodes downloaded at 8:40am EST were showing
12:40pm UTC webhook time instead of the actual import timestamp.

Enhanced webhook processing now:
1. Checks database for existing entries (prevents duplicates)
2. Queries Sonarr import history for real import dates
3. Converts UTC import dates to local timezone
4. Only uses current time as last resort for true new downloads
This commit is contained in:
2025-09-14 10:41:43 -04:00
parent d8754e41a7
commit e188706001
3 changed files with 56 additions and 14 deletions
+35 -1
View File
@@ -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**: `<dateadded>2025-09-14T12:40:07+00:00</dateadded>` 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
<!-- Before (Wrong) -->
<dateadded>2025-09-14T12:40:07+00:00</dateadded> <!-- Current webhook time in UTC -->
<!-- source: TV Episode; webhook:new_download -->
<!-- After (Correct) -->
<dateadded>2025-09-14T08:40:07-04:00</dateadded> <!-- Real import time in Eastern -->
<!-- source: TV Episode; sonarr:history.import -->
```
**🛠️ 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
+1 -1
View File
@@ -1 +1 @@
0.6.6
0.6.7
+19 -11
View File
@@ -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:
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"