diff --git a/requirements.txt b/requirements.txt index 4ad4884..e6eab76 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -starlette==0.14.2 -fastapi==0.68.1 +fastapi==0.70.0 +giteapy==1.0.8 +starlette==0.16.0 uvicorn==0.15.0 -gunicorn==20.1.0 \ No newline at end of file diff --git a/routers/gitea.py b/routers/gitea.py index 20a80a1..4caed8e 100644 --- a/routers/gitea.py +++ b/routers/gitea.py @@ -5,7 +5,6 @@ import json from pathlib import Path from fastapi import APIRouter, Header, Query, HTTPException -from gitea.gitea import Repository ROOT_PATH = Path.home().joinpath(".radical_api/gitea") diff --git a/routers/mc_optimizer.py b/routers/mc_optimizer.py new file mode 100644 index 0000000..4eccf78 --- /dev/null +++ b/routers/mc_optimizer.py @@ -0,0 +1,56 @@ +from __future__ import annotations + +from fastapi import APIRouter, HTTPException, Query +from fastapi.responses import StreamingResponse +from pathlib import Path +from datetime import datetime +from zipfile import ZipFile +import json + +from starlette.responses import FileResponse + + +router = APIRouter(prefix="/mc_optimized") +ROOT_PATH = Path.home().joinpath(".radical_api/mc_optimized") +ROOT_PATH.mkdir(exist_ok=True) + +class PackFile: + def __init__(self, file: Path) -> None: + self.file = file + self.datetime = self.set_datetime() + self.build = self.set_build() + + def set_datetime(self): + zf = ZipFile(self.file) + manifest = json.loads(zf.read("manifest.json").decode("utf-8")) + return datetime.strptime(manifest['build-date'], "%d-%m-%Y-%H%M%S") + + def set_build(self): + zf = ZipFile(self.file) + manifest = json.loads(zf.read("manifest.json").decode("utf-8")) + return manifest['build'] + + def __lt__(self, other: PackFile): + return self.datetime.__lt__(other.datetime) + +def iterfile(path: Path): + with path.open('rb') as fp: + yield from fp + +@router.get("/latest") +def latest_pack(): + files = [] + for file in ROOT_PATH.iterdir(): + files.append(PackFile(file)) + files.sort() + if len(files) == 0: + raise HTTPException(404, "No files found.") + return FileResponse(files[-1].file, filename=f"mc-optimizer-pack-{files[-1].build}.zip") + +@router.get("/build") +def get_build(build: int): + for file in ROOT_PATH.iterdir(): + pf = PackFile(file) + if pf.build == build: + return FileResponse(pf.file, filename=f"mc-optimizer-pack-{pf.build}.zip") + raise HTTPException(404, "Build not found.") \ No newline at end of file