From e8976563967f8ddcc426a07b3a40dcdaefe5af7e Mon Sep 17 00:00:00 2001 From: sbcrumb Date: Thu, 9 Oct 2025 18:10:11 -0400 Subject: [PATCH 1/2] Fix TV rename webhooks: Implement targeted episode processing - Extract episode info from episodeFile when webhook.episodes is empty for rename events - Create proper episode data structure for process_webhook_episodes method - Prevents full series processing (177 episodes) for single episode renames - Dramatically improves performance for rename operations - Version bump to 2.0.11 Resolves issue where rename webhooks were falling back to full series processing instead of targeted mode. --- VERSION | 2 +- api/routes.py | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 0a69206..6cbacdc 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.10 +2.0.11 diff --git a/api/routes.py b/api/routes.py index ad7e582..a7df1db 100644 --- a/api/routes.py +++ b/api/routes.py @@ -72,13 +72,32 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de print(f"ERROR: Could not find series directory: {series_title} ({imdb_id})") return {"status": "error", "reason": "Series directory not found"} + # Extract episode data for targeted processing + episodes_data = webhook.episodes or [] + + # For rename events, if no episodes in webhook.episodes, try to extract from episodeFile + if webhook.eventType == "Rename" and not episodes_data and webhook.episodeFile: + episode_file = webhook.episodeFile + # Extract season and episode from episodeFile if available + season_num = episode_file.get("seasonNumber") + episode_num = episode_file.get("episodeNumber") + if season_num and episode_num: + # Create episode data structure that matches what process_webhook_episodes expects + episodes_data = [{ + "seasonNumber": season_num, + "episodeNumber": episode_num, + "id": episode_file.get("id"), + "title": episode_file.get("title") + }] + print(f"INFO: Extracted episode info from episodeFile for rename: S{season_num:02d}E{episode_num:02d}") + # Add to batch queue with TV-prefixed key to avoid movie conflicts tv_batch_key = f"tv:{imdb_id}" webhook_dict = { 'path': str(series_path), 'series_info': series_info, 'event_type': webhook.eventType, - 'episodes': webhook.episodes or [], # Include episode data for targeted processing + 'episodes': episodes_data, # Include enhanced episode data for targeted processing 'processing_mode': config.tv_webhook_processing_mode } batcher.add_webhook(tv_batch_key, webhook_dict, 'tv') -- 2.39.5 From 3787ca2d230f7914f2c37567be0b465779272cbd Mon Sep 17 00:00:00 2001 From: sbcrumb Date: Thu, 9 Oct 2025 18:11:47 -0400 Subject: [PATCH 2/2] Fix all Sonarr webhooks: Force targeted processing for single episodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extend episodeFile extraction to ALL webhook types (Download, Rename, Upgrade) - Force targeted mode when episodes_data has ≤3 episodes to prevent full series processing - Add debug logging to track episode extraction and processing mode decisions - Prevents processing 177 episodes when only 1-3 episodes are involved - Applies to imports, renames, and upgrades - not just renames - Version bump to 2.0.12 This ensures that single episode operations (most common) only process the specific episode(s) involved instead of scanning the entire series. --- VERSION | 2 +- api/routes.py | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/VERSION b/VERSION index 6cbacdc..280a1e3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.11 +2.0.12 diff --git a/api/routes.py b/api/routes.py index a7df1db..cd126b3 100644 --- a/api/routes.py +++ b/api/routes.py @@ -74,9 +74,11 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de # Extract episode data for targeted processing episodes_data = webhook.episodes or [] + print(f"DEBUG: Initial episodes_data from webhook.episodes: {len(episodes_data)} episodes") - # For rename events, if no episodes in webhook.episodes, try to extract from episodeFile - if webhook.eventType == "Rename" and not episodes_data and webhook.episodeFile: + # For all webhook events, if no episodes in webhook.episodes, try to extract from episodeFile + # This ensures targeted processing for single episode operations (Download, Rename, Upgrade) + if not episodes_data and webhook.episodeFile: episode_file = webhook.episodeFile # Extract season and episode from episodeFile if available season_num = episode_file.get("seasonNumber") @@ -89,7 +91,13 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de "id": episode_file.get("id"), "title": episode_file.get("title") }] - print(f"INFO: Extracted episode info from episodeFile for rename: S{season_num:02d}E{episode_num:02d}") + print(f"INFO: Extracted episode info from episodeFile for {webhook.eventType}: S{season_num:02d}E{episode_num:02d}") + + # Force targeted mode for single-episode webhooks to prevent full series processing + processing_mode = config.tv_webhook_processing_mode + if episodes_data and len(episodes_data) <= 3: # Single episode or small batch + processing_mode = "targeted" + print(f"INFO: Forcing targeted mode for {len(episodes_data)} episode(s)") # Add to batch queue with TV-prefixed key to avoid movie conflicts tv_batch_key = f"tv:{imdb_id}" @@ -98,7 +106,7 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de 'series_info': series_info, 'event_type': webhook.eventType, 'episodes': episodes_data, # Include enhanced episode data for targeted processing - 'processing_mode': config.tv_webhook_processing_mode + 'processing_mode': processing_mode # Use forced targeted mode when appropriate } batcher.add_webhook(tv_batch_key, webhook_dict, 'tv') -- 2.39.5