:chore: Initialisation du projet
This commit is contained in:
1 parent
75a8c6addd
commit
6278d19618
27 files changed
+4418
-81
No files matched your search
+151
@@ -0,0 +1,151 @@
|
||||
import os
|
||||
import shutil
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import UTC, datetime
|
||||
from typing import Annotated
|
||||
|
||||
import httpx
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import Base, engine, get_db
|
||||
from app.models import IsoMovie
|
||||
from app.radarr import RadarrClient, movie_file_path
|
||||
from app.schemas import (
|
||||
ConnectionResult,
|
||||
HealthRead,
|
||||
IsoMovieRead,
|
||||
RadarrSettingsRead,
|
||||
RadarrSettingsWrite,
|
||||
ScanResult,
|
||||
)
|
||||
from app.settings_store import radarr_credentials, read_setting, write_setting
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="MKVTool", version="0.1.0", lifespan=lifespan)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
DbSession = Annotated[Session, Depends(get_db)]
|
||||
|
||||
|
||||
def client_from_db(db: Session) -> RadarrClient:
|
||||
try:
|
||||
url, api_key = radarr_credentials(db)
|
||||
except ValueError as error:
|
||||
raise HTTPException(status_code=409, detail=str(error)) from error
|
||||
return RadarrClient(url, api_key)
|
||||
|
||||
|
||||
@app.get("/api/health", response_model=HealthRead)
|
||||
def health() -> HealthRead:
|
||||
executable = shutil.which("makemkvcon")
|
||||
return HealthRead(
|
||||
status="ok",
|
||||
database="ok",
|
||||
makemkv_available=executable is not None,
|
||||
makemkv_version=os.getenv("MAKEMKV_VERSION") if executable else None,
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
def get_radarr_settings(db: DbSession) -> RadarrSettingsRead:
|
||||
return RadarrSettingsRead(
|
||||
url=read_setting(db, "radarr_url"),
|
||||
api_key_configured=read_setting(db, "radarr_api_key") is not None,
|
||||
)
|
||||
|
||||
|
||||
@app.put("/api/settings/radarr", response_model=RadarrSettingsRead)
|
||||
def put_radarr_settings(
|
||||
payload: RadarrSettingsWrite, db: DbSession
|
||||
) -> RadarrSettingsRead:
|
||||
url = str(payload.url).rstrip("/")
|
||||
write_setting(db, "radarr_url", url)
|
||||
if payload.api_key:
|
||||
write_setting(db, "radarr_api_key", payload.api_key)
|
||||
db.commit()
|
||||
return RadarrSettingsRead(
|
||||
url=url, api_key_configured=read_setting(db, "radarr_api_key") is not None
|
||||
)
|
||||
|
||||
|
||||
@app.post("/api/settings/radarr/test", response_model=ConnectionResult)
|
||||
async def test_radarr(db: DbSession) -> ConnectionResult:
|
||||
client = client_from_db(db)
|
||||
try:
|
||||
status = await client.system_status()
|
||||
except (httpx.HTTPError, ValueError) as error:
|
||||
raise HTTPException(
|
||||
status_code=502, detail=f"Connexion Radarr impossible: {error}"
|
||||
) from error
|
||||
version = str(status.get("version", "inconnue"))
|
||||
return ConnectionResult(success=True, version=version, message=f"Radarr {version} accessible")
|
||||
|
||||
|
||||
@app.get("/api/movies", response_model=list[IsoMovieRead])
|
||||
def list_movies(db: DbSession) -> list[IsoMovie]:
|
||||
return list(db.scalars(select(IsoMovie).order_by(IsoMovie.title)).all())
|
||||
|
||||
|
||||
@app.post("/api/scans", response_model=ScanResult)
|
||||
async def scan_radarr(db: DbSession) -> ScanResult:
|
||||
client = client_from_db(db)
|
||||
try:
|
||||
movies = await client.movies()
|
||||
except (httpx.HTTPError, ValueError) as error:
|
||||
raise HTTPException(status_code=502, detail=f"Scan Radarr impossible: {error}") from error
|
||||
|
||||
iso_files = created = updated = 0
|
||||
now = datetime.now(UTC)
|
||||
for movie in movies:
|
||||
movie_file = movie.get("movieFile") or {}
|
||||
path = movie_file_path(movie)
|
||||
if not path or not path.lower().endswith(".iso"):
|
||||
continue
|
||||
movie_id = movie.get("id")
|
||||
movie_file_id = movie_file.get("id")
|
||||
if not isinstance(movie_id, int) or not isinstance(movie_file_id, int):
|
||||
continue
|
||||
iso_files += 1
|
||||
item = db.scalar(
|
||||
select(IsoMovie).where(
|
||||
IsoMovie.radarr_movie_id == movie_id,
|
||||
IsoMovie.radarr_movie_file_id == movie_file_id,
|
||||
)
|
||||
)
|
||||
if item is None:
|
||||
item = IsoMovie(
|
||||
radarr_movie_id=movie_id,
|
||||
radarr_movie_file_id=movie_file_id,
|
||||
title=str(movie.get("title", "Film sans titre")),
|
||||
year=movie.get("year"),
|
||||
path=path,
|
||||
size=movie_file.get("size"),
|
||||
last_seen_at=now,
|
||||
)
|
||||
db.add(item)
|
||||
created += 1
|
||||
else:
|
||||
item.title = str(movie.get("title", item.title))
|
||||
item.year = movie.get("year")
|
||||
item.path = path
|
||||
item.size = movie_file.get("size")
|
||||
item.last_seen_at = now
|
||||
updated += 1
|
||||
db.commit()
|
||||
return ScanResult(
|
||||
scanned_movies=len(movies), iso_files=iso_files, created=created, updated=updated
|
||||
)
|
||||
Reference in new issue
Block a user