fix: Improve title detection logic to prevent duplicates
Local Docker Build (Dev) / build-dev (push) Successful in 26s

- Fix flawed title detection that caused duplicates when migrating NFOs
- Add robust title validation (check for non-empty, non-whitespace text)
- Improve debugging output to show title detection process
- Properly handle existing valid titles vs missing/empty titles
- Remove invalid title elements before adding filename-extracted titles

Resolves duplicate <title> elements in NFO files during migration.
This commit is contained in:
2025-09-25 10:33:03 -04:00
parent f1420cac58
commit 6a20337181
+13 -7
View File
@@ -696,15 +696,19 @@ class NFOManager:
runtime_elem = ET.SubElement(episode, "runtime") runtime_elem = ET.SubElement(episode, "runtime")
runtime_elem.text = str(enhanced_metadata["runtime"]) runtime_elem.text = str(enhanced_metadata["runtime"])
# Fallback: Extract title from filename if no title present # Fallback: Extract title from filename if no valid title present
if not episode.find("title") or not episode.find("title").text: title_elem = episode.find("title")
print(f"🔍 No title found in NFO for S{season_num:02d}E{episode_num:02d}, attempting filename extraction") has_valid_title = title_elem is not None and title_elem.text and title_elem.text.strip()
print(f"🔍 Title check for S{season_num:02d}E{episode_num:02d}: has_element={title_elem is not None}, has_text={title_elem.text if title_elem is not None else 'N/A'}, has_valid_title={has_valid_title}")
if not has_valid_title:
print(f"🔍 No valid title found in NFO for S{season_num:02d}E{episode_num:02d}, attempting filename extraction")
filename_title = self._extract_title_from_filename(season_dir, season_num, episode_num) filename_title = self._extract_title_from_filename(season_dir, season_num, episode_num)
if filename_title: if filename_title:
# Remove existing empty title element if present # Remove existing empty/invalid title element if present
existing_title = episode.find("title") if title_elem is not None:
if existing_title is not None: episode.remove(title_elem)
episode.remove(existing_title)
# Add new title element # Add new title element
title_elem = ET.SubElement(episode, "title") title_elem = ET.SubElement(episode, "title")
@@ -712,6 +716,8 @@ class NFOManager:
print(f"✅ Added filename-extracted title to NFO: S{season_num:02d}E{episode_num:02d} - '{filename_title}'") print(f"✅ Added filename-extracted title to NFO: S{season_num:02d}E{episode_num:02d} - '{filename_title}'")
else: else:
print(f"⚠️ Could not extract title from filename for S{season_num:02d}E{episode_num:02d}") print(f"⚠️ Could not extract title from filename for S{season_num:02d}E{episode_num:02d}")
else:
print(f"✅ Valid title already exists in NFO for S{season_num:02d}E{episode_num:02d}: '{title_elem.text.strip()}')")
# Add NFOGuard fields at the bottom # Add NFOGuard fields at the bottom