Download Backend/fastapi/main.py from DevDoCode/stremio: direct link, hf CLI and curl.
- Browser
- Download file 5.63 kB
-
https://huggingface.co/spaces/DevDoCode/stremio/resolve/690a196abfc54cb84bd47339716d0b71b6dd720d/Backend/fastapi/main.py
- Command line
-
hf download hf://spaces/DevDoCode/stremio@690a196abfc54cb84bd47339716d0b71b6dd720d/Backend/fastapi/main.py
-
curl -L -o main.py https://huggingface.co/spaces/DevDoCode/stremio/resolve/690a196abfc54cb84bd47339716d0b71b6dd720d/Backend/fastapi/main.py
5.63 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 | |
| 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 | |
| ) | |
| 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 | |
| ) | |
| 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 | |
| # --- Include existing API routers --- | |
| app.include_router(stream_router) | |
| app.include_router(stremio_router) | |
| # --- Public Routes (No Authentication Required) --- | |
| async def login_get(request: Request): | |
| return await login_page(request) | |
| async def login_post_route(request: Request, username: str = Form(...), password: str = Form(...)): | |
| return await login_post(request, username, password) | |
| async def logout_route(request: Request): | |
| return await logout(request) | |
| async def set_theme_route(request: Request, theme: str = Form(...)): | |
| return await set_theme(request, theme) | |
| async def public_status(request: Request): | |
| return await public_status_page(request) | |
| async def stremio_guide(request: Request): | |
| return await stremio_guide_page(request) | |
| # --- Protected Routes (Authentication Required) --- | |
| async def root(request: Request, _: bool = Depends(require_auth)): | |
| return await dashboard_page(request, _) | |
| async def media_management(request: Request, media_type: str = "movie", _: bool = Depends(require_auth)): | |
| return await media_management_page(request, media_type, _) | |
| 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, _) | |
| 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) | |
| 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) | |
| 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) | |
| async def delete_movie_quality(tmdb_id: int, db_index: int, quality: str, _: bool = Depends(require_auth)): | |
| return await delete_movie_quality_api(tmdb_id, db_index, quality) | |
| async def delete_tv_quality(tmdb_id: int, db_index: int, season: int, episode: int, quality: str, _: bool = Depends(require_auth)): | |
| return await delete_tv_quality_api(tmdb_id, db_index, season, episode, quality) | |
| 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) | |
| 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) | |
| 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": {}} | |
| async def auth_exception_handler(request: Request, exc): | |
| return RedirectResponse(url="/login", status_code=302) | |