feat: add perform_nfo_repair_scan startup hook
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Centralized initialization service for application startup and setup."""
|
||||
from typing import Callable
|
||||
from pathlib import Path
|
||||
from typing import Callable, Optional
|
||||
|
||||
import structlog
|
||||
|
||||
@@ -375,6 +376,66 @@ async def perform_nfo_scan_if_needed(progress_service=None):
|
||||
)
|
||||
|
||||
|
||||
async def perform_nfo_repair_scan(background_loader=None) -> None:
|
||||
"""Scan all series folders and repair incomplete tvshow.nfo files.
|
||||
|
||||
Runs on every application startup (not guarded by a run-once DB flag).
|
||||
Checks each subfolder of ``settings.anime_directory`` for a ``tvshow.nfo``
|
||||
and queues a repair via *background_loader* when required tags are absent
|
||||
or empty, or runs repairs inline when no loader is provided.
|
||||
|
||||
Args:
|
||||
background_loader: Optional BackgroundLoaderService. When provided,
|
||||
deficient series are queued non-blocking. When None, repairs
|
||||
execute inline (useful in tests).
|
||||
"""
|
||||
from src.core.services.nfo_factory import NFOServiceFactory
|
||||
from src.core.services.nfo_repair_service import NfoRepairService, nfo_needs_repair
|
||||
if not settings.tmdb_api_key:
|
||||
logger.warning("NFO repair scan skipped — TMDB API key not configured")
|
||||
return
|
||||
if not settings.anime_directory:
|
||||
logger.warning("NFO repair scan skipped — anime directory not configured")
|
||||
return
|
||||
anime_dir = Path(settings.anime_directory)
|
||||
if not anime_dir.is_dir():
|
||||
logger.warning("NFO repair scan skipped — anime directory not found: %s", anime_dir)
|
||||
return
|
||||
try:
|
||||
factory = NFOServiceFactory()
|
||||
nfo_service = factory.create()
|
||||
except ValueError as exc:
|
||||
logger.warning("NFO repair scan skipped — cannot create NFOService: %s", exc)
|
||||
return
|
||||
repair_service = NfoRepairService(nfo_service)
|
||||
queued = 0
|
||||
total = 0
|
||||
for series_dir in sorted(anime_dir.iterdir()):
|
||||
if not series_dir.is_dir():
|
||||
continue
|
||||
nfo_path = series_dir / "tvshow.nfo"
|
||||
if not nfo_path.exists():
|
||||
continue
|
||||
total += 1
|
||||
series_name = series_dir.name
|
||||
if nfo_needs_repair(nfo_path):
|
||||
queued += 1
|
||||
if background_loader is not None:
|
||||
await background_loader.add_series_loading_task(
|
||||
key=series_name,
|
||||
folder=series_name,
|
||||
name=series_name,
|
||||
)
|
||||
else:
|
||||
await repair_service.repair_series(series_dir, series_name)
|
||||
|
||||
logger.info(
|
||||
"NFO repair scan complete: %d of %d series queued for repair",
|
||||
queued,
|
||||
total,
|
||||
)
|
||||
|
||||
|
||||
async def _check_media_scan_status() -> bool:
|
||||
"""Check if initial media scan has been completed.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user