stremio / Backend /fastapi /main.py
vflixa1prime
New Feature added (#33)
f697c3b unverified
Raw History Blame
7.99 kB
from fastapi import FastAPI, Request, Form, Depends, Query
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from Backend import __version__
from Backend.fastapi.security.credentials import require_auth
from Backend.fastapi.routes.stream_routes import router as stream_router, decay_client_failures
from Backend.fastapi.routes.stremio_routes import router as stremio_router
from Backend.fastapi.routes.template_routes import (
login_page, login_post, logout, set_theme, dashboard_page,
media_management_page, edit_media_page, public_status_page, stremio_guide_page,
admin_dashboard_page
)
from Backend.fastapi.routes.api_routes import (
list_media_api, delete_media_api, update_media_api,
delete_movie_quality_api, delete_tv_quality_api,
delete_tv_episode_api, delete_tv_season_api,
create_token_api, revoke_token_api, update_token_limits_api,
speed_test_api, speed_test_stream_api,
get_admin_stats_api, clear_cache_api, get_dead_links_api,
get_stream_analytics_api
)
app = FastAPI(
title="Telegram Stremio Media Server",
description="A powerful, self-hosted Telegram Stremio Media Server built with FastAPI, MongoDB, and PyroFork seamlessly integrated with Stremio for automated media streaming and discovery.",
version=__version__
)
# --- Middleware Setup ---
app.add_middleware(SessionMiddleware, secret_key="f6d2e3b9a0f43d9a2e6a56b2d3175cd9c05bbfe31d95ed2a7306b57cb1a8b6f0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
try:
app.mount("/static", StaticFiles(directory="Backend/fastapi/static"), name="static")
except Exception:
pass
@app.on_event("startup")
async def _startup():
import asyncio
asyncio.create_task(decay_client_failures())
# --- Include existing API routers ---
app.include_router(stream_router)
app.include_router(stremio_router)
# --- Public Routes (No Authentication Required) ---
@app.get("/login", response_class=HTMLResponse)
async def login_get(request: Request):
return await login_page(request)
@app.post("/login", response_class=HTMLResponse)
async def login_post_route(request: Request, username: str = Form(...), password: str = Form(...)):
return await login_post(request, username, password)
@app.get("/logout")
async def logout_route(request: Request):
return await logout(request)
@app.post("/set-theme")
async def set_theme_route(request: Request, theme: str = Form(...)):
return await set_theme(request, theme)
@app.get("/status", response_class=HTMLResponse)
async def public_status(request: Request):
return await public_status_page(request)
@app.get("/stremio", response_class=HTMLResponse)
async def stremio_guide(request: Request):
return await stremio_guide_page(request)
# --- Protected Routes (Authentication Required) ---
@app.get("/", response_class=HTMLResponse)
async def root(request: Request, _: bool = Depends(require_auth)):
return await dashboard_page(request, _)
@app.get("/admin/dashboard", response_class=HTMLResponse)
async def admin_dashboard(request: Request, _: bool = Depends(require_auth)):
return await admin_dashboard_page(request, _)
@app.get("/media/manage", response_class=HTMLResponse)
async def media_management(request: Request, media_type: str = "movie", _: bool = Depends(require_auth)):
return await media_management_page(request, media_type, _)
@app.get("/media/edit", response_class=HTMLResponse)
async def edit_media(request: Request, tmdb_id: int, db_index: int, media_type: str, _: bool = Depends(require_auth)):
return await edit_media_page(request, tmdb_id, db_index, media_type, _)
@app.get("/api/media/list")
async def list_media(
media_type: str = Query("movie", regex="^(movie|tv)$"),
page: int = Query(1, ge=1),
page_size: int = Query(24, ge=1, le=100),
search: str = Query("", max_length=100),
_: bool = Depends(require_auth)
):
return await list_media_api(media_type, page, page_size, search)
@app.delete("/api/media/delete")
async def delete_media(tmdb_id: int, db_index: int, media_type: str, _: bool = Depends(require_auth)):
return await delete_media_api(tmdb_id, db_index, media_type)
@app.put("/api/media/update")
async def update_media(request: Request, tmdb_id: int, db_index: int, media_type: str, _: bool = Depends(require_auth)):
return await update_media_api(request, tmdb_id, db_index, media_type)
@app.delete("/api/media/delete-quality")
async def delete_movie_quality(tmdb_id: int, db_index: int, id: str, _: bool = Depends(require_auth)):
return await delete_movie_quality_api(tmdb_id, db_index, id)
@app.delete("/api/media/delete-tv-quality")
async def delete_tv_quality(tmdb_id: int, db_index: int, season: int, episode: int, id: str, _: bool = Depends(require_auth)):
return await delete_tv_quality_api(tmdb_id, db_index, season, episode, id)
@app.delete("/api/media/delete-tv-episode")
async def delete_tv_episode(tmdb_id: int, db_index: int, season: int, episode: int, _: bool = Depends(require_auth)):
return await delete_tv_episode_api(tmdb_id, db_index, season, episode)
@app.delete("/api/media/delete-tv-season")
async def delete_tv_season(tmdb_id: int, db_index: int, season: int, _: bool = Depends(require_auth)):
return await delete_tv_season_api(tmdb_id, db_index, season)
@app.get("/api/system/workloads")
async def get_workloads(_: bool = Depends(require_auth)):
try:
from Backend.pyrofork.bot import work_loads
return {
"loads": {
f"bot{c + 1}": l
for c, (_, l) in enumerate(
sorted(work_loads.items(), key=lambda x: x[1], reverse=True)
)
} if work_loads else {}
}
except Exception as e:
return {"loads": {}}
@app.post("/api/tokens")
async def create_token(payload: dict, _: bool = Depends(require_auth)):
return await create_token_api(payload)
@app.put("/api/tokens/{token}")
async def update_token(token: str, payload: dict, _: bool = Depends(require_auth)):
return await update_token_limits_api(token, payload)
@app.delete("/api/tokens/{token}")
async def revoke_token(token: str, _: bool = Depends(require_auth)):
return await revoke_token_api(token)
@app.get("/api/system/stats")
async def get_system_stats(_: bool = Depends(require_auth)):
from Backend.fastapi.routes.api_routes import get_system_stats_api
return await get_system_stats_api()
@app.get("/api/admin/system-stats")
async def admin_system_stats(_: bool = Depends(require_auth)):
return await get_admin_stats_api()
@app.post("/api/admin/clear-cache")
async def clear_cache(_: bool = Depends(require_auth)):
return await clear_cache_api()
@app.get("/api/admin/dead-links")
async def get_dead_links(_: bool = Depends(require_auth)):
return await get_dead_links_api()
@app.get("/api/admin/stream-analytics")
async def get_stream_analytics(_: bool = Depends(require_auth)):
return await get_stream_analytics_api()
@app.get("/api/system/speedtest")
async def speed_test(
quality_id: str = Query(...),
tmdb_id: int = Query(...),
db_index: int = Query(...),
media_type: str = Query(...),
_: bool = Depends(require_auth)
):
return await speed_test_api(quality_id, tmdb_id, db_index, media_type)
@app.get("/api/system/speedtest/stream")
async def speed_test_stream(
quality_id: str = Query(...),
tmdb_id: int = Query(...),
db_index: int = Query(...),
media_type: str = Query(...),
_: bool = Depends(require_auth)
):
return await speed_test_stream_api(quality_id, tmdb_id, db_index, media_type)
@app.exception_handler(401)
async def auth_exception_handler(request: Request, exc):
return RedirectResponse(url="/login", status_code=302)