Correction git
This commit is contained in:
1 parent
795e4efe29
commit
1198528f31
8 files changed
+494
No files matched your search
@@ -0,0 +1,130 @@
|
||||
"""Conversion d'une ISO en MKV puis import dans la bibliothèque Jellyfin.
|
||||
|
||||
Le MKV est écrit dans un dossier dédié sous le répertoire de téléchargements
|
||||
qBittorrent, puis lié (lien dur `ln`) dans le dossier du film sous la
|
||||
bibliothèque Jellyfin après nettoyage de ce dossier.
|
||||
"""
|
||||
|
||||
import errno
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from app.config import settings
|
||||
from app.models import IsoMovie
|
||||
|
||||
INVALID_CHARACTERS = re.compile(r'[/\\:*?"<>|]')
|
||||
|
||||
|
||||
def guarded_folder(folder: Path) -> Path:
|
||||
resolved = folder.resolve()
|
||||
films_root = settings.films_dir.resolve()
|
||||
if not resolved.is_relative_to(films_root):
|
||||
raise RuntimeError(f"Refus de toucher un dossier hors de {films_root}: {resolved}")
|
||||
return resolved
|
||||
|
||||
|
||||
def sanitize_component(value: str) -> str:
|
||||
cleaned = INVALID_CHARACTERS.sub(" ", value).strip()
|
||||
cleaned = re.sub(r"\s{2,}", " ", cleaned)
|
||||
return cleaned or "Sans titre"
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConversionPaths:
|
||||
downloads_folder: Path
|
||||
mkv_path: Path
|
||||
destination_folder: Path
|
||||
|
||||
|
||||
def conversion_paths(movie: IsoMovie) -> ConversionPaths:
|
||||
title = sanitize_component(movie.title)
|
||||
movie_name = f"{title} {movie.year}" if movie.year else title
|
||||
mkv_name = f"{title} ({movie.year}).mkv" if movie.year else f"{title}.mkv"
|
||||
downloads_folder = settings.downloads_dir / movie_name
|
||||
|
||||
iso_folder = Path(movie.path).parent
|
||||
films_root = settings.films_dir.resolve()
|
||||
if iso_folder.resolve().is_relative_to(films_root):
|
||||
destination_folder = iso_folder
|
||||
else:
|
||||
destination_folder = films_root / movie_name
|
||||
return ConversionPaths(
|
||||
downloads_folder=downloads_folder,
|
||||
mkv_path=downloads_folder / mkv_name,
|
||||
destination_folder=destination_folder,
|
||||
)
|
||||
|
||||
|
||||
def extract_mkv(movie: IsoMovie, mkv_path: Path) -> None:
|
||||
mkv_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if settings.simulate_conversion:
|
||||
# Simulation en attendant MakeMKV sur le NAS : fichier MKV de substitution.
|
||||
mkv_path.write_text(
|
||||
"MKV simule par MKVTool — MakeMKV n'est pas installe dans cet environnement.\n"
|
||||
)
|
||||
return
|
||||
if shutil.which("makemkvcon") is None:
|
||||
raise RuntimeError("MakeMKV (makemkvcon) n'est pas installe sur le serveur")
|
||||
result = subprocess.run(
|
||||
["makemkvcon", "mkv", f"iso:{movie.path}", "0", str(mkv_path.parent)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
detail = result.stderr.strip() or result.stdout.strip()
|
||||
raise RuntimeError(f"MakeMKV a echoue: {detail}")
|
||||
produced = sorted(mkv_path.parent.glob("*.mkv"))
|
||||
if not produced:
|
||||
raise RuntimeError("MakeMKV n'a produit aucun fichier MKV")
|
||||
produced[0].replace(mkv_path)
|
||||
|
||||
|
||||
def import_mkv(mkv_path: Path, destination_folder: Path) -> Path:
|
||||
"""Crée le lien du MKV dans le dossier du film, sans toucher au reste.
|
||||
|
||||
L'ISO et les autres fichiers du dossier ne sont supprimés qu'à la
|
||||
validation manuelle (finalize_validation).
|
||||
"""
|
||||
resolved = guarded_folder(destination_folder)
|
||||
resolved.mkdir(parents=True, exist_ok=True)
|
||||
target = resolved / mkv_path.name
|
||||
if target.exists() or target.is_symlink():
|
||||
target.unlink()
|
||||
try:
|
||||
os.link(mkv_path, target)
|
||||
except OSError as error:
|
||||
if error.errno != errno.EXDEV:
|
||||
raise
|
||||
# Volumes différents: un lien dur est impossible, on retombe sur un lien symbolique.
|
||||
os.symlink(mkv_path, target)
|
||||
return target
|
||||
|
||||
|
||||
def finalize_validation(destination_folder: Path, keep_name: str) -> None:
|
||||
"""Supprime l'ISO et les autres fichiers du dossier, ne garde que le MKV lié."""
|
||||
resolved = guarded_folder(destination_folder)
|
||||
for entry in resolved.iterdir():
|
||||
if entry.name == keep_name:
|
||||
continue
|
||||
if entry.is_dir() and not entry.is_symlink():
|
||||
shutil.rmtree(entry)
|
||||
else:
|
||||
entry.unlink()
|
||||
|
||||
|
||||
def cancel_conversion(mkv_path: Path, destination_folder: Path) -> None:
|
||||
"""Annule une conversion en attente de validation.
|
||||
|
||||
Supprime le MKV converti et son lien dans le dossier du film (s'ils
|
||||
existent); l'ISO et le reste du dossier ne sont pas touches.
|
||||
"""
|
||||
target = destination_folder / mkv_path.name
|
||||
for path in (target, mkv_path):
|
||||
if path.is_symlink() or path.exists():
|
||||
path.unlink()
|
||||
if mkv_path.parent.is_dir() and not any(mkv_path.parent.iterdir()):
|
||||
mkv_path.parent.rmdir()
|
||||
Reference in new issue
Block a user