Upload files to "kronic/resolver"
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import httpx
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from .models import FormatInfo, SearchResponse, VideoInfo
|
||||
from .ytdlp_service import YTDLP
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Kept for future use; direct media is consumed by Kronic/ffmpeg.
|
||||
app.state.http = httpx.AsyncClient(
|
||||
follow_redirects=True,
|
||||
timeout=httpx.Timeout(
|
||||
connect=10.0,
|
||||
read=60.0,
|
||||
write=30.0,
|
||||
pool=30.0,
|
||||
),
|
||||
)
|
||||
yield
|
||||
await app.state.http.aclose()
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Kronic YouTube Resolver",
|
||||
version="1.1.0-direct",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=False,
|
||||
allow_methods=["GET", "HEAD", "OPTIONS"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health() -> dict[str, str]:
|
||||
return {
|
||||
"status": "ok",
|
||||
"mode": "direct-format",
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/search", response_model=SearchResponse)
|
||||
async def search(
|
||||
q: str,
|
||||
limit: int = 20,
|
||||
) -> SearchResponse:
|
||||
try:
|
||||
raw = await YTDLP.search(q, limit=limit)
|
||||
except Exception as error:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"yt-dlp search failed: {type(error).__name__}: {error}",
|
||||
) from error
|
||||
|
||||
return SearchResponse(**raw)
|
||||
|
||||
|
||||
@app.get("/api/video/{video_id}", response_model=VideoInfo)
|
||||
async def video_info(
|
||||
video_id: str,
|
||||
refresh: bool = False,
|
||||
) -> VideoInfo:
|
||||
try:
|
||||
raw = await YTDLP.video(video_id, refresh=refresh)
|
||||
except Exception as error:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail=f"yt-dlp video resolve failed: {type(error).__name__}: {error}",
|
||||
) from error
|
||||
|
||||
formats = []
|
||||
|
||||
for fmt in raw["formats"]:
|
||||
direct_url = fmt.get("url")
|
||||
if not direct_url:
|
||||
continue
|
||||
|
||||
headers = {
|
||||
str(k): str(v)
|
||||
for k, v in (fmt.get("http_headers") or {}).items()
|
||||
if v is not None
|
||||
}
|
||||
|
||||
formats.append(
|
||||
FormatInfo(
|
||||
**{
|
||||
k: v
|
||||
for k, v in fmt.items()
|
||||
if k not in {"url", "http_headers"}
|
||||
},
|
||||
direct_url=direct_url,
|
||||
http_headers=headers,
|
||||
)
|
||||
)
|
||||
|
||||
return VideoInfo(
|
||||
id=raw["id"],
|
||||
title=raw["title"],
|
||||
duration=raw.get("duration"),
|
||||
channel=raw.get("channel"),
|
||||
channel_id=raw.get("channel_id"),
|
||||
thumbnail=raw.get("thumbnail"),
|
||||
formats=formats,
|
||||
)
|
||||
Reference in New Issue
Block a user