syllabus/app/main.py
2025-04-29 17:11:02 -04:00

123 lines
4.9 KiB
Python

from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from fastapi import BackgroundTasks
from functools import partial
import json, download, asyncio
from typing import Optional
app = FastAPI()
app.mount("/data", StaticFiles(directory="/data"), name="data")
templates = Jinja2Templates(directory="templates")
loop = asyncio.get_running_loop()
# JSON cache
cached_data = None
# api
@app.get("/dropout/update")
async def dropoutUpdate(force: bool = True):
global cached_data
try:
download.dropout.series(force)
with open('/data/dropout.json') as f:
cached_data = json.load(f)
return JSONResponse(status_code=200, content={"status": "success", "message": "Series grab complete."})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.get("/dropout/series")
async def dropoutSeries():
global cached_data
if cached_data is None:
await dropoutUpdate()
return JSONResponse(content=cached_data)
return JSONResponse(content={"error": "File not found"}, status_code=404)
@app.post("/dropout/download", description="Download an entire season from episode 1. Ignores behind the scenes and trailers.")
async def dropoutDownload(
background_tasks: BackgroundTasks,
show: str = Form(...),
season: int = Form(...)
):
try:
background_tasks.add_task(download.dropout.show,show,season,None)
# download.dropout.show(show,season,episode)
return JSONResponse(status_code=200, content={"status": "success", "message": "Series downloaded."})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.post("/dropout/download/specials", description="Downloads a seasons behind the scenes and trailers, ignores main episodes.")
async def dropoutDownload(
background_tasks: BackgroundTasks,
show: str = Form(...),
season: int = Form(...),
episode: Optional[int] = Form(None)
):
try:
background_tasks.add_task(download.dropout.specials,show,season,episode)
# download.dropout.show(show,season,episode)
return JSONResponse(status_code=200, content={"status": "success", "message": "Series downloaded."})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.post("/ydl")
async def ydl(background_tasks: BackgroundTasks, url: str = Form(...), location: str = Form(...)):
try:
background_tasks.add_task(download.youtube.ydl, url, location)
# download.youtube.ydl(url,location)
# grab.thumbnail(ydl,url,location)
return JSONResponse(status_code=200, content={"status": "success", "message": "Video download completed."})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.get("/", include_in_schema=False, response_class=HTMLResponse)
async def index(request: Request):
global cached_data
try:
if cached_data is None:
await dropoutUpdate()
return templates.TemplateResponse("index.html", {"request": request, "data": cached_data})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.get("/show/{show}", include_in_schema=False, response_class=HTMLResponse)
async def index(request: Request, show = str):
try:
global cached_data
if cached_data is None:
await dropoutUpdate()
try:
for item in cached_data:
if show == item['LINK']:
if "SEASONS" not in item:
item['SEASONS'] = download.grab.season(item['URL'])
return templates.TemplateResponse("show.html", {"request": request, "show": item})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.get("/ydl", include_in_schema=False)
async def webpage(request: Request):
try:
return templates.TemplateResponse("ydl.html", {"request": request})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})
@app.get("/dropout", include_in_schema=False)
async def webpage(request: Request):
global cached_data
if cached_data is None:
await dropoutUpdate()
try:
return templates.TemplateResponse("dropout.html", {"request": request, "data": cached_data})
except Exception as e:
return JSONResponse(status_code=500, content={"status": "error", "message": str(e)})