This commit is contained in:
@@ -921,8 +921,8 @@ class TVProcessor:
|
||||
"""
|
||||
Get episode date for webhook processing with correct priority:
|
||||
1. Check existing NFOGuard database entry (preserve previous import dates)
|
||||
2. If not found, use current time (webhook = new download)
|
||||
3. Get aired date from Sonarr/TMDB for reference
|
||||
2. Check Sonarr import history - if old history exists, prefer air date
|
||||
3. If no history, use current time (webhook = new download)
|
||||
"""
|
||||
# Check if we already have this episode in our database (preserve existing dates)
|
||||
existing = self.db.get_episode_date(imdb_id, season_num, episode_num)
|
||||
@@ -938,7 +938,43 @@ class TVProcessor:
|
||||
aired = episode_data.get('airDate')
|
||||
_log("DEBUG", f"Got aired date from Sonarr for S{season_num:02d}E{episode_num:02d}: {aired}")
|
||||
|
||||
# For webhook processing, use current time as dateadded (new download)
|
||||
# NEW: Check Sonarr import history to detect if this is an existing show
|
||||
episode_id = None
|
||||
if series_metadata and 'episodes' in series_metadata:
|
||||
episode_data = series_metadata['episodes'].get((season_num, episode_num))
|
||||
if episode_data:
|
||||
episode_id = episode_data.get('id')
|
||||
|
||||
if episode_id and hasattr(self, 'sonarr'):
|
||||
try:
|
||||
import_history = self.sonarr.get_episode_import_history(episode_id)
|
||||
if import_history:
|
||||
from datetime import datetime as dt
|
||||
from dateutil import parser
|
||||
|
||||
# Parse the import history date
|
||||
history_date = parser.parse(import_history)
|
||||
current_time = dt.now(history_date.tzinfo)
|
||||
time_diff = current_time - history_date
|
||||
|
||||
# If import history is more than 24 hours old, this is likely a re-download/upgrade
|
||||
# In this case, prefer air date over webhook date
|
||||
if time_diff.days > 0:
|
||||
_log("INFO", f"Found old import history for S{season_num:02d}E{episode_num:02d} ({import_history}), using air date instead of webhook date")
|
||||
if aired:
|
||||
# Use air date for shows with existing history
|
||||
return aired, aired + "T20:00:00", "airdate"
|
||||
else:
|
||||
# Fallback to import history if no air date
|
||||
return aired, import_history, "sonarr:import_history"
|
||||
else:
|
||||
# Recent import, use the import date
|
||||
_log("DEBUG", f"Found recent import history for S{season_num:02d}E{episode_num:02d}: {import_history}")
|
||||
return aired, import_history, "sonarr:import_history"
|
||||
except Exception as e:
|
||||
_log("DEBUG", f"Error checking Sonarr import history for S{season_num:02d}E{episode_num:02d}: {e}")
|
||||
|
||||
# For webhook processing with no history, use current time as dateadded (new download)
|
||||
dateadded = datetime.now().isoformat()
|
||||
source = "sonarr:webhook"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user