Uebernimm Enum-Listen und Filtergruppen in die Taxonomie, speichere Suchtreffer als Kandidaten und lasse CI das vorhandene API-Image nutzen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
2117ca6b54
commit
68a8e3d945
23 files changed
+481
-118
No files matched your search
@@ -20,6 +20,7 @@ EXPAND_LABELS = (
|
||||
"Immer alle Filter anzeigen",
|
||||
"weitere Filter anzeigen",
|
||||
)
|
||||
MORE_OPTION_SELECTOR = 'button:has-text("weitere"), a:has-text("weitere")'
|
||||
|
||||
|
||||
def load_result_page(url: str, *, playwright: bool = True) -> tuple[str, str, int, bool]:
|
||||
@@ -101,6 +102,7 @@ def _fetch_playwright(url: str, *, expand_filters: bool = False) -> tuple[int, s
|
||||
continue
|
||||
if expand_filters:
|
||||
_expand_filter_sections(page)
|
||||
_expand_more_options(page)
|
||||
html = page.content()
|
||||
status = response.status if response is not None else 200
|
||||
browser.close()
|
||||
@@ -119,6 +121,18 @@ def _expand_filter_sections(page: object) -> None:
|
||||
return
|
||||
|
||||
|
||||
def _expand_more_options(page: object) -> None:
|
||||
locator = page.locator(MORE_OPTION_SELECTOR)
|
||||
for _ in range(80):
|
||||
if locator.count() == 0:
|
||||
return
|
||||
try:
|
||||
locator.first.click(timeout=2000)
|
||||
page.wait_for_timeout(350)
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def _blocked(status: int, html: str) -> bool:
|
||||
if status in {401, 403, 429}:
|
||||
return True
|
||||
|
||||
@@ -19,7 +19,26 @@ SKIP_OPTION_LABELS = {
|
||||
"filter überspringen",
|
||||
"einfache filter",
|
||||
"erweiterte filter",
|
||||
"weitere",
|
||||
"weniger",
|
||||
"alle anzeigen",
|
||||
"weniger anzeigen",
|
||||
}
|
||||
PRODUCT_COUNT_RE = re.compile(r"\(\d[\d.]*\s*(produkte)?\)\s*$", re.IGNORECASE)
|
||||
OPTION_TAGS = {"a", "button", "label", "input", "span"}
|
||||
GROUP_ORDER = (
|
||||
"Allgemein",
|
||||
"Display",
|
||||
"CPU",
|
||||
"RAM",
|
||||
"RAM erweitert",
|
||||
"Grafik",
|
||||
"Anschlüsse",
|
||||
"Wireless",
|
||||
"Akku",
|
||||
"Abmessungen",
|
||||
"Sonstiges",
|
||||
)
|
||||
RANGE_MIN_NAMES = {"bpmin", "minp", "prmin"}
|
||||
RANGE_MAX_NAMES = {"bpmax", "maxp", "prmax"}
|
||||
TEXT_NAMES = {"asuch", "fs"}
|
||||
@@ -117,7 +136,11 @@ def parse_taxonomy_html(html: str, *, category_ref: str, source_url: str) -> dic
|
||||
for group in groups.values()
|
||||
if _usable_group(group)
|
||||
]
|
||||
filters.sort(key=lambda item: (item["label"].lower(), item["external_filter_key"]))
|
||||
for group in filters:
|
||||
section = group.get("group_label") or "Allgemein"
|
||||
group["group_label"] = section
|
||||
group["group_sort"] = _group_sort(section)
|
||||
filters.sort(key=lambda item: (item["group_sort"], item["group_label"].lower(), item["label"].lower(), item["external_filter_key"]))
|
||||
return {
|
||||
"source": "geizhals",
|
||||
"external_category_key": category_ref,
|
||||
@@ -148,6 +171,9 @@ def _collect_declared_filters(node: _Node, groups: dict[str, dict[str, Any]]) ->
|
||||
source_type = node.attrs.get("data-filter-type") or ""
|
||||
label = _declared_label(node) or f"Filter {filter_id}"
|
||||
group = groups.setdefault(filter_id, _empty_group(filter_id, label, source_type))
|
||||
section = _section_label(node)
|
||||
if section:
|
||||
group["group_label"] = section
|
||||
if label and group["label"].startswith("Filter "):
|
||||
group["label"] = label
|
||||
if source_type:
|
||||
@@ -169,19 +195,21 @@ def _collect_xf_links(
|
||||
required_key: str | None = None,
|
||||
allow_histogram: bool = False,
|
||||
) -> None:
|
||||
if node.tag == "a":
|
||||
href = node.attrs.get("href") or ""
|
||||
label = node.attrs.get("aria-label") or _node_text(node)
|
||||
if node.tag in OPTION_TAGS:
|
||||
label = node.attrs.get("aria-label") or node.attrs.get("title") or _node_text(node)
|
||||
if _usable_option(node, label, allow_histogram=allow_histogram):
|
||||
for key, query_value in _xf_tokens(href):
|
||||
for key, query_value in _node_xf_tokens(node):
|
||||
if required_key is not None and key != required_key:
|
||||
continue
|
||||
if required_key is None and not _has_structural_label(node):
|
||||
continue
|
||||
group_label = _declared_label(_filter_root(node)) or _nearest_group_label(node)
|
||||
group = groups.setdefault(key, _empty_group(key, group_label, None))
|
||||
if group_label and (group["label"].startswith("Filter ") or group["label"] == key):
|
||||
group["label"] = group_label
|
||||
filter_label = _declared_label(_filter_root(node)) or _nearest_group_label(node)
|
||||
group = groups.setdefault(key, _empty_group(key, filter_label, None))
|
||||
section = _section_label(node)
|
||||
if section:
|
||||
group["group_label"] = section
|
||||
if filter_label and (group["label"].startswith("Filter ") or group["label"] == key):
|
||||
group["label"] = filter_label
|
||||
option = _option_from_link(label, query_value)
|
||||
existing = {item["source_value"] for item in group["options"]}
|
||||
if option["source_value"] not in existing:
|
||||
@@ -207,11 +235,14 @@ def _collect_named_inputs(node: _Node, groups: dict[str, dict[str, Any]]) -> Non
|
||||
min_name = next((name for name in RANGE_MIN_NAMES if name in names), "bpmin")
|
||||
max_name = next((name for name in RANGE_MAX_NAMES if name in names), "bpmax")
|
||||
label = _nearest_group_label(names.get(min_name) or names.get(max_name) or node) or "Preis"
|
||||
section = _section_label(names.get(min_name) or names.get(max_name) or node) or "Allgemein"
|
||||
groups["price"] = {
|
||||
"external_filter_key": "price",
|
||||
"label": "Preis" if "preis" not in label.lower() else label,
|
||||
"filter_type": FilterType.NUMERIC_RANGE.value,
|
||||
"canonical_unit": "EUR",
|
||||
"group_label": section,
|
||||
"group_sort": _group_sort(section),
|
||||
"options": [],
|
||||
"range": {"minimum": 0, "maximum": None, "step": 1, "unit": "EUR"},
|
||||
"encoding": {
|
||||
@@ -225,11 +256,14 @@ def _collect_named_inputs(node: _Node, groups: dict[str, dict[str, Any]]) -> Non
|
||||
if name not in names:
|
||||
continue
|
||||
label = _nearest_group_label(names[name]) or "Suchbegriffe"
|
||||
section = _section_label(names[name]) or "Allgemein"
|
||||
groups[name] = {
|
||||
"external_filter_key": name,
|
||||
"label": label,
|
||||
"filter_type": FilterType.TEXT.value,
|
||||
"canonical_unit": None,
|
||||
"group_label": section,
|
||||
"group_sort": _group_sort(section),
|
||||
"options": [],
|
||||
"encoding": {"url_param": name, "encoding_type": "TEXT_PARAMETER", "value_template": "{value}"},
|
||||
}
|
||||
@@ -243,6 +277,30 @@ def _find_inputs(node: _Node, found: list[_Node]) -> None:
|
||||
_find_inputs(child, found)
|
||||
|
||||
|
||||
def _node_xf_tokens(node: _Node) -> list[tuple[str, str]]:
|
||||
tokens = _xf_tokens(node.attrs.get("href") or "")
|
||||
if tokens:
|
||||
return tokens
|
||||
for attr in ("data-xf", "data-filter-value", "data-value", "value"):
|
||||
raw = node.attrs.get(attr) or ""
|
||||
if attr == "value" and node.attrs.get("name") not in {"xf", ""} and node.tag != "input":
|
||||
continue
|
||||
parsed = _xf_raw_token(raw)
|
||||
if parsed:
|
||||
return [parsed]
|
||||
return []
|
||||
|
||||
|
||||
def _xf_raw_token(raw: str) -> tuple[str, str] | None:
|
||||
value = unquote(raw).replace("|", "~").split("~")[0]
|
||||
if "_" not in value:
|
||||
return None
|
||||
key, token = value.split("_", 1)
|
||||
if key.isdigit() and token:
|
||||
return key, token.replace("+", " ")
|
||||
return None
|
||||
|
||||
|
||||
def _xf_tokens(href: str) -> list[tuple[str, str]]:
|
||||
if not href or href.startswith("#") or href.startswith("javascript:"):
|
||||
return []
|
||||
@@ -261,8 +319,7 @@ def _xf_tokens(href: str) -> list[tuple[str, str]]:
|
||||
|
||||
|
||||
def _option_from_link(label: str, query_value: str) -> dict[str, Any]:
|
||||
cleaned = re.sub(r"\(\d[\d.]*\s*(produkte)?\)\s*$", "", label, flags=re.IGNORECASE).strip()
|
||||
cleaned = re.sub(r"\s+", " ", cleaned)
|
||||
cleaned = PRODUCT_COUNT_RE.sub("", re.sub(r"\s+", " ", label)).strip()
|
||||
operator = None
|
||||
if cleaned.lower().startswith("ab "):
|
||||
operator = ">="
|
||||
@@ -299,6 +356,8 @@ def _empty_group(key: str, label: str | None, source_type: str | None) -> dict[s
|
||||
"label": label or f"Filter {key}",
|
||||
"filter_type": TYPE_MAP.get(source_type or "", FilterType.ENUM.value),
|
||||
"canonical_unit": None,
|
||||
"group_label": None,
|
||||
"group_sort": _group_sort("Allgemein"),
|
||||
"options": [],
|
||||
"encoding": {
|
||||
"url_param": "xf",
|
||||
@@ -375,18 +434,62 @@ def _usable_option(node: _Node, label: str, *, allow_histogram: bool) -> bool:
|
||||
return False
|
||||
if "bucket" in classes and not allow_histogram:
|
||||
return False
|
||||
cleaned = re.sub(r"\s+", " ", label).strip()
|
||||
cleaned = PRODUCT_COUNT_RE.sub("", re.sub(r"\s+", " ", label)).strip()
|
||||
if not cleaned or _skip_option(cleaned):
|
||||
return False
|
||||
if "überspringen" in cleaned.lower():
|
||||
if "überspringen" in cleaned.lower() or "aufklappen" in cleaned.lower() or "zuklappen" in cleaned.lower():
|
||||
return False
|
||||
if not allow_histogram and "produkte" in cleaned.lower():
|
||||
if cleaned.lower().startswith("weitere"):
|
||||
return False
|
||||
if len(cleaned.split()) > 8:
|
||||
if not allow_histogram and re.fullmatch(r"\d[\d.]*\s*produkte", cleaned, re.IGNORECASE):
|
||||
return False
|
||||
if len(cleaned.split()) > 12:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _section_label(node: _Node | None) -> str | None:
|
||||
current = node
|
||||
while current is not None:
|
||||
classes = current.attrs.get("class", "").lower()
|
||||
if "filter-row" in classes or current.attrs.get("data-filter-row-id"):
|
||||
text = _strip_section_suffix(
|
||||
current.attrs.get("data-filter-row-name")
|
||||
or current.attrs.get("aria-label")
|
||||
or _row_heading(current)
|
||||
)
|
||||
if text:
|
||||
return text
|
||||
labeled = current.attrs.get("aria-label") or ""
|
||||
if re.search(r"(auf|zu)klappen", labeled, re.IGNORECASE):
|
||||
text = _strip_section_suffix(labeled)
|
||||
if text:
|
||||
return text
|
||||
current = current.parent
|
||||
return None
|
||||
|
||||
|
||||
def _row_heading(node: _Node) -> str:
|
||||
for child in node.children:
|
||||
if isinstance(child, _Node) and child.tag in {"button", "h2", "h3", "h4", "legend", "strong"}:
|
||||
text = _clean_label(_node_text(child))
|
||||
if text:
|
||||
return text
|
||||
return _clean_label(node.attrs.get("aria-label") or "")
|
||||
|
||||
|
||||
def _strip_section_suffix(text: str | None) -> str:
|
||||
cleaned = _clean_label(text or "")
|
||||
return re.sub(r"\s*(aufklappen|zuklappen|einklappen)$", "", cleaned, flags=re.IGNORECASE).strip()
|
||||
|
||||
|
||||
def _group_sort(label: str) -> int:
|
||||
try:
|
||||
return GROUP_ORDER.index(label)
|
||||
except ValueError:
|
||||
return len(GROUP_ORDER)
|
||||
|
||||
|
||||
def _usable_group(group: dict[str, Any]) -> bool:
|
||||
if group["filter_type"] == FilterType.TEXT.value:
|
||||
return True
|
||||
|
||||
Reference in new issue
Block a user