#!/usr/bin/env python3 """Создаёт минимальный локальный NFO без сетевых запросов.""" from __future__ import annotations import argparse import re import xml.etree.ElementTree as ET from pathlib import Path YEAR = re.compile(r"(? tuple[str, str]: stem = path.stem if path.is_file() else path.name matches = list(YEAR.finditer(stem)) match = matches[-1] if matches else None year = match.group(1) if match else "" title = stem[: match.start()] if match else stem title = re.sub(r"[._]+", " ", title) title = re.sub(r"\s+", " ", title).strip(" -_.") title = re.sub(r"[\s(\[{]+$", "", title).strip() return title, year def main() -> int: parser = argparse.ArgumentParser(description="Создать NFO для медиатеки") parser.add_argument("--path", type=Path, required=True, help="Папка или файл фильма") parser.add_argument("--kind", choices=["movie", "tvshow"], default="movie") parser.add_argument("--title", help="Название вручную") parser.add_argument("--year", help="Год вручную") parser.add_argument("--apply", action="store_true", help="Записать NFO") parser.add_argument("--force", action="store_true", help="Перезаписать существующий NFO") args = parser.parse_args() source = args.path.expanduser().resolve() if not source.exists(): parser.error(f"Путь не найден: {source}") folder = source if source.is_dir() else source.parent title, year = parse_name(source) title = args.title or title year = args.year or year root = ET.Element(args.kind) ET.SubElement(root, "title").text = title if year: ET.SubElement(root, "year").text = year ET.indent(root, space=" ") xml_text = ET.tostring(root, encoding="unicode") + "\n" target = folder / ("movie.nfo" if args.kind == "movie" else "tvshow.nfo") print(xml_text, end="") if not args.apply: print(f"Будет записано: {target}") print("Это предпросмотр. Для записи добавьте --apply.") return 0 if target.exists() and not args.force: parser.error(f"Файл уже существует: {target}. Добавьте --force.") target.write_text('\n' + xml_text, encoding="utf-8") print(f"Сохранено: {target}") return 0 if __name__ == "__main__": raise SystemExit(main())