M2: extraction quality — fallback extractors, thin-content detection, result cache
- extractor: trafilatura (precision) -> trafilatura (recall) -> readability-lxml fallback chain; longest result wins; 'thin' flag below 200 chars - orchestrator: Redis-backed scrape-result cache keyed by URL (cache hit skips render entirely); thin/empty results are never cached - /health reports orchestrator Redis reachability Verified: HN + synthetic thin/empty/boilerplate pages; cache hit path via fakeredis. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
e2b329b2bb
commit
56e7fa16dd
8 changed files with 156 additions and 32 deletions
|
|
@ -7,6 +7,7 @@ from fastapi import FastAPI, HTTPException
|
|||
from pydantic import BaseModel
|
||||
|
||||
from . import config, pipeline
|
||||
from .cache import Cache
|
||||
from .logging_setup import setup
|
||||
|
||||
log = setup()
|
||||
|
|
@ -15,7 +16,9 @@ log = setup()
|
|||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
app.state.client = httpx.AsyncClient()
|
||||
app.state.cache = Cache(config.REDIS_URL)
|
||||
yield
|
||||
await app.state.cache.close()
|
||||
await app.state.client.aclose()
|
||||
|
||||
|
||||
|
|
@ -38,7 +41,12 @@ async def health() -> dict:
|
|||
except httpx.HTTPError:
|
||||
services[name] = {"status": "unreachable"}
|
||||
overall = "ok" if all(s.get("status") == "ok" for s in services.values()) else "degraded"
|
||||
return {"status": overall, "service": "orchestrator", "services": services}
|
||||
return {
|
||||
"status": overall,
|
||||
"service": "orchestrator",
|
||||
"redis": await app.state.cache.is_ready(),
|
||||
"services": services,
|
||||
}
|
||||
|
||||
|
||||
@app.post("/scrape")
|
||||
|
|
@ -46,7 +54,7 @@ async def scrape(req: ScrapeRequest) -> dict:
|
|||
if not req.url.startswith(("http://", "https://")):
|
||||
raise HTTPException(status_code=400, detail="url must be http(s)")
|
||||
try:
|
||||
result = await pipeline.scrape_url(app.state.client, req.url)
|
||||
result = await pipeline.scrape_url(app.state.client, req.url, app.state.cache)
|
||||
except pipeline.StageError as exc:
|
||||
log.error("scrape failed at %s: %s", exc.stage, exc.detail)
|
||||
raise HTTPException(status_code=502, detail={"stage": exc.stage, "error": exc.detail}) from exc
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue