feat: Task 5 - Add NFO Management API Endpoints (85% complete)
- Create NFO API models (11 Pydantic models)
- Implement 8 REST API endpoints for NFO management
- Register NFO router in FastAPI app
- Create 18 comprehensive API tests
- Add detailed status documentation
Endpoints:
- GET /api/nfo/{id}/check - Check NFO/media status
- POST /api/nfo/{id}/create - Create NFO & media
- PUT /api/nfo/{id}/update - Update NFO
- GET /api/nfo/{id}/content - Get NFO content
- GET /api/nfo/{id}/media/status - Media status
- POST /api/nfo/{id}/media/download - Download media
- POST /api/nfo/batch/create - Batch operations
- GET /api/nfo/missing - List missing NFOs
Remaining: Refactor to use series_app dependency pattern
This commit is contained in:
686
src/server/api/nfo.py
Normal file
686
src/server/api/nfo.py
Normal file
@@ -0,0 +1,686 @@
|
||||
"""NFO Management API endpoints.
|
||||
|
||||
This module provides REST API endpoints for managing tvshow.nfo files
|
||||
and associated media (poster, logo, fanart).
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from src.config.settings import settings
|
||||
from src.core.entities.series import Serie
|
||||
from src.core.SeriesApp import SeriesApp
|
||||
from src.core.services.nfo_service import NFOService
|
||||
from src.core.services.tmdb_client import TMDBAPIError
|
||||
from src.server.models.nfo import (
|
||||
MediaDownloadRequest,
|
||||
MediaFilesStatus,
|
||||
NFOBatchCreateRequest,
|
||||
NFOBatchCreateResponse,
|
||||
NFOBatchResult,
|
||||
NFOCheckResponse,
|
||||
NFOContentResponse,
|
||||
NFOCreateRequest,
|
||||
NFOCreateResponse,
|
||||
NFOMissingResponse,
|
||||
NFOMissingSeries,
|
||||
)
|
||||
from src.server.utils.dependencies import (
|
||||
get_series_app,
|
||||
require_auth,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/nfo", tags=["nfo"])
|
||||
|
||||
|
||||
async def get_nfo_service() -> NFOService:
|
||||
"""Get NFO service dependency.
|
||||
|
||||
Returns:
|
||||
NFOService instance
|
||||
|
||||
Raises:
|
||||
HTTPException: If NFO service not configured
|
||||
"""
|
||||
if not settings.tmdb_api_key:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="NFO service not configured. TMDB API key required."
|
||||
)
|
||||
|
||||
return NFOService(
|
||||
tmdb_api_key=settings.tmdb_api_key,
|
||||
anime_directory=settings.anime_directory,
|
||||
image_size=settings.nfo_image_size,
|
||||
auto_create=settings.nfo_auto_create
|
||||
)
|
||||
|
||||
|
||||
def check_media_files(serie_folder: str) -> MediaFilesStatus:
|
||||
"""Check status of media files for a series.
|
||||
|
||||
Args:
|
||||
serie_folder: Series folder name
|
||||
|
||||
Returns:
|
||||
MediaFilesStatus with file existence info
|
||||
"""
|
||||
folder_path = Path(settings.anime_directory) / serie_folder
|
||||
|
||||
poster_path = folder_path / "poster.jpg"
|
||||
logo_path = folder_path / "logo.png"
|
||||
fanart_path = folder_path / "fanart.jpg"
|
||||
|
||||
return MediaFilesStatus(
|
||||
has_poster=poster_path.exists(),
|
||||
has_logo=logo_path.exists(),
|
||||
has_fanart=fanart_path.exists(),
|
||||
poster_path=str(poster_path) if poster_path.exists() else None,
|
||||
logo_path=str(logo_path) if logo_path.exists() else None,
|
||||
fanart_path=str(fanart_path) if fanart_path.exists() else None
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{serie_id}/check", response_model=NFOCheckResponse)
|
||||
async def check_nfo(
|
||||
serie_id: str,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOCheckResponse:
|
||||
"""Check if NFO and media files exist for a series.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOCheckResponse with NFO and media status
|
||||
|
||||
Raises:
|
||||
HTTPException: If series not found
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check NFO
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
nfo_path = None
|
||||
if has_nfo:
|
||||
nfo_path = str(
|
||||
Path(settings.anime_directory) / serie_folder / "tvshow.nfo"
|
||||
)
|
||||
|
||||
# Check media files
|
||||
media_files = check_media_files(serie_folder)
|
||||
|
||||
return NFOCheckResponse(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
has_nfo=has_nfo,
|
||||
nfo_path=nfo_path,
|
||||
media_files=media_files
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error checking NFO for {serie_id}: {e}", exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to check NFO: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.post("/{serie_id}/create", response_model=NFOCreateResponse)
|
||||
async def create_nfo(
|
||||
serie_id: str,
|
||||
request: NFOCreateRequest,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOCreateResponse:
|
||||
"""Create NFO file and download media for a series.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
request: NFO creation options
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOCreateResponse with creation result
|
||||
|
||||
Raises:
|
||||
HTTPException: If series not found or creation fails
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check if NFO already exists
|
||||
if not request.overwrite_existing:
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
if has_nfo:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_409_CONFLICT,
|
||||
detail="NFO already exists. Use overwrite_existing=true"
|
||||
)
|
||||
|
||||
# Create NFO
|
||||
serie_name = request.serie_name or serie.name or serie_folder
|
||||
nfo_path = await nfo_service.create_tvshow_nfo(
|
||||
serie_name=serie_name,
|
||||
serie_folder=serie_folder,
|
||||
year=request.year,
|
||||
download_poster=request.download_poster,
|
||||
download_logo=request.download_logo,
|
||||
download_fanart=request.download_fanart
|
||||
)
|
||||
|
||||
# Check media files
|
||||
media_files = check_media_files(serie_folder)
|
||||
|
||||
return NFOCreateResponse(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
nfo_path=str(nfo_path),
|
||||
media_files=media_files,
|
||||
message="NFO and media files created successfully"
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except TMDBAPIError as e:
|
||||
logger.warning(f"TMDB API error creating NFO for {serie_id}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=f"TMDB API error: {str(e)}"
|
||||
) from e
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error creating NFO for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to create NFO: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.put("/{serie_id}/update", response_model=NFOCreateResponse)
|
||||
async def update_nfo(
|
||||
serie_id: str,
|
||||
download_media: bool = True,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOCreateResponse:
|
||||
"""Update existing NFO file with fresh TMDB data.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
download_media: Whether to re-download media files
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOCreateResponse with update result
|
||||
|
||||
Raises:
|
||||
HTTPException: If series or NFO not found
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check if NFO exists
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
if not has_nfo:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="NFO file not found. Use create endpoint instead."
|
||||
)
|
||||
|
||||
# Update NFO
|
||||
nfo_path = await nfo_service.update_tvshow_nfo(
|
||||
serie_folder=serie_folder,
|
||||
download_media=download_media
|
||||
)
|
||||
|
||||
# Check media files
|
||||
media_files = check_media_files(serie_folder)
|
||||
|
||||
return NFOCreateResponse(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
nfo_path=str(nfo_path),
|
||||
media_files=media_files,
|
||||
message="NFO updated successfully"
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except TMDBAPIError as e:
|
||||
logger.warning(f"TMDB API error updating NFO for {serie_id}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=f"TMDB API error: {str(e)}"
|
||||
) from e
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error updating NFO for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to update NFO: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("/{serie_id}/content", response_model=NFOContentResponse)
|
||||
async def get_nfo_content(
|
||||
serie_id: str,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOContentResponse:
|
||||
"""Get NFO file content for a series.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOContentResponse with NFO content
|
||||
|
||||
Raises:
|
||||
HTTPException: If series or NFO not found
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check if NFO exists
|
||||
nfo_path = (
|
||||
Path(settings.anime_directory) / serie_folder / "tvshow.nfo"
|
||||
)
|
||||
if not nfo_path.exists():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="NFO file not found"
|
||||
)
|
||||
|
||||
# Read NFO content
|
||||
content = nfo_path.read_text(encoding="utf-8")
|
||||
file_size = nfo_path.stat().st_size
|
||||
last_modified = datetime.fromtimestamp(nfo_path.stat().st_mtime)
|
||||
|
||||
return NFOContentResponse(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
content=content,
|
||||
file_size=file_size,
|
||||
last_modified=last_modified
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error reading NFO content for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to read NFO content: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.get("/{serie_id}/media/status", response_model=MediaFilesStatus)
|
||||
async def get_media_status(
|
||||
serie_id: str,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service)
|
||||
) -> MediaFilesStatus:
|
||||
"""Get media files status for a series.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
|
||||
Returns:
|
||||
MediaFilesStatus with file existence info
|
||||
|
||||
Raises:
|
||||
HTTPException: If series not found
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
return check_media_files(serie.folder)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error checking media status for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to check media status: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.post("/{serie_id}/media/download", response_model=MediaFilesStatus)
|
||||
async def download_media(
|
||||
serie_id: str,
|
||||
request: MediaDownloadRequest,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> MediaFilesStatus:
|
||||
"""Download missing media files for a series.
|
||||
|
||||
Args:
|
||||
serie_id: Series identifier
|
||||
request: Media download options
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
MediaFilesStatus after download attempt
|
||||
|
||||
Raises:
|
||||
HTTPException: If series or NFO not found
|
||||
"""
|
||||
try:
|
||||
# Get series info
|
||||
series_list = anime_service.get_series_list()
|
||||
serie = next(
|
||||
(s for s in series_list if getattr(s, 'key', None) == serie_id),
|
||||
None
|
||||
)
|
||||
|
||||
if not serie:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"Series not found: {serie_id}"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check if NFO exists (needed for TMDB ID)
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
if not has_nfo:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="NFO required for media download. Create NFO first."
|
||||
)
|
||||
|
||||
# For now, update NFO which will re-download media
|
||||
# In future, could add standalone media download
|
||||
if (request.download_poster or request.download_logo
|
||||
or request.download_fanart):
|
||||
await nfo_service.update_tvshow_nfo(
|
||||
serie_folder=serie_folder,
|
||||
download_media=True
|
||||
)
|
||||
|
||||
return check_media_files(serie_folder)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error downloading media for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to download media: {str(e)}"
|
||||
) from e
|
||||
|
||||
|
||||
@router.post("/batch/create", response_model=NFOBatchCreateResponse)
|
||||
async def batch_create_nfo(
|
||||
request: NFOBatchCreateRequest,
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOBatchCreateResponse:
|
||||
"""Batch create NFO files for multiple series.
|
||||
|
||||
Args:
|
||||
request: Batch creation options
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOBatchCreateResponse with results
|
||||
"""
|
||||
results: List[NFOBatchResult] = []
|
||||
successful = 0
|
||||
failed = 0
|
||||
skipped = 0
|
||||
|
||||
# Get all series
|
||||
series_list = anime_service.get_series_list()
|
||||
series_map = {
|
||||
getattr(s, 'key', None): s
|
||||
for s in series_list
|
||||
if getattr(s, 'key', None)
|
||||
}
|
||||
|
||||
# Process each series
|
||||
semaphore = asyncio.Semaphore(request.max_concurrent)
|
||||
|
||||
async def process_serie(serie_id: str) -> NFOBatchResult:
|
||||
"""Process a single series."""
|
||||
async with semaphore:
|
||||
try:
|
||||
serie = series_map.get(serie_id)
|
||||
if not serie:
|
||||
return NFOBatchResult(
|
||||
serie_id=serie_id,
|
||||
serie_folder="",
|
||||
success=False,
|
||||
message="Series not found"
|
||||
)
|
||||
|
||||
serie_folder = serie.folder
|
||||
|
||||
# Check if NFO exists
|
||||
if request.skip_existing:
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
if has_nfo:
|
||||
return NFOBatchResult(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
success=False,
|
||||
message="Skipped - NFO already exists"
|
||||
)
|
||||
|
||||
# Create NFO
|
||||
nfo_path = await nfo_service.create_tvshow_nfo(
|
||||
serie_name=serie.name or serie_folder,
|
||||
serie_folder=serie_folder,
|
||||
download_poster=request.download_media,
|
||||
download_logo=request.download_media,
|
||||
download_fanart=request.download_media
|
||||
)
|
||||
|
||||
return NFOBatchResult(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
success=True,
|
||||
message="NFO created successfully",
|
||||
nfo_path=str(nfo_path)
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error creating NFO for {serie_id}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
return NFOBatchResult(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie.folder if serie else "",
|
||||
success=False,
|
||||
message=f"Error: {str(e)}"
|
||||
)
|
||||
|
||||
# Process all series concurrently
|
||||
tasks = [process_serie(sid) for sid in request.serie_ids]
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
# Count results
|
||||
for result in results:
|
||||
if result.success:
|
||||
successful += 1
|
||||
elif "Skipped" in result.message:
|
||||
skipped += 1
|
||||
else:
|
||||
failed += 1
|
||||
|
||||
return NFOBatchCreateResponse(
|
||||
total=len(request.serie_ids),
|
||||
successful=successful,
|
||||
failed=failed,
|
||||
skipped=skipped,
|
||||
results=results
|
||||
)
|
||||
|
||||
|
||||
@router.get("/missing", response_model=NFOMissingResponse)
|
||||
async def get_missing_nfo(
|
||||
_auth: dict = Depends(require_auth),
|
||||
anime_service: AnimeService = Depends(get_anime_service),
|
||||
nfo_service: NFOService = Depends(get_nfo_service)
|
||||
) -> NFOMissingResponse:
|
||||
"""Get list of series without NFO files.
|
||||
|
||||
Args:
|
||||
_auth: Authentication dependency
|
||||
anime_service: Anime service dependency
|
||||
nfo_service: NFO service dependency
|
||||
|
||||
Returns:
|
||||
NFOMissingResponse with series list
|
||||
"""
|
||||
try:
|
||||
series_list = anime_service.get_series_list()
|
||||
missing_series: List[NFOMissingSeries] = []
|
||||
|
||||
for serie in series_list:
|
||||
serie_id = getattr(serie, 'key', None)
|
||||
if not serie_id:
|
||||
continue
|
||||
|
||||
serie_folder = serie.folder
|
||||
has_nfo = await nfo_service.check_nfo_exists(serie_folder)
|
||||
|
||||
if not has_nfo:
|
||||
media_files = check_media_files(serie_folder)
|
||||
has_media = (
|
||||
media_files.has_poster
|
||||
or media_files.has_logo
|
||||
or media_files.has_fanart
|
||||
)
|
||||
|
||||
missing_series.append(NFOMissingSeries(
|
||||
serie_id=serie_id,
|
||||
serie_folder=serie_folder,
|
||||
serie_name=serie.name or serie_folder,
|
||||
has_media=has_media,
|
||||
media_files=media_files
|
||||
))
|
||||
|
||||
return NFOMissingResponse(
|
||||
total_series=len(series_list),
|
||||
missing_nfo_count=len(missing_series),
|
||||
series=missing_series
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting missing NFOs: {e}", exc_info=True)
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to get missing NFOs: {str(e)}"
|
||||
) from e
|
||||
@@ -23,6 +23,7 @@ from src.server.api.auth import router as auth_router
|
||||
from src.server.api.config import router as config_router
|
||||
from src.server.api.download import router as download_router
|
||||
from src.server.api.health import router as health_router
|
||||
from src.server.api.nfo import router as nfo_router
|
||||
from src.server.api.scheduler import router as scheduler_router
|
||||
from src.server.api.websocket import router as websocket_router
|
||||
from src.server.controllers.error_controller import (
|
||||
@@ -282,6 +283,7 @@ app.include_router(config_router)
|
||||
app.include_router(scheduler_router)
|
||||
app.include_router(anime_router)
|
||||
app.include_router(download_router)
|
||||
app.include_router(nfo_router)
|
||||
app.include_router(websocket_router)
|
||||
|
||||
# Register exception handlers
|
||||
|
||||
357
src/server/models/nfo.py
Normal file
357
src/server/models/nfo.py
Normal file
@@ -0,0 +1,357 @@
|
||||
"""NFO API request and response models.
|
||||
|
||||
This module defines Pydantic models for NFO management API operations.
|
||||
"""
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class MediaFilesStatus(BaseModel):
|
||||
"""Status of media files (poster, logo, fanart) for a series.
|
||||
|
||||
Attributes:
|
||||
has_poster: Whether poster.jpg exists
|
||||
has_logo: Whether logo.png exists
|
||||
has_fanart: Whether fanart.jpg exists
|
||||
poster_path: Path to poster file if exists
|
||||
logo_path: Path to logo file if exists
|
||||
fanart_path: Path to fanart file if exists
|
||||
"""
|
||||
has_poster: bool = Field(
|
||||
default=False,
|
||||
description="Whether poster.jpg exists"
|
||||
)
|
||||
has_logo: bool = Field(
|
||||
default=False,
|
||||
description="Whether logo.png exists"
|
||||
)
|
||||
has_fanart: bool = Field(
|
||||
default=False,
|
||||
description="Whether fanart.jpg exists"
|
||||
)
|
||||
poster_path: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to poster file if exists"
|
||||
)
|
||||
logo_path: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to logo file if exists"
|
||||
)
|
||||
fanart_path: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to fanart file if exists"
|
||||
)
|
||||
|
||||
|
||||
class NFOCheckResponse(BaseModel):
|
||||
"""Response for NFO existence check.
|
||||
|
||||
Attributes:
|
||||
serie_id: Series identifier
|
||||
serie_folder: Series folder name
|
||||
has_nfo: Whether tvshow.nfo exists
|
||||
nfo_path: Path to NFO file if exists
|
||||
media_files: Status of media files
|
||||
"""
|
||||
serie_id: str = Field(
|
||||
...,
|
||||
description="Series identifier"
|
||||
)
|
||||
serie_folder: str = Field(
|
||||
...,
|
||||
description="Series folder name"
|
||||
)
|
||||
has_nfo: bool = Field(
|
||||
...,
|
||||
description="Whether tvshow.nfo exists"
|
||||
)
|
||||
nfo_path: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to NFO file if exists"
|
||||
)
|
||||
media_files: MediaFilesStatus = Field(
|
||||
...,
|
||||
description="Status of media files"
|
||||
)
|
||||
|
||||
|
||||
class NFOCreateRequest(BaseModel):
|
||||
"""Request to create NFO file.
|
||||
|
||||
Attributes:
|
||||
serie_name: Name to search in TMDB
|
||||
year: Optional year to narrow search
|
||||
download_poster: Whether to download poster.jpg
|
||||
download_logo: Whether to download logo.png
|
||||
download_fanart: Whether to download fanart.jpg
|
||||
overwrite_existing: Whether to overwrite existing NFO
|
||||
"""
|
||||
serie_name: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Name to search in TMDB (defaults to folder name)"
|
||||
)
|
||||
year: Optional[int] = Field(
|
||||
default=None,
|
||||
description="Optional year to narrow search"
|
||||
)
|
||||
download_poster: bool = Field(
|
||||
default=True,
|
||||
description="Whether to download poster.jpg"
|
||||
)
|
||||
download_logo: bool = Field(
|
||||
default=True,
|
||||
description="Whether to download logo.png"
|
||||
)
|
||||
download_fanart: bool = Field(
|
||||
default=True,
|
||||
description="Whether to download fanart.jpg"
|
||||
)
|
||||
overwrite_existing: bool = Field(
|
||||
default=False,
|
||||
description="Whether to overwrite existing NFO"
|
||||
)
|
||||
|
||||
|
||||
class NFOCreateResponse(BaseModel):
|
||||
"""Response after NFO creation.
|
||||
|
||||
Attributes:
|
||||
serie_id: Series identifier
|
||||
serie_folder: Series folder name
|
||||
nfo_path: Path to created NFO file
|
||||
media_files: Status of downloaded media files
|
||||
tmdb_id: TMDB ID of matched series
|
||||
message: Success message
|
||||
"""
|
||||
serie_id: str = Field(
|
||||
...,
|
||||
description="Series identifier"
|
||||
)
|
||||
serie_folder: str = Field(
|
||||
...,
|
||||
description="Series folder name"
|
||||
)
|
||||
nfo_path: str = Field(
|
||||
...,
|
||||
description="Path to created NFO file"
|
||||
)
|
||||
media_files: MediaFilesStatus = Field(
|
||||
...,
|
||||
description="Status of downloaded media files"
|
||||
)
|
||||
tmdb_id: Optional[int] = Field(
|
||||
default=None,
|
||||
description="TMDB ID of matched series"
|
||||
)
|
||||
message: str = Field(
|
||||
...,
|
||||
description="Success message"
|
||||
)
|
||||
|
||||
|
||||
class NFOContentResponse(BaseModel):
|
||||
"""Response containing NFO XML content.
|
||||
|
||||
Attributes:
|
||||
serie_id: Series identifier
|
||||
serie_folder: Series folder name
|
||||
content: NFO XML content
|
||||
file_size: Size of NFO file in bytes
|
||||
last_modified: Last modification timestamp
|
||||
"""
|
||||
serie_id: str = Field(
|
||||
...,
|
||||
description="Series identifier"
|
||||
)
|
||||
serie_folder: str = Field(
|
||||
...,
|
||||
description="Series folder name"
|
||||
)
|
||||
content: str = Field(
|
||||
...,
|
||||
description="NFO XML content"
|
||||
)
|
||||
file_size: int = Field(
|
||||
...,
|
||||
description="Size of NFO file in bytes"
|
||||
)
|
||||
last_modified: Optional[datetime] = Field(
|
||||
default=None,
|
||||
description="Last modification timestamp"
|
||||
)
|
||||
|
||||
|
||||
class MediaDownloadRequest(BaseModel):
|
||||
"""Request to download specific media files.
|
||||
|
||||
Attributes:
|
||||
download_poster: Whether to download poster.jpg
|
||||
download_logo: Whether to download logo.png
|
||||
download_fanart: Whether to download fanart.jpg
|
||||
overwrite_existing: Whether to overwrite existing files
|
||||
"""
|
||||
download_poster: bool = Field(
|
||||
default=False,
|
||||
description="Whether to download poster.jpg"
|
||||
)
|
||||
download_logo: bool = Field(
|
||||
default=False,
|
||||
description="Whether to download logo.png"
|
||||
)
|
||||
download_fanart: bool = Field(
|
||||
default=False,
|
||||
description="Whether to download fanart.jpg"
|
||||
)
|
||||
overwrite_existing: bool = Field(
|
||||
default=False,
|
||||
description="Whether to overwrite existing files"
|
||||
)
|
||||
|
||||
|
||||
class NFOBatchCreateRequest(BaseModel):
|
||||
"""Request to batch create NFOs for multiple series.
|
||||
|
||||
Attributes:
|
||||
serie_ids: List of series IDs to process
|
||||
download_media: Whether to download media files
|
||||
skip_existing: Whether to skip series with existing NFOs
|
||||
max_concurrent: Maximum concurrent creations
|
||||
"""
|
||||
serie_ids: List[str] = Field(
|
||||
...,
|
||||
description="List of series IDs to process"
|
||||
)
|
||||
download_media: bool = Field(
|
||||
default=True,
|
||||
description="Whether to download media files"
|
||||
)
|
||||
skip_existing: bool = Field(
|
||||
default=True,
|
||||
description="Whether to skip series with existing NFOs"
|
||||
)
|
||||
max_concurrent: int = Field(
|
||||
default=3,
|
||||
ge=1,
|
||||
le=10,
|
||||
description="Maximum concurrent creations (1-10)"
|
||||
)
|
||||
|
||||
|
||||
class NFOBatchResult(BaseModel):
|
||||
"""Result for a single series in batch operation.
|
||||
|
||||
Attributes:
|
||||
serie_id: Series identifier
|
||||
serie_folder: Series folder name
|
||||
success: Whether operation succeeded
|
||||
message: Success or error message
|
||||
nfo_path: Path to NFO file if successful
|
||||
"""
|
||||
serie_id: str = Field(
|
||||
...,
|
||||
description="Series identifier"
|
||||
)
|
||||
serie_folder: str = Field(
|
||||
...,
|
||||
description="Series folder name"
|
||||
)
|
||||
success: bool = Field(
|
||||
...,
|
||||
description="Whether operation succeeded"
|
||||
)
|
||||
message: str = Field(
|
||||
...,
|
||||
description="Success or error message"
|
||||
)
|
||||
nfo_path: Optional[str] = Field(
|
||||
default=None,
|
||||
description="Path to NFO file if successful"
|
||||
)
|
||||
|
||||
|
||||
class NFOBatchCreateResponse(BaseModel):
|
||||
"""Response after batch NFO creation.
|
||||
|
||||
Attributes:
|
||||
total: Total number of series processed
|
||||
successful: Number of successful creations
|
||||
failed: Number of failed creations
|
||||
skipped: Number of skipped series
|
||||
results: Detailed results for each series
|
||||
"""
|
||||
total: int = Field(
|
||||
...,
|
||||
description="Total number of series processed"
|
||||
)
|
||||
successful: int = Field(
|
||||
...,
|
||||
description="Number of successful creations"
|
||||
)
|
||||
failed: int = Field(
|
||||
...,
|
||||
description="Number of failed creations"
|
||||
)
|
||||
skipped: int = Field(
|
||||
...,
|
||||
description="Number of skipped series"
|
||||
)
|
||||
results: List[NFOBatchResult] = Field(
|
||||
...,
|
||||
description="Detailed results for each series"
|
||||
)
|
||||
|
||||
|
||||
class NFOMissingSeries(BaseModel):
|
||||
"""Information about a series missing NFO.
|
||||
|
||||
Attributes:
|
||||
serie_id: Series identifier
|
||||
serie_folder: Series folder name
|
||||
serie_name: Display name
|
||||
has_media: Whether any media files exist
|
||||
media_files: Status of media files
|
||||
"""
|
||||
serie_id: str = Field(
|
||||
...,
|
||||
description="Series identifier"
|
||||
)
|
||||
serie_folder: str = Field(
|
||||
...,
|
||||
description="Series folder name"
|
||||
)
|
||||
serie_name: str = Field(
|
||||
...,
|
||||
description="Display name"
|
||||
)
|
||||
has_media: bool = Field(
|
||||
default=False,
|
||||
description="Whether any media files exist"
|
||||
)
|
||||
media_files: MediaFilesStatus = Field(
|
||||
...,
|
||||
description="Status of media files"
|
||||
)
|
||||
|
||||
|
||||
class NFOMissingResponse(BaseModel):
|
||||
"""Response listing series without NFOs.
|
||||
|
||||
Attributes:
|
||||
total_series: Total number of series in library
|
||||
missing_nfo_count: Number of series without NFO
|
||||
series: List of series missing NFO
|
||||
"""
|
||||
total_series: int = Field(
|
||||
...,
|
||||
description="Total number of series in library"
|
||||
)
|
||||
missing_nfo_count: int = Field(
|
||||
...,
|
||||
description="Number of series without NFO"
|
||||
)
|
||||
series: List[NFOMissingSeries] = Field(
|
||||
...,
|
||||
description="List of series missing NFO"
|
||||
)
|
||||
Reference in New Issue
Block a user