Download Backend/fastapi/main.py from DevDoCode/stremio: direct link, hf CLI and curl.
- Browser
- Download file 34 kB
-
https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/fastapi/main.py
- Command line
-
hf download hf://spaces/DevDoCode/stremio/Backend/fastapi/main.py
-
curl -L -o main.py https://huggingface.co/spaces/DevDoCode/stremio/resolve/main/Backend/fastapi/main.py
34 kB
| import asyncio | |
| from fastapi import Depends, FastAPI, Form, HTTPException, Query, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse, Response | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.templating import Jinja2Templates | |
| from Backend import __version__ | |
| from Backend.fastapi.themes import get_theme, theme_key | |
| from Backend.fastapi.routes.api_routes import ( | |
| add_custom_catalog_item_api, | |
| add_subscription_plan_api, | |
| apply_media_rescan_api, | |
| assign_plan_api, | |
| auto_catalog_sync_status_api, | |
| auto_sync_custom_catalogs_api, | |
| cancel_dbcheck_api, | |
| cancel_duplicate_check_api, | |
| cancel_scan_api, | |
| clear_cache_api, | |
| clear_stream_analytics_api, | |
| create_custom_catalog_api, | |
| create_token_api, | |
| grant_lifetime_api, | |
| set_token_lifetime_api, | |
| set_token_expiry_api, | |
| subscription_preflight_api, | |
| backfill_subscriber_names_api, | |
| dbcheck_status_api, | |
| duplicate_check_status_api, | |
| delete_custom_catalog_api, | |
| delete_media_api, | |
| delete_movie_quality_api, | |
| delete_request_api, | |
| delete_subscription_plan_api, | |
| export_config_api, | |
| import_config_api, | |
| delete_tv_episode_api, | |
| delete_tv_quality_api, | |
| delete_tv_season_api, | |
| download_logs_api, | |
| get_admin_stats_api, | |
| get_db_stats_api, | |
| get_all_subscribers_api, | |
| get_all_tokens_api, | |
| get_auto_catalog_settings_api, | |
| get_catalog_order_api, | |
| update_catalog_order_api, | |
| get_user_activity_api, | |
| session_send_code_api, | |
| session_verify_code_api, | |
| session_verify_password_api, | |
| session_status_api, | |
| session_disconnect_api, | |
| session_reconnect_api, | |
| session_remove_api, | |
| get_custom_catalog_items_api, | |
| get_dead_links_api, | |
| get_media_visibility_api, | |
| get_requests_api, | |
| request_popular_api, | |
| request_search_api, | |
| request_submit_api, | |
| get_stream_analytics_api, | |
| get_subscription_plans_api, | |
| get_settings_api, | |
| get_logs_api, | |
| get_manual_session_api, | |
| get_system_stats_api, | |
| get_tools_channels_api, | |
| bot_admin_scan_api, | |
| bot_admin_apply_api, | |
| bot_admin_apply_status_api, | |
| clear_manual_session_api, | |
| search_manual_session_api, | |
| set_manual_session_api, | |
| health_api, | |
| health_report_api, | |
| version_status_api, | |
| setup_status_api, | |
| link_token_user_api, | |
| list_custom_catalogs_api, | |
| list_media_api, | |
| manage_subscriber_api, | |
| manual_add_media_api, | |
| list_manual_add_catalogs_api, | |
| resolve_manual_metadata_api, | |
| purge_dead_links_api, | |
| purge_duplicates_api, | |
| remove_custom_catalog_item_api, | |
| resolve_telegram_api, | |
| resolve_subtitle_api, | |
| list_subtitle_languages_api, | |
| list_subtitles_api, | |
| add_subtitles_api, | |
| remove_subtitle_api, | |
| restart_app_api, | |
| revoke_token_api, | |
| scan_status_api, | |
| search_catalog_media_api, | |
| set_media_visibility_api, | |
| search_media_rescan_api, | |
| tag_media_api, | |
| speed_test_api, | |
| speed_test_stream_api, | |
| start_dbcheck_api, | |
| start_duplicate_check_api, | |
| start_scan_api, | |
| update_auto_catalog_settings_api, | |
| update_custom_catalog_api, | |
| update_media_api, | |
| update_request_api, | |
| update_settings_api, | |
| update_subscription_plan_api, | |
| update_token_cf_api, | |
| update_token_limits_api, | |
| ) | |
| from Backend.fastapi.routes.stream_routes import decay_client_failures | |
| from Backend.fastapi.routes.stream_routes import router as stream_router | |
| from Backend.fastapi.routes.cf_routes import router as cf_router | |
| from Backend.fastapi.routes.stremio_routes import router as stremio_router | |
| from Backend.fastapi.routes.webdav_routes import router as webdav_router | |
| from Backend.fastapi.routes.template_routes import ( | |
| admin_access_page, | |
| admin_dashboard_page, | |
| admin_requests_page, | |
| admin_subscriptions_page, | |
| public_request_page, | |
| custom_catalogs_page, | |
| dashboard_page, | |
| edit_media_page, | |
| login_page, | |
| login_post, | |
| logout, | |
| media_management_page, | |
| settings_page, | |
| set_theme, | |
| tools_page, | |
| ) | |
| from Backend.fastapi.security.credentials import require_auth | |
| from Backend.pyrofork.bot import work_loads_summary | |
| templates = Jinja2Templates(directory="Backend/fastapi/templates") | |
| 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 | |
| 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 | |
| async def _startup(): | |
| asyncio.create_task(decay_client_failures()) | |
| from Backend.helper.version_check import version_check_loop | |
| asyncio.create_task(version_check_loop()) | |
| #----- Streaming and Stremio routers | |
| app.include_router(stream_router) | |
| app.include_router(cf_router) | |
| app.include_router(stremio_router) | |
| app.include_router(webdav_router) | |
| #----- Public routes (no authentication) | |
| 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(None)): | |
| return await set_theme(request, theme) | |
| async def pwa_manifest(request: Request): | |
| theme = get_theme(theme_key(request)) | |
| return JSONResponse( | |
| { | |
| "name": "Telegram Stremio", | |
| "short_name": "TG Stremio", | |
| "description": "Telegram Stremio media management", | |
| "start_url": "/", | |
| "scope": "/", | |
| "display": "standalone", | |
| "orientation": "any", | |
| "background_color": theme["colors"]["background"], | |
| "theme_color": theme["colors"]["primary"], | |
| "icons": [ | |
| { | |
| "src": "/pwa-icon.svg", | |
| "sizes": "any", | |
| "type": "image/svg+xml", | |
| "purpose": "any" | |
| }, | |
| { | |
| "src": "/pwa-icon.svg", | |
| "sizes": "any", | |
| "type": "image/svg+xml", | |
| "purpose": "maskable" | |
| } | |
| ] | |
| }, | |
| media_type="application/manifest+json", | |
| headers={"Cache-Control": "no-cache"} | |
| ) | |
| async def pwa_icon(request: Request): | |
| theme = get_theme(theme_key(request)) | |
| primary = theme["colors"]["primary"] | |
| svg = ( | |
| f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">' | |
| f'<rect width="512" height="512" rx="96" fill="{primary}"/>' | |
| f'<path d="M200 152l176 104-176 104z" fill="{theme["colors"]["on_primary"]}"/>' | |
| f'</svg>' | |
| ) | |
| return Response( | |
| content=svg, | |
| media_type="image/svg+xml", | |
| headers={"Cache-Control": "no-cache"} | |
| ) | |
| async def service_worker(): | |
| js = ( | |
| "self.addEventListener('install',e=>self.skipWaiting());" | |
| "self.addEventListener('activate',e=>e.waitUntil(clients.claim()));" | |
| "self.addEventListener('fetch',e=>e.respondWith(fetch(e.request).catch(()=>caches.match(e.request))));" | |
| ) | |
| return Response( | |
| content=js, | |
| media_type="application/javascript", | |
| headers={"Cache-Control": "no-cache", "Service-Worker-Allowed": "/"} | |
| ) | |
| async def public_status(): | |
| return {"status": "ok", "version": __version__} | |
| async def open_in_app(app_name: str, media_type: str, content_id: str): | |
| stremio_type = "series" if media_type in ("series", "tv") else "movie" | |
| web = f"https://web.stremio.com/#/detail/{stremio_type}/{content_id}/{content_id}" | |
| schemes = { | |
| "nuvio": f"nuvio://meta?type={stremio_type}&id={content_id}", | |
| "stremio": f"stremio:///detail/{stremio_type}/{content_id}", | |
| } | |
| scheme = schemes.get(app_name, schemes["stremio"]) | |
| label = "Nuvio" if app_name == "nuvio" else "Stremio" | |
| html = f"""<!doctype html><html><head><meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width,initial-scale=1"> | |
| <title>Opening {label}…</title> | |
| <style>body{{font-family:system-ui,-apple-system,sans-serif;background:#0f172a;color:#f8fafc;display:flex;min-height:100vh;align-items:center;justify-content:center;margin:0;text-align:center}}a{{color:#60a5fa}}.b{{display:inline-block;margin-top:16px;padding:12px 22px;background:#3b82f6;color:#fff;border-radius:12px;text-decoration:none;font-weight:700}}</style> | |
| </head><body><div><h2>Opening in {label}…</h2> | |
| <p>If nothing happens, use the buttons below.</p> | |
| <a class="b" href="{scheme}">Open {label}</a><br> | |
| <a class="b" style="background:#334155" href="{web}">Open Stremio Web</a></div> | |
| <script>setTimeout(function(){{window.location.href="{scheme}";}},200);</script> | |
| </body></html>""" | |
| return HTMLResponse(html) | |
| #----- Protected routes (authentication required) | |
| async def root(request: Request, _: bool = Depends(require_auth)): | |
| return await dashboard_page(request, _) | |
| async def admin_dashboard(request: Request, _: bool = Depends(require_auth)): | |
| return await admin_dashboard_page(request, _) | |
| async def media_management(request: Request, media_type: str = "movie", custom: bool = False, _: bool = Depends(require_auth)): | |
| return await media_management_page(request, media_type, custom, _) | |
| async def custom_catalogs(request: Request, _: bool = Depends(require_auth)): | |
| return await custom_catalogs_page(request, _) | |
| 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), | |
| custom: bool = Query(False), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await list_media_api(media_type, page, page_size, search, custom) | |
| 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, id: str, _: bool = Depends(require_auth)): | |
| return await delete_movie_quality_api(tmdb_id, db_index, id) | |
| 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) | |
| 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: | |
| return {"loads": work_loads_summary()} | |
| except Exception: | |
| return {"loads": {}} | |
| async def create_token(payload: dict, _: bool = Depends(require_auth)): | |
| return await create_token_api(payload) | |
| async def update_token(token: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await update_token_limits_api(token, payload) | |
| async def update_token_cf(token: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await update_token_cf_api(token, payload) | |
| async def revoke_token(token: str, _: bool = Depends(require_auth)): | |
| return await revoke_token_api(token) | |
| async def get_system_stats(_: bool = Depends(require_auth)): | |
| return await get_system_stats_api() | |
| async def admin_system_stats(_: bool = Depends(require_auth)): | |
| return await get_admin_stats_api() | |
| async def clear_cache(_: bool = Depends(require_auth)): | |
| return await clear_cache_api() | |
| async def get_dead_links(_: bool = Depends(require_auth)): | |
| return await get_dead_links_api() | |
| async def get_stream_analytics(_: bool = Depends(require_auth)): | |
| return await get_stream_analytics_api() | |
| async def get_user_activity(page: int = 1, per_page: int = 5, _: bool = Depends(require_auth)): | |
| return await get_user_activity_api(page, per_page) | |
| async def clear_analytics(_: bool = Depends(require_auth)): | |
| return await clear_stream_analytics_api() | |
| async def admin_subscriptions(request: Request, _: bool = Depends(require_auth)): | |
| return await admin_subscriptions_page(request, _) | |
| async def get_subscription_plans(_: bool = Depends(require_auth)): | |
| return await get_subscription_plans_api() | |
| async def add_subscription_plan(payload: dict, _: bool = Depends(require_auth)): | |
| return await add_subscription_plan_api(payload) | |
| async def update_subscription_plan(plan_id: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await update_subscription_plan_api(plan_id, payload) | |
| async def delete_subscription_plan(plan_id: str, _: bool = Depends(require_auth)): | |
| return await delete_subscription_plan_api(plan_id) | |
| async def get_subscribers(_: bool = Depends(require_auth)): | |
| return await get_all_subscribers_api() | |
| async def manage_subscriber(user_id: int, payload: dict, _: bool = Depends(require_auth)): | |
| return await manage_subscriber_api(user_id, payload) | |
| #----- Access management | |
| async def admin_access(request: Request, _: bool = Depends(require_auth)): | |
| return await admin_access_page(request, _) | |
| async def get_access_tokens(_: bool = Depends(require_auth)): | |
| return await get_all_tokens_api() | |
| async def delete_access_token(token: str, _: bool = Depends(require_auth)): | |
| return await revoke_token_api(token) | |
| async def assign_access_plan(user_id: int, payload: dict, _: bool = Depends(require_auth)): | |
| days = int(payload.get("days", 0)) | |
| return await assign_plan_api(user_id, days) | |
| async def link_token_to_user(token: str, payload: dict, _: bool = Depends(require_auth)): | |
| user_id = int(payload.get("user_id", 0)) | |
| if not user_id: | |
| raise HTTPException(status_code=400, detail="user_id is required.") | |
| return await link_token_user_api(token, user_id) | |
| async def set_token_lifetime(token: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await set_token_lifetime_api(token, payload) | |
| async def set_token_expiry(token: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await set_token_expiry_api(token, payload) | |
| async def grant_lifetime(_: bool = Depends(require_auth)): | |
| return await grant_lifetime_api() | |
| async def subscription_preflight(_: bool = Depends(require_auth)): | |
| return await subscription_preflight_api() | |
| async def backfill_subscriber_names(_: bool = Depends(require_auth)): | |
| return await backfill_subscriber_names_api() | |
| #----- Public content request page (no auth) | |
| async def public_request(request: Request): | |
| return await public_request_page(request) | |
| async def request_search(q: str = Query("")): | |
| return await request_search_api(q) | |
| async def request_popular(): | |
| return await request_popular_api() | |
| async def request_submit(payload: dict, request: Request): | |
| client_ip = request.client.host if request.client else None | |
| return await request_submit_api(payload, client_ip) | |
| #----- Admin content requests | |
| async def admin_requests(request: Request, _: bool = Depends(require_auth)): | |
| return await admin_requests_page(request, _) | |
| async def get_requests(_: bool = Depends(require_auth)): | |
| return await get_requests_api() | |
| async def update_request(request_id: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await update_request_api(request_id, payload) | |
| async def delete_request_route(request_id: str, _: bool = Depends(require_auth)): | |
| return await delete_request_api(request_id) | |
| 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) | |
| 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) | |
| async def search_media_rescan( | |
| media_type: str, | |
| query: str, | |
| year: int | None = None, | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await search_media_rescan_api(media_type, query, year) | |
| async def apply_media_rescan( | |
| request: Request, | |
| tmdb_id: int, | |
| db_index: int, | |
| media_type: str, | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await apply_media_rescan_api(request, tmdb_id, db_index, media_type) | |
| async def tag_media(tmdb_id: int, db_index: int, media_type: str, _: bool = Depends(require_auth)): | |
| return await tag_media_api(tmdb_id, db_index, media_type) | |
| #----- Manual add (custom movie/tv/season/episode/stream) | |
| async def resolve_telegram(payload: dict, _: bool = Depends(require_auth)): | |
| return await resolve_telegram_api(payload) | |
| async def manual_add_media(payload: dict, _: bool = Depends(require_auth)): | |
| return await manual_add_media_api(payload) | |
| async def manual_add_catalogs(_: bool = Depends(require_auth)): | |
| return await list_manual_add_catalogs_api() | |
| async def manual_add_resolve_meta(media_type: str, selected_id: str, _: bool = Depends(require_auth)): | |
| return await resolve_manual_metadata_api(media_type, selected_id) | |
| #----- Manual subtitle management | |
| async def subtitle_languages(_: bool = Depends(require_auth)): | |
| return list_subtitle_languages_api() | |
| async def list_subtitles(media_type: str, tmdb_id: int, db_index: int, _: bool = Depends(require_auth)): | |
| return await list_subtitles_api(media_type, tmdb_id, db_index) | |
| async def resolve_subtitle(payload: dict, _: bool = Depends(require_auth)): | |
| return await resolve_subtitle_api(payload) | |
| async def add_subtitles(payload: dict, _: bool = Depends(require_auth)): | |
| return await add_subtitles_api(payload) | |
| async def remove_subtitle_route(payload: dict, _: bool = Depends(require_auth)): | |
| return await remove_subtitle_api(payload) | |
| #----- Custom catalog management | |
| async def list_custom_catalogs( | |
| tmdb_id: int | None = None, | |
| db_index: int | None = None, | |
| media_type: str | None = None, | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await list_custom_catalogs_api(tmdb_id, db_index, media_type) | |
| async def create_custom_catalog(payload: dict, _: bool = Depends(require_auth)): | |
| return await create_custom_catalog_api(payload) | |
| async def update_custom_catalog(catalog_id: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await update_custom_catalog_api(catalog_id, payload) | |
| async def delete_custom_catalog(catalog_id: str, _: bool = Depends(require_auth)): | |
| return await delete_custom_catalog_api(catalog_id) | |
| async def set_media_visibility(payload: dict, _: bool = Depends(require_auth)): | |
| return await set_media_visibility_api(payload) | |
| async def get_media_visibility( | |
| tmdb_id: int, | |
| db_index: int, | |
| media_type: str = Query("movie", regex="^(movie|tv|series)$"), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await get_media_visibility_api(tmdb_id, db_index, media_type) | |
| async def search_catalog_media( | |
| query: str, | |
| media_type: str = Query("movie", regex="^(movie|tv)$"), | |
| page: int = Query(1, ge=1), | |
| page_size: int = Query(12, ge=1, le=50), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await search_catalog_media_api(query, media_type, page, page_size) | |
| async def auto_sync_custom_catalogs( | |
| force_refresh: bool = Query(False), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await auto_sync_custom_catalogs_api(force_refresh) | |
| async def auto_catalog_sync_status(_: bool = Depends(require_auth)): | |
| return await auto_catalog_sync_status_api() | |
| async def get_catalog_order_route(_: bool = Depends(require_auth)): | |
| return await get_catalog_order_api() | |
| async def update_catalog_order_route(payload: dict, _: bool = Depends(require_auth)): | |
| return await update_catalog_order_api(payload) | |
| async def get_auto_catalog_settings_route(_: bool = Depends(require_auth)): | |
| return await get_auto_catalog_settings_api() | |
| async def update_auto_catalog_settings_route(payload: dict, _: bool = Depends(require_auth)): | |
| return await update_auto_catalog_settings_api(payload) | |
| async def get_custom_catalog_items( | |
| catalog_id: str, | |
| media_type: str | None = Query(None, regex="^(movie|tv)$"), | |
| page: int = Query(1, ge=1), | |
| page_size: int = Query(24, ge=1, le=100), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await get_custom_catalog_items_api(catalog_id, media_type, page, page_size) | |
| async def add_custom_catalog_item(catalog_id: str, payload: dict, _: bool = Depends(require_auth)): | |
| return await add_custom_catalog_item_api(catalog_id, payload) | |
| async def remove_custom_catalog_item( | |
| catalog_id: str, | |
| tmdb_id: int, | |
| db_index: int, | |
| media_type: str = Query("movie", regex="^(movie|tv)$"), | |
| _: bool = Depends(require_auth) | |
| ): | |
| return await remove_custom_catalog_item_api(catalog_id, tmdb_id, db_index, media_type) | |
| #----- Settings | |
| async def admin_settings(request: Request, _: bool = Depends(require_auth)): | |
| return await settings_page(request, _) | |
| async def get_settings(_: bool = Depends(require_auth)): | |
| return await get_settings_api() | |
| async def update_settings(payload: dict, _: bool = Depends(require_auth)): | |
| return await update_settings_api(payload) | |
| async def formatter_preview(payload: dict, _: bool = Depends(require_auth)): | |
| from Backend.helper.stream_formatter import PRESETS, build_parse_value, render | |
| tpl = PRESETS.get(payload.get("preset")) or {"name": str(payload.get("name") or ""), "description": str(payload.get("description") or "")} | |
| pv = build_parse_value(str(payload.get("filename") or "video.mkv"), 2_500_000_000, channel="My Channel", | |
| meta={"queryType": "movie", "type": "movie"}) | |
| return {"name": render(tpl["name"], pv), "description": render(tpl["description"], pv)} | |
| #----- Telegram user session login (replaces manual USER_SESSION_STRING) | |
| async def session_status(_: bool = Depends(require_auth)): | |
| return await session_status_api() | |
| async def session_send_code(payload: dict, _: bool = Depends(require_auth)): | |
| return await session_send_code_api(payload) | |
| async def session_verify_code(payload: dict, _: bool = Depends(require_auth)): | |
| return await session_verify_code_api(payload) | |
| async def session_verify_password(payload: dict, _: bool = Depends(require_auth)): | |
| return await session_verify_password_api(payload) | |
| async def session_disconnect(_: bool = Depends(require_auth)): | |
| return await session_disconnect_api() | |
| async def session_reconnect(_: bool = Depends(require_auth)): | |
| return await session_reconnect_api() | |
| async def session_remove(_: bool = Depends(require_auth)): | |
| return await session_remove_api() | |
| #----- System & Maintenance (WebUI replacement for /stats, /log, /restart bot commands) | |
| async def admin_db_stats(_: bool = Depends(require_auth)): | |
| return await get_db_stats_api() | |
| async def admin_health(_: bool = Depends(require_auth)): | |
| return await health_api() | |
| async def admin_health_report(fresh: bool = Query(False), _: bool = Depends(require_auth)): | |
| return await health_report_api(force=fresh) | |
| async def admin_version(force: bool = Query(False), _: bool = Depends(require_auth)): | |
| return await version_status_api(force=force) | |
| async def admin_setup_status(_: bool = Depends(require_auth)): | |
| return await setup_status_api() | |
| async def admin_backup_export(_: bool = Depends(require_auth)): | |
| from fastapi.responses import JSONResponse | |
| data = await export_config_api() | |
| return JSONResponse( | |
| content=data, | |
| headers={"Content-Disposition": 'attachment; filename="telegram-stremio-backup.json"'}, | |
| ) | |
| async def admin_backup_library(_: bool = Depends(require_auth)): | |
| from fastapi.responses import StreamingResponse | |
| from Backend.helper.backup import export_library | |
| return StreamingResponse( | |
| export_library(), media_type="application/gzip", | |
| headers={"Content-Disposition": 'attachment; filename="telestremio-catalog.json.gz"'}, | |
| ) | |
| async def admin_backup_import(payload: dict, _: bool = Depends(require_auth)): | |
| return await import_config_api(payload) | |
| async def admin_logs(lines: int = Query(300, ge=1, le=2000), _: bool = Depends(require_auth)): | |
| return await get_logs_api(lines) | |
| async def admin_logs_download(_: bool = Depends(require_auth)): | |
| return await download_logs_api() | |
| async def admin_restart(_: bool = Depends(require_auth)): | |
| return await restart_app_api() | |
| #----- Tools (WebUI replacement for /scan, /rescan, /dbcheck bot commands) | |
| async def admin_tools(request: Request, _: bool = Depends(require_auth)): | |
| return await tools_page(request, _) | |
| async def tools_channels(_: bool = Depends(require_auth)): | |
| return await get_tools_channels_api() | |
| async def tools_bot_admin_scan(_: bool = Depends(require_auth)): | |
| return await bot_admin_scan_api() | |
| async def tools_bot_admin_apply(payload: dict, _: bool = Depends(require_auth)): | |
| return await bot_admin_apply_api(payload) | |
| async def tools_bot_admin_apply_status(_: bool = Depends(require_auth)): | |
| return await bot_admin_apply_status_api() | |
| async def tools_manual_session_get(_: bool = Depends(require_auth)): | |
| return await get_manual_session_api() | |
| async def tools_manual_session_search(query: str = Query(""), _: bool = Depends(require_auth)): | |
| return await search_manual_session_api(query) | |
| async def tools_manual_session_set(payload: dict, _: bool = Depends(require_auth)): | |
| return await set_manual_session_api(payload) | |
| async def tools_manual_session_clear(_: bool = Depends(require_auth)): | |
| return await clear_manual_session_api() | |
| async def tools_scan_start(payload: dict, _: bool = Depends(require_auth)): | |
| return await start_scan_api(payload) | |
| async def tools_scan_cancel(_: bool = Depends(require_auth)): | |
| return await cancel_scan_api() | |
| async def tools_scan_status(_: bool = Depends(require_auth)): | |
| return await scan_status_api() | |
| async def tools_dbcheck_start(_: bool = Depends(require_auth)): | |
| return await start_dbcheck_api() | |
| async def tools_dbcheck_cancel(_: bool = Depends(require_auth)): | |
| return await cancel_dbcheck_api() | |
| async def tools_dbcheck_status(_: bool = Depends(require_auth)): | |
| return await dbcheck_status_api() | |
| async def tools_purge_dead_links(payload: dict | None = None, _: bool = Depends(require_auth)): | |
| return await purge_dead_links_api(payload) | |
| async def tools_duplicates_start(_: bool = Depends(require_auth)): | |
| return await start_duplicate_check_api() | |
| async def tools_duplicates_cancel(_: bool = Depends(require_auth)): | |
| return await cancel_duplicate_check_api() | |
| async def tools_duplicates_status(_: bool = Depends(require_auth)): | |
| return await duplicate_check_status_api() | |
| async def tools_duplicates_purge(payload: dict | None = None, _: bool = Depends(require_auth)): | |
| return await purge_duplicates_api(payload) | |
| async def auth_exception_handler(request: Request, exc): | |
| # API / stream / WebDAV clients must receive a real 401, not an HTML login redirect. | |
| path = request.url.path or "" | |
| if path.startswith(("/webdav", "/dl/", "/sub/", "/stremio/", "/api/", "/thumb/")): | |
| from fastapi.responses import JSONResponse | |
| detail = getattr(exc, "detail", "Unauthorized") | |
| headers = {} | |
| # Preserve WWW-Authenticate for WebDAV Basic auth prompts | |
| if hasattr(exc, "headers") and exc.headers: | |
| headers.update(exc.headers) | |
| return JSONResponse(status_code=401, content={"detail": detail}, headers=headers) | |
| return RedirectResponse(url="/login", status_code=302) | |