fix: Replace NFO migration logic with preservation logic in create_episode_nfo

The core issue was that create_episode_nfo() method was still using the old
migration logic that renamed long NFO files to S01E01.nfo format.

Changes:
- Modified create_episode_nfo() to check for video-matching NFO files first
- Update existing NFO files in-place while preserving original filenames
- Removed old migration and cleanup logic that deleted original files
- Added clear logging to distinguish preservation vs standard creation

This ensures TV episode NFO files keep their original long names that
match the video files exactly, while still getting NFOGuard metadata.
This commit is contained in:
2025-09-26 08:28:39 -04:00
parent 0b17ee54fd
commit 16bcf89ddd
+26 -39
View File
@@ -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 <episodedetails>")
# 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}")