add 实时进度API

This commit is contained in:
jxxghp
2023-06-19 18:38:59 +08:00
parent 9d8866de47
commit b181af40cd
9 changed files with 220 additions and 89 deletions

View File

@ -1,7 +1,7 @@
from fastapi import APIRouter
from app.api.endpoints import login, user, site, message, webhook, subscribe, \
media, douban, search, plugin, tmdb, history
media, douban, search, plugin, tmdb, history, system
api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
@ -15,4 +15,5 @@ api_router.include_router(search.router, prefix="/search", tags=["search"])
api_router.include_router(douban.router, prefix="/douban", tags=["douban"])
api_router.include_router(tmdb.router, prefix="/tmdb", tags=["tmdb"])
api_router.include_router(history.router, prefix="/history", tags=["history"])
api_router.include_router(system.router, prefix="/system", tags=["system"])
api_router.include_router(plugin.router, prefix="/plugin", tags=["plugin"])

View File

@ -0,0 +1,25 @@
import asyncio
import json
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from app.helper.progress import ProgressHelper
router = APIRouter()
@router.get("/progress/{process_type}", summary="实时进度")
async def get_progress(process_type: str):
"""
实时获取处理进度返回格式为SSE
"""
progress = ProgressHelper()
async def event_generator():
while True:
detail = progress.get(process_type)
yield 'data: %s\n\n' % json.dumps(detail)
await asyncio.sleep(0.2)
return StreamingResponse(event_generator(), media_type="text/event-stream")