# 2️⃣ Create a virtualenv python3 -m venv venv source venv/bin/activate
if settings.WHITELISTED_DOMAINS: domain = parsed.hostname or "" if domain not in settings.WHITELISTED_DOMAINS: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail=f"Domain 'domain' is not whitelisted.", )
# --------------------------------------------------------------------------- # # Core: async generator that streams the remote response to the caller. # --------------------------------------------------------------------------- # async def stream_remote_file(url: str) -> AsyncGenerator[bytes, None]: """ Yield chunks from the remote URL, respecting the global size limit. """ parsed = _validate_url(url) heretic webdl
settings = Settings() # app/downloader.py import re import urllib.parse from typing import AsyncGenerator
# Stream the remote file directly back to the caller # We preserve the original content‑type and disposition when possible async def generator(): async for chunk in stream_remote_file(payload.url): yield chunk # 2️⃣ Create a virtualenv python3 -m venv
# --------------------------------------------------- # # Main download endpoint # --------------------------------------------------- # @app.post("/download") async def download(req: Request, payload: DownloadRequest): client_ip = req.client.host _check_rate_limit(client_ip)
# 5️⃣ Run the dev server uvicorn app.main:app --reload Open http://127.0.0.1:8000/docs – you’ll see the interactive Swagger UI. # Dockerfile FROM python:3.11-slim # Dockerfile FROM python:3
# --------------------------------------------------- # # Simple in‑memory rate limiter (IP → timestamps) # --------------------------------------------------- # _rate_limiter: Dict[str, list[datetime]] = defaultdict(list)