8000 Some results were missing from search due to the "if sole_article_ids… · codesalatdev/python-picnic-api@7e07967 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e07967

Browse files
committed
Some results were missing from search due to the "if sole_article_ids" condition. This commit includes items that don't have a sole article id, and just sets the sole article id to None.
Also includes some minor reformatting and refactor (updated type hint Optional for functions that can return a None).
1 parent 9dc1f50 commit 7e07967

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

python_picnic_api/helper.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import re
3+
from typing import List, Dict, Any, Optional
34

45
# prefix components:
56
space = " "
@@ -11,6 +12,9 @@
1112
IMAGE_SIZES = ["small", "medium", "regular", "large", "extra-large"]
1213
IMAGE_BASE_URL = "https://storefront-prod.nl.picnicinternational.com/static/images"
1314

15+
SOLE_ARTICLE_ID_PATTERN = re.compile(r"sole_article_id=([0-9]+)")
16+
17+
1418
def _tree_generator(response: list, prefix: str = ""):
1519
"""A recursive tree generator,
1620
will yield a visual tree structure line by line
@@ -38,8 +42,8 @@ def _url_generator(url: str, country_code: str, api_version: str):
3842
return url.format(country_code.lower(), api_version)
3943

4044

41-
def _get_category_id_from_link(category_link: str) -> str:
42-
pattern = r'categories/(\d+)'
45+
def _get_category_id_from_link(category_link: str) -> Optional[str]:
46+
pattern = r"categories/(\d+)"
4347
first_number = re.search(pattern, category_link)
4448
if first_number:
4549
result = str(first_number.group(1))
@@ -48,55 +52,54 @@ def _get_category_id_from_link(category_link: str) -> str:
4852
return None
4953

5054

51-
def _get_category_name(category_link: str, categories: list) -> str:
55+
def _get_category_name(category_link: str, categories: list) -> Optional[str]:
5256
category_id = _get_category_id_from_link(category_link)
5357
if category_id:
54-
category = next((item for item in categories if item["id"] == category_id), None)
58+
category = next(
59+
(item for item in categories if item["id"] == category_id), None
60+
)
5561
if category:
5662
return category["name"]
5763
else:
5864
return None
5965
else:
6066
return None
6167

68+
6269
def get_recipe_image(id: str, size="regular"):
6370
sizes = IMAGE_SIZES + ["1250x1250"]
6471
assert size in sizes, "size must be one of: " + ", ".join(sizes)
6572
return f"{IMAGE_BASE_URL}/recipes/{id}/{size}.png"
6673

6774

6875
def get_image(id: str, size="regular", suffix="webp"):
69-
assert "tile" in size if suffix == "webp" else True, (
70-
"webp format only supports tile sizes"
71-
)
76+
assert (
77+
"tile" in size if suffix == "webp" else True
78+
), "webp format only supports tile sizes"
7279
assert suffix in ["webp", "png"], "suffix must be webp or png"
7380
sizes = IMAGE_SIZES + [f"tile-{size}" for size in IMAGE_SIZES]
7481

75-
assert size in sizes, (
76-
"size must be one of: " + ", ".join(sizes)
77-
)
82+
assert size in sizes, "size must be one of: " + ", ".join(sizes)
7883
return f"{IMAGE_BASE_URL}/{id}/{size}.{suffix}"
7984

8085

81-
def _extract_search_results(raw_results: dict) -> list:
86+
def _extract_search_results(raw_results: Dict[str, Any]) -> List[Dict[str, Any]]:
87+
"""Extract search results from a nested dictionary structure returned by Picnic search."""
8288
search_results = []
83-
sole_article_id_pattern = re.compile(r"sole_article_id=([0-9]+)")
84-
85-
# Iterate over the nested structure of raw_results
86-
for child1 in raw_results.get("body", {}).get("children", []):
87-
for child2 in child1.get("children", []):
88-
content = child2.get("content")
89-
if content and "selling_unit" in content:
90-
# Extracting the sole_article_id from the serialized JSON of pml
91-
sole_article_ids = sole_article_id_pattern.findall(
92-
json.dumps(child2.get("pml", {}))
89+
90+
for section in raw_results.get("body", {}).get("children", []):
91+
for item in section.get("children", []):
92+
content = item.get("content", {})
93+
if "selling_unit" in content:
94+
sole_article_ids = SOLE_ARTICLE_ID_PATTERN.findall(
95+
json.dumps(item.get("pml", {}))
9396
)
94-
if sole_article_ids:
95-
sole_article_id = sole_article_ids[0]
96-
# Create and append the result entry
97-
result_entry = {
98-
**content["selling_unit"],
99-
"sole_article_id": sole_article_id,
100-
}
101-
search_results.append(result_entry)
97+
sole_article_id = sole_article_ids[0] if sole_article_ids else None
98+
99+
result_entry = {
100+
**content["selling_unit"],
101+
"sole_article_id": sole_article_id,
102+
}
103+
search_results.append(result_entry)
104+
102105
return search_results

0 commit comments

Comments
 (0)
0