diff --git a/anime/__main__.py b/anime/__main__.py index c86f2e5..a86fc03 100644 --- a/anime/__main__.py +++ b/anime/__main__.py @@ -1,10 +1,11 @@ from pathlib import Path from typing import Optional -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException, Request, Response from fastapi.staticfiles import StaticFiles from typer import Typer, Argument import uvicorn from anime.routes import router +from starlette.middleware.base import BaseHTTPMiddleware CURRENT_DIR = Path(__file__).parent STATIC_DIR = CURRENT_DIR / "static" @@ -12,6 +13,20 @@ STATIC_DIR = CURRENT_DIR / "static" cli = Typer() +async def response_formatter(request: Request, call_next): + try: + response = await call_next(request) + print(response) + except Exception as e: + response = Response( + status_code=500, + content={ + "detail": str(e), + }, + ) + return response + + @cli.command() def run_app( host: str = "0.0.0.0", @@ -33,7 +48,9 @@ def run_app( check_dir=False, ), ) + app.add_middleware(BaseHTTPMiddleware, dispatch=response_formatter) app.state.anime_dir = anime_dir + app.state.pid = None uvicorn.run(app, host=host, port=port, workers=1) diff --git a/anime/routes.py b/anime/routes.py index 808cd57..7c2a418 100644 --- a/anime/routes.py +++ b/anime/routes.py @@ -1,5 +1,7 @@ import os from pathlib import Path +import shutil +import subprocess from fastapi import APIRouter, HTTPException, Request from anime.dtos import KillRequest, PlayerOffsetRequest @@ -25,17 +27,54 @@ def start_watching(request: Request) -> None: anime_dir = request.app.state.anime_dir if not anime_dir: raise HTTPException(status_code=400, detail="Anime directory is not set.") - os.chdir(anime_dir) - os.system("awatch") - os.chdir(CWD) + if request.app.state.pid: + try: + os.kill(request.app.state.pid, 0) + except OSError: + pass + else: + raise HTTPException( + status_code=400, + detail="Awatch is already running.", + ) + awatch = shutil.which("awatch") + if awatch is None: + raise Exception( + "awatch command is not available. Please install awatch.\n" + "https://gitlab.le-memese.com/s3rius/awatch/" + ) + ret = subprocess.Popen( + [awatch], + cwd=anime_dir, + ) + request.app.state.pid = ret.pid + ret.wait() + request.app.state.pid = None @router.post("/player/offset") async def offset(req: PlayerOffsetRequest) -> None: direction = "+" if req.forward else "-" - os.system(f"playerctl position {req.offset}{direction}") + playerctl = shutil.which("playerctl") + if playerctl is None: + raise HTTPException( + status_code=500, + detail="playerctl command is not available.", + ) + + subprocess.run( + [playerctl, "position", f"{req.offset}{direction}"], + check=False, + ) @router.post("/player/play-pause") async def play_pause() -> None: - os.system("playerctl play-pause") + playerctl = shutil.which("playerctl") + if playerctl is None: + raise HTTPException( + status_code=500, + detail="playerctl command is not available.", + ) + + subprocess.run([playerctl, "play-pause"], check=False) diff --git a/build.py b/build.py index ff330ed..7273c4c 100644 --- a/build.py +++ b/build.py @@ -1,6 +1,7 @@ import os from pathlib import Path -from shutil import copytree, rmtree +import shutil +import subprocess CURRENT_DIR = Path(__file__).parent @@ -29,15 +30,19 @@ def build(setup_kwargs): This script is useful for packaging the application. """ print("Starting building frontend ...") + pnpm_path = shutil.which("pnpm") + if pnpm_path is None: + raise Exception("pnpm command is not available. PLease install pnpm.") - with DirChanger(CURRENT_DIR / "frontend"): - ret_status = os.system("pnpm build") - if ret_status != 0: - raise Exception("Frontend build failed.") + subprocess.run( + [pnpm_path, "build"], + cwd=CURRENT_DIR / "frontend", + check=True, + ) print("Frontend build finished.") - rmtree(STATIC_OUTPUT_DIR, ignore_errors=True) - copytree(symlinks=True, src=DIST_DIR, dst=STATIC_OUTPUT_DIR) + shutil.rmtree(STATIC_OUTPUT_DIR, ignore_errors=True) + shutil.copytree(symlinks=True, src=DIST_DIR, dst=STATIC_OUTPUT_DIR) return setup_kwargs diff --git a/frontend/.gitignore b/frontend/.gitignore index 8ee54e8..0e2eaa7 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -11,6 +11,7 @@ node_modules .DS_Store dist dist-ssr +dev-dist coverage *.local diff --git a/frontend/index.html b/frontend/index.html index 99f583a..9818b67 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1,13 +1,18 @@ -
- - - -