-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnurtiQuery.py
More file actions
112 lines (88 loc) · 3.74 KB
/
nurtiQuery.py
File metadata and controls
112 lines (88 loc) · 3.74 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import openfoodfacts
import json
import pandas as pd
# User-Agent is mandatory
api = openfoodfacts.API(user_agent="Grocerly - Python - Version 1.0 ")
def fetch_data_from_api(query):
"""
Fetches data from the Open Food Facts API based on the provided query.
Parameters:
- query (str): The search query to find relevant products.
Returns:
- json_data of nutrients
"""
query_data = api.product.text_search(query).get("products")
print(query_data[0])
json_data = extract_nutrient_data(query_data)
if int(json_data["nutrients"]["nova-group"]) != 1:
recommend_healthy_suggestions(json_data, int(json_data["nutrients"]["nova-group"]), 3)
return json_data
def extract_nutrient_data(api_obj):
"""
Extracts nutrient data from the API response and constructs a JSON object.
Parameters:
- api_obj (dict): The API response containing product data.
Returns:
dict: A JSON object containing extracted nutrient data.
"""
allergens = api_obj[0].get("allergens")
nutrients = api_obj[0].get("nutriments")
json_data = {
"name": api_obj[0].get("abbreviated_product_name"),
"categories": [],
"nutrients": {
"nova-group": nutrients["nova-group"],
"nutrition-score-fr_100g": nutrients["nutrition-score-fr_100g"],
"proteins_100g": nutrients["proteins_100g"],
"saturated-fat_100g": nutrients["saturated-fat_100g"],
"fat_100g": nutrients["fat_100g"],
"energy_100g": nutrients["energy_100g"],
"carbohydrates_100g": nutrients["carbohydrates_100g"]
},
"allergens": allergens
}
for i in range(3):
if i < len(api_obj[0].get("categories_hierarchy")):
json_data["categories"].append(api_obj[0].get("categories_hierarchy")[i])
optional_nutrients = ["salt_100g", "sugars_100g", "ph_100g"]
for nutrient in optional_nutrients:
if nutrient in nutrients:
json_data["nutrients"][nutrient] = nutrients[nutrient]
json_string = json.dumps(json_data)
print(json_string)
return json_data
def recommend_healthy_suggestions(json_data, nova_group, num_suggestions):
"""
Recommends healthy food suggestions based on nutrient data.
Parameters:
- json_data (dict): JSON object containing nutrient data.
- nova_group (int): Nova group value for the product.
- num_suggestions (int): Number of healthy suggestions to recommend.
Returns:
None
"""
print("RECOMMENDATIONS (based on nova-group):")
for category in json_data["categories"]:
category = category.strip("en:")
query_data = api.product.text_search(category).get("products")
df = pd.DataFrame(query_data)
filtered_df = df[df["nutriments"].apply(lambda x: "nova-group" in x and x["nova-group"] < nova_group)]
item = min(num_suggestions, len(filtered_df))
for i in range(item):
data = filtered_df.iloc[i]
nutrients = data["nutriments"]
recommendation = {
"name": data["product_name"],
"nutrients": {
"nova-group": nutrients["nova-group"],
"nutrition-score-fr_100g": nutrients["nutrition-score-fr_100g"],
"proteins_100g": nutrients["proteins_100g"],
"saturated-fat_100g": nutrients["saturated-fat_100g"],
"fat_100g": nutrients["fat_100g"],
"energy_100g": nutrients["energy_100g"],
"carbohydrates_100g": nutrients["carbohydrates_100g"]
},
}
print(recommendation)
# Example usage
fetch_data_from_api("cookie")