feat: initial commit
This commit is contained in:
4
service/routers/__init__.py
Normal file
4
service/routers/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .scrape import router as scrape_router
|
||||
from .health import router as health_router
|
||||
|
||||
__all__ = ["scrape_router", "health_router"]
|
||||
15
service/routers/health.py
Normal file
15
service/routers/health.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from fastapi import APIRouter
|
||||
from ..models.response import HealthResponse
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
VERSION = "0.1.0"
|
||||
|
||||
|
||||
@router.get("/health", response_model=HealthResponse)
|
||||
async def health() -> HealthResponse:
|
||||
return HealthResponse(
|
||||
status="ok",
|
||||
version=VERSION,
|
||||
dynamic_session_ready=True,
|
||||
)
|
||||
35
service/routers/scrape.py
Normal file
35
service/routers/scrape.py
Normal file
@@ -0,0 +1,35 @@
|
||||
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),
|
||||
)
|
||||
Reference in New Issue
Block a user