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
+20 -12
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:
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"