diff --git a/core/nfo_manager.py b/core/nfo_manager.py index 0c650fe..b801406 100644 --- a/core/nfo_manager.py +++ b/core/nfo_manager.py @@ -633,22 +633,32 @@ class NFOManager: def create_episode_nfo(self, season_dir: Path, season_num: int, episode_num: int, aired: Optional[str], dateadded: Optional[str], source: str, lock_metadata: bool = True, enhanced_metadata: Optional[Dict[str, Any]] = None) -> None: - """Create or update episode NFO file preserving existing content""" - # Generate episode filename pattern - episode_filename = f"S{season_num:02d}E{episode_num:02d}.nfo" - nfo_path = season_dir / episode_filename - - # Track if we need to delete an old long-named NFO file - old_nfo_to_delete = None + """Create or update episode NFO file - preserves existing long names matching video files""" try: - # First, check for existing long-named NFO files that need migration - existing_long_nfo = self.find_existing_episode_nfo(season_dir, season_num, episode_num) + # First, check for NFO files that match video file names (preserve long names) + existing_matching_nfo = self.find_episode_nfo_matching_video(season_dir, season_num, episode_num) - # Prioritize long-named file for migration, otherwise use standard file - source_nfo_path = existing_long_nfo if existing_long_nfo else nfo_path if nfo_path.exists() else None + if existing_matching_nfo: + # Update the existing NFO in-place while preserving its filename + print(f"🎯 Updating existing NFO (preserving name): {existing_matching_nfo.name}") + success = self.update_episode_nfo_preserving_name( + existing_matching_nfo, season_num, episode_num, aired, dateadded, source + ) + if success: + return # Successfully updated, we're done + else: + print(f"⚠️ Failed to update existing NFO, falling back to standard creation") + + # Fallback: Create standard S01E01.nfo format (for new episodes without existing NFOs) + episode_filename = f"S{season_num:02d}E{episode_num:02d}.nfo" + nfo_path = season_dir / episode_filename + + # Check if standard format NFO exists + source_nfo_path = nfo_path if nfo_path.exists() else None if source_nfo_path: + # Standard NFO file processing (S01E01.nfo format) try: tree = ET.parse(source_nfo_path) episode = tree.getroot() @@ -657,33 +667,16 @@ class NFOManager: if episode.tag != "episodedetails": raise ValueError("Root element is not ") - # If we're migrating from a long-named file, mark it for deletion - if existing_long_nfo and source_nfo_path == existing_long_nfo: - old_nfo_to_delete = existing_long_nfo - print(f"📦 Migrating episode NFO: {existing_long_nfo.name} -> {episode_filename}") - - # Show what content fields are being preserved - content_fields = ["title", "plot", "runtime", "premiered"] - preserved_content = [] - for field in content_fields: - elem = episode.find(field) - if elem is not None and elem.text: - preserved_content.append(field) - if preserved_content: - print(f" 📄 Preserving content: {', '.join(preserved_content)}") + print(f"📝 Updating standard NFO: {episode_filename}") - # Preserve existing content fields and store NFOGuard-managed fields for re-adding - preserved_values = {} - - # Extract and preserve NFOGuard-managed fields (we'll re-add these at the bottom) + # Remove existing NFOGuard fields to re-add them at the bottom nfoguard_fields = ["aired", "dateadded", "season", "episode"] for tag in nfoguard_fields: existing = episode.find(tag) if existing is not None: - # Store the value before removing + # Preserve existing aired date if not provided if tag == "aired" and not aired: - aired = existing.text # Preserve existing aired date - preserved_values[tag] = existing.text + aired = existing.text episode.remove(existing) # Important: DO NOT remove content fields like title, plot, runtime, premiered, etc. @@ -776,13 +769,7 @@ class NFOManager: print(f"✅ Successfully created/updated episode NFO: {nfo_path}") print(f" S{season_num:02d}E{episode_num:02d}, Aired: {aired}, Date Added: {dateadded}") - # Clean up old long-named NFO file if we migrated from it - if old_nfo_to_delete and old_nfo_to_delete.exists(): - try: - old_nfo_to_delete.unlink() - print(f"🗑️ Cleaned up old NFO file: {old_nfo_to_delete.name}") - except Exception as cleanup_error: - print(f"⚠️ Warning: Could not delete old NFO file {old_nfo_to_delete.name}: {cleanup_error}") + # No cleanup needed - we preserve existing NFO filenames except Exception as e: print(f"❌ Error creating/updating episode NFO {nfo_path}: {e}")