From c5177ba920e79b4de770e0f2c11e26ffb578a078 Mon Sep 17 00:00:00 2001 From: sbcrumb Date: Thu, 16 Oct 2025 08:51:54 -0400 Subject: [PATCH] sonarr: fix airdates not being used. --- VERSION | 2 +- api/web_routes.py | 40 ++++++++++++++++++++++++++++++++++++-- processors/tv_processor.py | 13 ++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/VERSION b/VERSION index c36c648..8c57128 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2.17 +2.2.18 diff --git a/api/web_routes.py b/api/web_routes.py index caf4034..de175cc 100644 --- a/api/web_routes.py +++ b/api/web_routes.py @@ -906,11 +906,19 @@ async def get_episode_date_options(dependencies: dict, imdb_id: str, season: int for ep in episodes: ep_season = ep.get('seasonNumber') - ep_episode = ep.get('episodeNumber') + ep_episode = ep.get('episodeNumber') + # Convert to int for proper comparison (handle both string and int from Sonarr) + try: + ep_season = int(ep_season) if ep_season is not None else None + ep_episode = int(ep_episode) if ep_episode is not None else None + except (ValueError, TypeError): + continue # Skip episodes with invalid season/episode numbers + if ep_season == season and ep_episode == episode: episode_id = ep.get('id') ep_title = ep.get('title', 'Unknown') - print(f"🔍 DEBUG: Found target episode '{ep_title}' with ID {episode_id}") + ep_air_date = ep.get('airDate') # Get air date from Sonarr + print(f"🔍 DEBUG: Found target episode '{ep_title}' with ID {episode_id}, airDate: {ep_air_date}") if episode_id: # Get import history for this specific episode @@ -930,6 +938,34 @@ async def get_episode_date_options(dependencies: dict, imdb_id: str, season: int "description": f"Import date from Sonarr: {import_date[:10]}" }) print(f"✅ Added Sonarr import option: {import_date[:10]}") + + # If no import date but we have air date from Sonarr, add as air date option + if not import_date and ep_air_date: + current_aired = episode_data.get('aired', '') + current_dateadded = episode_data.get('dateadded', '') + + # Add air date option if different from current or missing + if not current_aired or current_aired != ep_air_date: + options.append({ + "type": "sonarr_air", + "label": "Sonarr Air Date", + "date": f"{ep_air_date}T20:00:00", + "source": "sonarr:airdate", + "description": f"Air date from Sonarr: {ep_air_date}" + }) + print(f"✅ Added Sonarr air date option: {ep_air_date}") + + # If no dateadded, suggest using air date as import date fallback + if not current_dateadded: + options.append({ + "type": "sonarr_air_fallback", + "label": "Use Air Date as Import Date", + "date": f"{ep_air_date}T20:00:00", + "source": "sonarr:aired_fallback", + "description": f"Use Sonarr air date as import date: {ep_air_date}" + }) + print(f"✅ Added Sonarr air date fallback option: {ep_air_date}") + break else: print(f"❌ No series found in Sonarr for {imdb_id}") diff --git a/processors/tv_processor.py b/processors/tv_processor.py index f39f873..7504313 100644 --- a/processors/tv_processor.py +++ b/processors/tv_processor.py @@ -229,14 +229,25 @@ class TVProcessor: _log("WARNING", f"S{season:02d}E{episode:02d}: No aired date found from external APIs for {imdb_id}") # Use air date as fallback for dateadded if no import date found - if not dateadded and aired and source != "sonarr:history.import": + if not dateadded and aired: + # Always use air date as fallback when no import date is available dateadded = aired if source == "sonarr:no_import_date": source = "sonarr:aired_fallback" + elif source == "sonarr:history.import": + # This shouldn't happen but handle it gracefully + source = "sonarr:aired_fallback" else: source = f"{source}_fallback" if source != "unknown" else "aired_fallback" _log("DEBUG", f"S{season:02d}E{episode:02d}: Using aired date as fallback: {dateadded} (source: {source})") + # Ensure air date is saved to database even if used as dateadded fallback + if aired and not dateadded: + # This is a fallback for cases where we have air date but absolutely no dateadded + dateadded = aired + source = "aired_only_fallback" + _log("INFO", f"S{season:02d}E{episode:02d}: No import date found, using air date as both aired and dateadded: {dateadded}") + episode_dates[(season, episode)] = (aired, dateadded, source) return episode_dates