diff --git a/api/routes.py b/api/routes.py index 74b657e..142a83d 100644 --- a/api/routes.py +++ b/api/routes.py @@ -107,9 +107,26 @@ async def sonarr_webhook(request: Request, background_tasks: BackgroundTasks, de if not episodes_data and webhook.episodeFile: episode_file = webhook.episodeFile - # Extract season and episode from episodeFile if available + # Extract season and episode from episodeFile path/filename season_num = episode_file.get("seasonNumber") - episode_num = episode_file.get("episodeNumber") + episode_num = episode_file.get("episodeNumber") + + # If not directly available, parse from relativePath or path + if not (season_num and episode_num): + from utils.nfo_patterns import extract_episode_info_from_filename + + # Try relativePath first, then path + file_path = episode_file.get("relativePath") or episode_file.get("path", "") + print(f"DEBUG: Parsing episode info from path: {file_path}") + + episode_info = extract_episode_info_from_filename(file_path) + if episode_info: + season_num = episode_info["season"] + episode_num = episode_info["episode"] + print(f"DEBUG: Extracted from filename - Season: {season_num}, Episode: {episode_num}") + else: + print(f"DEBUG: Could not extract season/episode from filename: {file_path}") + print(f"DEBUG: episodeFile seasonNumber: {season_num}, episodeNumber: {episode_num}") if season_num and episode_num: # Create episode data structure that matches what process_webhook_episodes expects diff --git a/core/episode_nfo_manager.py b/core/episode_nfo_manager.py index b6823d4..24b1858 100644 --- a/core/episode_nfo_manager.py +++ b/core/episode_nfo_manager.py @@ -220,7 +220,7 @@ class EpisodeNFOManager: lockdata_elem = ET.SubElement(episode_elem, "lockdata") lockdata_elem.text = "true" - # Add comment with source + # Add comment with source at the bottom comment = ET.Comment(f" Created by {self.manager_brand} - Source: {source} ") episode_elem.append(comment) diff --git a/core/nfo_manager.py b/core/nfo_manager.py index b03507e..5dd4854 100644 --- a/core/nfo_manager.py +++ b/core/nfo_manager.py @@ -342,10 +342,6 @@ class NFOManager: # This ensures they appear as a group at the bottom of the file nfoguard_elements = [] - # Add NFOGuard comment marker as the first of our additions - nfoguard_comment = ET.Comment(f" NFOGuard - Source: {source} ") - nfoguard_elements.append(nfoguard_comment) - # Add IMDb uniqueid uniqueid = ET.Element("uniqueid", type="imdb", default="true") uniqueid.text = imdb_id @@ -384,6 +380,10 @@ class NFOManager: lockdata.text = "true" nfoguard_elements.append(lockdata) + # Add NFOGuard comment as the very last element (appears at bottom) + nfoguard_comment = ET.Comment(f" NFOGuard - Source: {source} ") + nfoguard_elements.append(nfoguard_comment) + # Now append all NFOGuard elements to the movie in one batch # This ensures they appear as a contiguous block at the bottom for elem in nfoguard_elements: @@ -395,7 +395,7 @@ class NFOManager: tree = ET.ElementTree(movie) ET.indent(tree, space=" ", level=0) - # Write directly to file (comment is already embedded in XML) + # Write directly to file (comment is already embedded in XML at bottom) with open(nfo_path, 'w', encoding='utf-8') as f: f.write('\n') tree.write(f, encoding='unicode', xml_declaration=False) @@ -455,22 +455,14 @@ class NFOManager: lockdata = ET.SubElement(tvshow, "lockdata") lockdata.text = "true" - # Add NFOGuard comment at the beginning - comment_text = f" Created by {self.manager_brand} " + # Add NFOGuard comment at the bottom + comment = ET.Comment(f" Created by {self.manager_brand} ") + tvshow.append(comment) # Write file with proper formatting tree = ET.ElementTree(tvshow) ET.indent(tree, space=" ", level=0) - - # Write to string first to add comment - xml_str = ET.tostring(tvshow, encoding='unicode') - - # Add XML declaration and comment - full_xml = f'\n\n{xml_str}' - - # Write to file - with open(nfo_path, 'w', encoding='utf-8') as f: - f.write(full_xml) + tree.write(nfo_path, encoding='utf-8', xml_declaration=True) print(f"✅ Successfully created/updated TV show NFO: {nfo_path}") print(f" IMDb ID: {imdb_id}" + (f", TVDB ID: {tvdb_id}" if tvdb_id else ""))