This commit is contained in:
2025-09-14 13:40:22 -04:00
parent 05ba16666e
commit 49266d651a
3 changed files with 70 additions and 29 deletions
+41 -10
View File
@@ -37,29 +37,60 @@ class NFOManager:
def create_movie_nfo(self, movie_dir: Path, imdb_id: str, dateadded: str,
released: Optional[str] = None, source: str = "unknown",
lock_metadata: bool = True) -> None:
"""Create movie.nfo file with proper metadata"""
"""Create or update movie.nfo file preserving existing content"""
nfo_path = movie_dir / "movie.nfo"
try:
movie = ET.Element("movie")
# Try to load existing NFO file
if nfo_path.exists():
try:
tree = ET.parse(nfo_path)
movie = tree.getroot()
# Ensure root element is <movie>
if movie.tag != "movie":
raise ValueError("Root element is not <movie>")
# Remove existing NFOGuard elements to avoid duplicates
for elem in movie.findall(".//comment()"):
if elem is not None and "NFOGuard" in str(elem):
movie.remove(elem)
# Remove existing NFOGuard-managed elements
for tag in ["dateadded", "lockdata"]:
existing = movie.find(tag)
if existing is not None:
movie.remove(existing)
# Remove any existing uniqueid with type="imdb"
for uniqueid in movie.findall("uniqueid[@type='imdb']"):
movie.remove(uniqueid)
except (ET.ParseError, ValueError) as e:
print(f"Warning: Could not parse existing NFO {nfo_path}, creating new: {e}")
movie = ET.Element("movie")
else:
# Create new NFO structure
movie = ET.Element("movie")
# Add unique ID
# Add/update NFOGuard comment
comment = ET.Comment(f" Created by {self.manager_brand} - Source: {source} ")
movie.insert(0, comment)
# Add/update IMDb uniqueid
uniqueid = ET.SubElement(movie, "uniqueid", type="imdb", default="true")
uniqueid.text = imdb_id
# Add dates
# Add dateadded
if dateadded:
dateadded_elem = ET.SubElement(movie, "dateadded")
dateadded_elem.text = dateadded
if released:
# Add premiered if we have release date (only if not already present)
if released and movie.find("premiered") is None:
premiered_elem = ET.SubElement(movie, "premiered")
premiered_elem.text = released[:10] if len(released) >= 10 else released
# Add source comment
comment = ET.Comment(f" Created by {self.manager_brand} - Source: {source} ")
movie.insert(0, comment)
# Add lockdata if requested
if lock_metadata:
lockdata = ET.SubElement(movie, "lockdata")
@@ -71,7 +102,7 @@ class NFOManager:
tree.write(nfo_path, encoding="utf-8", xml_declaration=True)
except Exception as e:
print(f"Error creating movie NFO {nfo_path}: {e}")
print(f"Error creating/updating movie NFO {nfo_path}: {e}")
def create_tvshow_nfo(self, series_dir: Path, imdb_id: str, tvdb_id: Optional[str] = None) -> None:
"""Create tvshow.nfo file"""