feat: add serve command

This commit is contained in:
Benex254
2024-10-06 01:04:20 +03:00
parent a747529279
commit f7b2b4e0c9
5 changed files with 742 additions and 9 deletions

25
fastanime/api/__init__.py Normal file
View File

@@ -0,0 +1,25 @@
from typing import Literal
from fastapi import FastAPI
from ..AnimeProvider import AnimeProvider
app = FastAPI()
anime_provider = AnimeProvider("allanime", "true", "true")
@app.get("/search")
def search_for_anime(title: str, translation_type: Literal["dub", "sub"] = "sub"):
return anime_provider.search_for_anime(title, translation_type)
@app.get("/anime/{anime_id}")
def get_anime(anime_id: str):
return anime_provider.get_anime(anime_id)
@app.get("/anime/{anime_id}/watch")
def get_episode_streams(
anime_id: str, episode: str, translation_type: Literal["sub", "dub"]
):
return anime_provider.get_episode_streams(anime_id, episode, translation_type)

View File

@@ -16,6 +16,7 @@ commands = {
"completions": "completions.completions",
"update": "update.update",
"grab": "grab.grab",
"serve": "serve.serve",
}

View File

@@ -0,0 +1,31 @@
import click
@click.command(
help="Command that automates the starting of the builtin fastanime server",
epilog="""
\b
\b\bExamples:
# default
fastanime serve
# specify host and port
fastanime serve --host 127.0.0.1 --port 8080
""",
)
@click.option("--host", "-H", help="Specify the host to run the server on")
@click.option("--port", "-p", help="Check for the latest release", type=int)
def serve(host, port):
import os
import sys
from ...constants import APP_DIR
args = ["python", "-m", "fastapi", "run"]
if host:
args.extend(["--host", host])
if port:
args.extend(["--port", port])
args.append(os.path.join(APP_DIR, "api"))
os.execv(sys.executable, args)