36 lines
1.0 KiB
Python
36 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from ..models.request import ScrapeRequest
|
|
from ..models.response import ScrapeResponse
|
|
from ..scrapers import DynamicScraper, HttpScraper, StealthyScraper
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/scrape", response_model=ScrapeResponse)
|
|
async def scrape(req: ScrapeRequest) -> ScrapeResponse:
|
|
try:
|
|
if req.fetcher_type == "http":
|
|
scraper = HttpScraper()
|
|
elif req.fetcher_type == "stealth":
|
|
scraper = StealthyScraper()
|
|
elif req.fetcher_type == "dynamic":
|
|
scraper = DynamicScraper()
|
|
else:
|
|
raise HTTPException(status_code=400, detail=f"Unknown fetcher_type: {req.fetcher_type}")
|
|
|
|
return await scraper.scrape(req)
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as exc:
|
|
return ScrapeResponse(
|
|
url=req.url,
|
|
status_code=0,
|
|
fetcher_used=req.fetcher_type,
|
|
elapsed_ms=0,
|
|
error=str(exc),
|
|
)
|