100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
from __future__ import annotations
|
|||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from fastapi import APIRouter, HTTPException, Query, status
|
||
|
|
from pydantic import BaseModel, Field
|
||
|
|
|
||
|
|
from sakura_cloud.api.deps import CurrentUser
|
||
|
|
from sakura_cloud.schemas.common import ApiResponse
|
||
|
|
from sakura_cloud.services.online_search import OnlineSearchError, parse_source_payload, search_sources
|
||
|
|
from sakura_cloud.services.online_search_engine import OnlineSearchEngine, SearchEngineError
|
||
|
|
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
class OnlineSearchRequest(BaseModel):
|
||
|
|
query: str = Field(min_length=1, max_length=80)
|
||
|
|
sources: Any
|
||
|
|
limit_sources: int = Field(default=30, ge=1, le=80)
|
||
|
|
per_source_limit: int = Field(default=20, ge=1, le=50)
|
||
|
|
|
||
|
|
|
||
|
|
class OnlineSearchResult(BaseModel):
|
||
|
|
source_name: str
|
||
|
|
source_url: str
|
||
|
|
title: str
|
||
|
|
author: str | None = None
|
||
|
|
cover_url: str | None = None
|
||
|
|
detail_url: str
|
||
|
|
kind: str
|
||
|
|
|
||
|
|
|
||
|
|
class OnlineParseRequest(BaseModel):
|
||
|
|
detail_url: str = Field(min_length=1, max_length=2048)
|
||
|
|
|
||
|
|
|
||
|
|
class OnlineParsedChapter(BaseModel):
|
||
|
|
name: str
|
||
|
|
url: str | None = None
|
||
|
|
content: str | None = None
|
||
|
|
images: list[str] = Field(default_factory=list)
|
||
|
|
|
||
|
|
|
||
|
|
class OnlineParsedContent(BaseModel):
|
||
|
|
title: str
|
||
|
|
cover: str | None = None
|
||
|
|
kind: str
|
||
|
|
chapters: list[OnlineParsedChapter]
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("", response_model=ApiResponse[list[OnlineSearchResult]])
|
||
|
|
def online_search(
|
||
|
|
payload: OnlineSearchRequest,
|
||
|
|
current_user: CurrentUser,
|
||
|
|
mode: str = Query(pattern="^(novel|manga)$"),
|
||
|
|
) -> ApiResponse[list[OnlineSearchResult]]:
|
||
|
|
try:
|
||
|
|
sources = parse_source_payload(payload.sources)
|
||
|
|
rows = search_sources(
|
||
|
|
sources,
|
||
|
|
payload.query.strip(),
|
||
|
|
mode=mode,
|
||
|
|
limit_sources=payload.limit_sources,
|
||
|
|
per_source_limit=payload.per_source_limit,
|
||
|
|
)
|
||
|
|
except OnlineSearchError as error:
|
||
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(error)) from error
|
||
|
|
except ValueError as error:
|
||
|
|
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Invalid source JSON") from error
|
||
|
|
return ApiResponse(data=[OnlineSearchResult(**item.__dict__) for item in rows])
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/parse", response_model=ApiResponse[OnlineParsedContent])
|
||
|
|
def parse_online_content(
|
||
|
|
payload: OnlineParseRequest,
|
||
|
|
current_user: CurrentUser,
|
||
|
|
mode: str = Query(pattern="^(novel|manga)$"),
|
||
|
|
) -> ApiResponse[OnlineParsedContent]:
|
||
|
|
engine = OnlineSearchEngine(timeout=12.0)
|
||
|
|
try:
|
||
|
|
if mode == "manga":
|
||
|
|
parsed = engine.parse_comic(payload.detail_url)
|
||
|
|
chapters = [
|
||
|
|
OnlineParsedChapter(name=item.name, url=item.url, images=list(item.images))
|
||
|
|
for item in parsed.chapters
|
||
|
|
]
|
||
|
|
data = OnlineParsedContent(title=parsed.title, cover=parsed.cover, kind="manga", chapters=chapters)
|
||
|
|
else:
|
||
|
|
parsed = engine.parse_novel(payload.detail_url)
|
||
|
|
chapters = [
|
||
|
|
OnlineParsedChapter(name=item.name, url=item.url, content=item.content)
|
||
|
|
for item in parsed.chapters
|
||
|
|
]
|
||
|
|
data = OnlineParsedContent(title=parsed.title, cover=parsed.cover, kind="novel", chapters=chapters)
|
||
|
|
except SearchEngineError as error:
|
||
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(error)) from error
|
||
|
|
except Exception as error:
|
||
|
|
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="Online source parse failed") from error
|
||
|
|
return ApiResponse(data=data)
|