✨ Scan: rattrapage des chemins de telechargement (qb_content_path) apres changement de montage - torrent qBittorrent restant ou recherche bornee par nom

This commit is contained in:
Orionis312050 committed 2026-10-14 17:44:22 +00:00
1 parent daf6a54190
commit 280102952b
3 files changed
+123 -2

No files matched your search

+72 -1
View File
@@ -3,7 +3,7 @@ import os
import shutil
from contextlib import asynccontextmanager
from datetime import UTC, datetime
from pathlib import Path
from pathlib import Path, PurePath
from typing import Annotated
import httpx
@@ -453,6 +453,9 @@ async def scan_radarr(db: DbSession) -> ScanResult:
item.radarr_linked = True
item.last_seen_at = now
refreshed += 1
# Rattrapage des chemins de telechargement (qb_content_path) apres un
# changement de montage: comptabilise dans « refreshed ».
refreshed += await _refresh_stale_qb_content_paths(db)
db.commit()
return ScanResult(
scanned_movies=len(movies),
@@ -464,6 +467,74 @@ async def scan_radarr(db: DbSession) -> ScanResult:
# --- Conversion (file traitee par le worker, une tache a la fois) ---
def _find_downloaded_iso(name: str) -> str | None:
"""Retrouve l'ISO par son nom sous la racine des telechargements.
Recherche bornee: la racine meme puis un niveau de sous-dossier
(l'arborescence qBittorrent usuelle — un dossier par torrent); un
parcours recursif complet serait trop lourd sur un gros NAS.
"""
root = settings.downloads_dir
direct = root / name
if direct.is_file():
return str(direct)
try:
for entry in root.iterdir():
candidate = entry / name
if candidate.is_file():
return str(candidate)
except OSError:
return None
return None
async def _refresh_stale_qb_content_paths(db: DbSession) -> int:
"""Rattrape les chemins de telechargement des traites (qb_content_path).
Apres un changement de montage (ex: passage au volume unique), le chemin
capture a la validation ne correspond plus au referentiel actuel: la
fiche perd le tag « ISO a supprimer » et la suppression refuse le
chemin. Le torrent qBittorrent s'il existe encore, sinon une recherche
bornee sous la racine des telechargements, redonne le bon chemin.
Best effort: un chemin introuvable reste en l'etat.
"""
items = list(
db.scalars(
select(IsoMovie).where(
IsoMovie.status == "completed",
IsoMovie.qb_content_path.is_not(None),
)
)
)
stale = [
item for item in items if item.qb_content_path and not Path(item.qb_content_path).is_file()
]
if not stale:
return 0
torrents: list[dict] = []
qb = read_config().qbittorrent
url = build_base_url(qb.url, qb.port)
if url:
try:
client = QbittorrentClient(url, qb.username, qb.password, qb.api_key)
torrents = await client.torrents()
except Exception: # noqa: BLE001 - rattrapage best effort
torrents = []
refreshed = 0
for item in stale:
iso_name = PurePath(item.qb_content_path).name
new_path = None
match = match_torrent(torrents, item.qb_content_path)
if match is not None:
new_path = str(match.get("content_path") or "") or None
if new_path is None or not Path(new_path).is_file():
new_path = _find_downloaded_iso(iso_name)
if new_path and Path(new_path).is_file():
item.qb_content_path = new_path
refreshed += 1
return refreshed
def _linked_mkv(movie: IsoMovie) -> Path | None:
"""Retrouve le MKV lie dans le dossier du film (meme inode que la source)."""
paths = conversion_paths(movie)