Skip to content

Commit

Permalink
Hardcode Picard prefix. Loop on multiple queries to enlarge scope
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 10, 2024
1 parent 305103b commit a5af670
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions scripts/gdpr/create_prices_from_gdpr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,43 +151,62 @@ def gdpr_source_filter_rules(op_price_list, gdpr_source=""):
# the product_code is incomplete
# use Search-a-licious API to get the full product code
# and prompt the user to find the correct one
print("==================")
print(
"Input:",
"----- Input:",
op_price["product_code"],
op_price["product_name"],
op_price["price"],
)
response = requests.get(
OFF_SEARCHLICIOUS_API_ENDPOINT,
params={"q": f"code:*{op_price['product_code']}* brands:picard"},
)
if response.status_code == 200:
response_product_list = response.json()["hits"]
response_product_list = list(
filter(
lambda x: "picard" in x["brands_tags"],
response_product_list,
)
for q_index, q_params in enumerate(
[
f"code:3270160{op_price['product_code']}? brands:picard",
f"code:3270160{op_price['product_code']}?",
f"code:*{op_price['product_code']}? brands:picard",
f"code:*{op_price['product_code']}?&page_size=50",
]
):
response = requests.get(
OFF_SEARCHLICIOUS_API_ENDPOINT,
params={"q": q_params},
)
print("Products found:", len(response_product_list))
if len(response_product_list):
for index, response_product in enumerate(response_product_list):
print(
index + 1,
":",
response_product["code"],
response_product["product_name"],
)
user_choice_number = int(input("Which product ? Type 0 to skip. "))
if user_choice_number:
user_choice_product_code = response_product_list[
user_choice_number - 1
]["code"]
print("Chosen product code:", user_choice_product_code)
op_price["product_code"] = user_choice_product_code
else:
passes_test = False
print(response.url)
if response.status_code == 200:
response_product_count = response.json()["count"]
print("Products found:", response_product_count)
if response_product_count:
# confidence strong enough: take the first product
if (q_index < 2) and (response_product_count == 1):
op_price["product_code"] = response.json()["hits"][0][
"code"
]
else:
response_product_list = response.json()["hits"]
for index, response_product in enumerate(
response_product_list
):
print(
index + 1,
":",
response_product.get("code"),
response_product.get("product_name", ""),
response_product.get("brands_tags", ""),
response_product.get("stores", ""),
)
user_choice_number_str = input(
"Which product ? Type 0 to skip. Or provide the correct code. "
)
if len(user_choice_number_str) == 1:
user_choice_product_code = response_product_list[
int(user_choice_number_str) - 1
]["code"]
print("Chosen product code:", user_choice_product_code)
op_price["product_code"] = user_choice_product_code
elif 3 < len(user_choice_number_str) <= 13:
print("Chosen product code:", user_choice_number_str)
op_price["product_code"] = user_choice_number_str
else:
passes_test = False
break

if passes_test:
op_price_list_filtered.append(op_price)
Expand Down

0 comments on commit a5af670

Please sign in to comment.