47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
|
|
from anime import dtos
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
def is_pid_alive(pid: int) -> bool:
|
|
if pid:
|
|
try:
|
|
os.kill(pid, 0)
|
|
except OSError:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
@router.get("/can-start-watching")
|
|
def can_start_watching(request: Request) -> None:
|
|
if not request.app.state.anime_dir:
|
|
raise HTTPException(status_code=400, detail="Anime directory is not set")
|
|
|
|
|
|
@router.post("/kill")
|
|
def kill(input_dto: dtos.KillRequest) -> None:
|
|
for name in input_dto.names:
|
|
os.system(f"killall {name}")
|
|
|
|
|
|
@router.post("/start-watching")
|
|
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")
|
|
awatch = shutil.which("awatch")
|
|
if awatch is None:
|
|
raise Exception("awatch command is not available")
|
|
ret = subprocess.Popen(
|
|
[awatch],
|
|
cwd=anime_dir,
|
|
)
|
|
request.app.state.pid = ret.pid
|