Skip to content

Commit

Permalink
Move picard code to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 12, 2024
1 parent 0890a2e commit 6ccef2e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion scripts/gdpr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See https://wiki.openfoodfacts.org/GDPR_request
|Carrefour |1 file with 2 tabs|- merge files<br/>- skip discounts|
|E.Leclerc |2 files |- merge files|
|Intermarché|1 single file ||
|Picard |1 file with multiple tables||
|Picard |1 file with multiple tables|- create seperate files<br>- merge files|

## Usage

Expand Down
72 changes: 6 additions & 66 deletions scripts/gdpr/create_prices_from_gdpr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time

import requests
from utils import get_picard_product_from_subcode

OPEN_PRICES_CREATE_PRICE_ENDPOINT = f'{os.environ.get("API_ENDPOINT")}/prices'
OPEN_PRICES_TOKEN = os.environ.get("API_TOKEN")
Expand Down Expand Up @@ -37,12 +38,6 @@
# DRY_MODE
]

OFF_SEARCHLICIOUS_API_ENDPOINT = (
"https://search.openfoodfacts.org/search" # ?q=code:*81714* brands:picard
)

PICARD_GS1_PREFIX = "327016"


def gdpr_source_field_cleanup_rules(gdpr_source, op_field, gdpr_field_value):
"""
Expand Down Expand Up @@ -150,66 +145,11 @@ def gdpr_source_filter_rules(op_price_list, gdpr_source=""):
elif gdpr_source == "INTERMARCHE":
pass
elif gdpr_source == "PICARD":
# 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(
"----- Input:",
op_price["product_code"],
op_price["product_name"],
op_price["price"],
)
for q_index, q_params in enumerate(
[
f"code:{PICARD_GS1_PREFIX}?{op_price['product_code']}? brands:picard",
f"code:{PICARD_GS1_PREFIX}?{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(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:
print("Product not found...")
passes_test = False
break
full_product_code = get_picard_product_from_subcode(op_price)
if full_product_code:
op_price["product_code"] = full_product_code
else:
passes_test = False

if passes_test:
op_price_list_filtered.append(op_price)
Expand Down
68 changes: 68 additions & 0 deletions scripts/gdpr/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import requests

OFF_SEARCHLICIOUS_API_ENDPOINT = "https://search.openfoodfacts.org/search"
PICARD_GS1_PREFIX = "327016"


def get_picard_product_from_subcode(op_price_dict):
# the Picard product_code is incomplete
# use Search-a-licious API to get the full product code
# if needed, prompt the user to select the correct one
passes_test = True
full_product_code = None

print(
"----- Input:",
op_price_dict["product_code"],
op_price_dict["product_name"],
op_price_dict["price"],
)
for q_index, q_params in enumerate(
[
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}? brands:picard",
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}?",
f"code:*{op_price_dict['product_code']}? brands:picard",
f"code:*{op_price_dict['product_code']}?&page_size=50",
]
):
response = requests.get(
OFF_SEARCHLICIOUS_API_ENDPOINT,
params={"q": q_params},
)
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):
full_product_code = response.json()["hits"][0]["code"]
else:
# multiple results: prompt the user to select
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:
full_product_code = response_product_list[
int(user_choice_number_str) - 1
]["code"]
print("Chosen product code:", full_product_code)
elif 3 < len(user_choice_number_str) <= 13:
full_product_code = user_choice_number_str
print("Chosen product code:", full_product_code)
else:
print("Product not found...")
passes_test = False
break

return passes_test, full_product_code

0 comments on commit 6ccef2e

Please sign in to comment.