Repository navigation
Expand file tree
/
Copy pathsearch.py
More file actions
65 lines (49 loc) · 2.1 KB
/
Copy pathsearch.py
File metadata and controls
65 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Search-result models, parsed from the ``search-page-results`` PML page."""
import json
import re
from .base import PicnicModel
from .pml import find_all
# ``sole_article_id`` is buried as a serialized string somewhere inside a tile.
_SOLE_ARTICLE_ID_PATTERN = re.compile(r'"sole_article_id":"(\w+)"')
class SearchResultItem(PicnicModel):
"""A single product tile in a search result.
Picnic's tile payload only carries ``display_price`` (the price shown on the
tile, in integer cents) — there is no separate ``price`` key — so that is the
field to read. ``price_ranges`` is populated instead of ``display_price`` for
some promotional tiles.
"""
id: str
name: str | None = None
display_price: int | None = None
image_id: str | None = None
unit_quantity: str | None = None
unit_quantity_sub: str | None = None
max_count: int | None = None
price_ranges: list | None = None
sole_article_id: str | None = None
decorators: list = []
class SearchResult(PicnicModel):
"""A search response: a collection of product tiles."""
items: list[SearchResultItem] = []
@classmethod
def from_page(cls, data: dict) -> "SearchResult":
"""Build a :class:`SearchResult` from a ``search-page-results`` payload.
Finds ``SELLING_UNIT_TILE`` nodes anywhere in the tree, merges each
node's ``sellingUnit`` payload with its ``sole_article_id``.
"""
body = data.get("body", {}) if isinstance(data, dict) else {}
nodes = find_all(
body.get("child", {}),
match={"type": "SELLING_UNIT_TILE", "sellingUnit": {}},
)
items: list[SearchResultItem] = []
for node in nodes:
fields = dict(node.get("sellingUnit", {}))
sole_ids = _SOLE_ARTICLE_ID_PATTERN.findall(json.dumps(node))
if sole_ids:
fields["sole_article_id"] = sole_ids[0]
item = SearchResultItem.model_validate(fields)
item.raw = node
items.append(item)
return cls(items=items, raw=data)
__all__ = ["SearchResult", "SearchResultItem"]