Lerne Taxonomie und Trefferliste live von Geizhals, damit Wizard und Kandidaten echte Quelldaten nutzen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
d784248e28
commit
46857715df
25 files changed
+1614
-27
No files matched your search
@@ -7,6 +7,10 @@ from urllib.parse import parse_qs, urlparse
|
||||
|
||||
from app.discovery.enums import FilterType
|
||||
from app.errors import AppError
|
||||
from app.settings import get_settings
|
||||
from app.sources.geizhals.live_page import load_category_page, load_result_page
|
||||
from app.sources.geizhals.result_parser import parse_result_html, with_page
|
||||
from app.sources.geizhals.taxonomy_parser import parse_taxonomy_html
|
||||
|
||||
FIXTURE_DIR = Path(__file__).resolve().parents[3] / "fixtures" / "geizhals"
|
||||
|
||||
@@ -14,12 +18,31 @@ FIXTURE_DIR = Path(__file__).resolve().parents[3] / "fixtures" / "geizhals"
|
||||
class GeizhalsAdapter:
|
||||
source_key = "geizhals"
|
||||
|
||||
def __init__(self, fixture_dir: Path | None = None, live: bool = False) -> None:
|
||||
def __init__(self, fixture_dir: Path | None = None, live: bool | None = None) -> None:
|
||||
self.fixture_dir = fixture_dir or FIXTURE_DIR
|
||||
self.live = live
|
||||
settings = get_settings()
|
||||
self.live = (not settings.geizhals_use_fixtures) if live is None else live
|
||||
|
||||
def discover_taxonomy(self, category_ref: str) -> dict[str, Any]:
|
||||
payload = self._load(f"{category_ref}-taxonomy.json")
|
||||
if not self.live:
|
||||
return self._load(f"{category_ref}-taxonomy.json")
|
||||
url = f"https://geizhals.de/?cat={category_ref}&promode=true"
|
||||
html, mode, status = load_category_page(
|
||||
url,
|
||||
category_ref=category_ref,
|
||||
playwright=get_settings().geizhals_playwright,
|
||||
)
|
||||
payload = parse_taxonomy_html(html, category_ref=category_ref, source_url=url)
|
||||
if payload.get("blocked") or not payload.get("filters"):
|
||||
raise AppError(
|
||||
422,
|
||||
"TAXONOMY_EMPTY",
|
||||
"Geizhals-Taxonomie konnte nicht aus der Kategorieseite gelesen werden",
|
||||
{"url": url, "http_status": status, "mode": mode},
|
||||
)
|
||||
payload["discovery_mode"] = mode
|
||||
payload["http_status"] = status
|
||||
payload["taxonomy_status"] = "DEGRADED" if mode == "recorded_html" else "READY"
|
||||
return payload
|
||||
|
||||
def build_query(self, criteria: list[dict[str, Any]], mappings: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
@@ -34,13 +57,32 @@ class GeizhalsAdapter:
|
||||
return {"url": url, "internal_only": internal_only}
|
||||
|
||||
def fetch_results(self, query: dict[str, Any], pagination: dict[str, Any]) -> dict[str, Any]:
|
||||
if self.live:
|
||||
raise AppError(422, "LIVE_FETCH_DISABLED", "live Geizhals fetch is not enabled in this process")
|
||||
page = int(pagination.get("page") or 1)
|
||||
category = parse_qs(urlparse(query["url"]).query).get("cat", ["nb"])[0]
|
||||
return self._load(f"{category}-search-page{page}.json")
|
||||
if not self.live:
|
||||
category = parse_qs(urlparse(query["url"]).query).get("cat", ["nb"])[0]
|
||||
return self._load(f"{category}-search-page{page}.json")
|
||||
url = with_page(str(query["url"]), page)
|
||||
html, mode, status, blocked = load_result_page(url, playwright=get_settings().geizhals_playwright)
|
||||
if blocked:
|
||||
return {
|
||||
"page": page,
|
||||
"page_count": page,
|
||||
"result_count": 0,
|
||||
"http_status": status or 403,
|
||||
"blocked": True,
|
||||
"challenge": "captcha" if status in {401, 403} else None,
|
||||
"items": [],
|
||||
"discovery_mode": mode,
|
||||
}
|
||||
payload = parse_result_html(html, page_url=url)
|
||||
payload["page"] = page
|
||||
payload["http_status"] = status
|
||||
payload["discovery_mode"] = mode
|
||||
return payload
|
||||
|
||||
def parse_results(self, response: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
if response.get("html") and not response.get("items"):
|
||||
return list(parse_result_html(str(response["html"]), page_url=str(response.get("url") or "")).get("items") or [])
|
||||
return list(response.get("items") or [])
|
||||
|
||||
def normalize_result(self, result: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
Reference in new issue
Block a user