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
|
|
@ -6,6 +6,7 @@ from typing import Any
|
|||
import httpx
|
||||
|
||||
from . import config
|
||||
from .cache import Cache
|
||||
|
||||
|
||||
class Trace:
|
||||
|
|
@ -68,12 +69,23 @@ async def extract(client: httpx.AsyncClient, html: str, url: str, trace: Trace)
|
|||
return result
|
||||
|
||||
|
||||
async def scrape_url(client: httpx.AsyncClient, url: str) -> dict:
|
||||
"""render -> extract for one URL. Raises StageError on hard failure."""
|
||||
async def scrape_url(client: httpx.AsyncClient, url: str, cache: Cache | None = None) -> dict:
|
||||
"""render -> extract for one URL. Raises StageError on hard failure.
|
||||
|
||||
The extracted result is cached by URL; a hit skips render entirely.
|
||||
"""
|
||||
trace = Trace()
|
||||
cache_key = f"scrape:{url}"
|
||||
if cache:
|
||||
start = time.monotonic()
|
||||
cached = await cache.get_json(cache_key)
|
||||
if cached:
|
||||
trace.record("scrape_cache", (time.monotonic() - start) * 1000, cache="hit")
|
||||
return {**cached, "trace": trace.as_dict()}
|
||||
|
||||
rendered = await render(client, url, trace)
|
||||
extracted = await extract(client, rendered["html"], rendered.get("final_url") or url, trace)
|
||||
return {
|
||||
result = {
|
||||
"url": url,
|
||||
"final_url": rendered.get("final_url"),
|
||||
"status": rendered.get("status"),
|
||||
|
|
@ -81,5 +93,8 @@ async def scrape_url(client: httpx.AsyncClient, url: str) -> dict:
|
|||
"content": extracted.get("text", ""),
|
||||
"metadata": extracted.get("metadata", {}),
|
||||
"extractor_used": extracted.get("extractor_used"),
|
||||
"trace": trace.as_dict(),
|
||||
"thin": extracted.get("thin", False),
|
||||
}
|
||||
if cache and result["content"] and not result["thin"]:
|
||||
await cache.set_json(cache_key, result, config.EXTRACT_CACHE_TTL)
|
||||
return {**result, "trace": trace.as_dict()}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue