-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
78 lines (70 loc) · 2.84 KB
/
api.py
File metadata and controls
78 lines (70 loc) · 2.84 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
import requests
from table.category import Category
from table.product import Product
"""
API connection data
"""
post_data = {"name": "Theotim", "username": "theodrem", "password": "Intelligence97"}
responses = requests.get("https://fr.openfoodfacts.org/categories.json", data=post_data)
current = responses.json()
def update_product_attr(obj, dico):
"""
Attributes the keys and values as parameters to the object.
"""
for key, value in dico.items():
setattr(obj, key, value)
def insert_data_from_openfoodfacts():
"""
Get the first fifteen categories and add them to our database
"""
for category in range(15):
name_category = current["tags"][category]["name"]
try:
cat = Category.get_object("name", name_category)
except Category.ObjectDoesNotExist:
cat = Category(name_category)
cat.save()
"""
Get the first hundred categories and add them to our database
"""
for page in range(5):
url_category = "%s/%s.json" % (current["tags"][category]["url"], page + 1)
products = requests.get(url_category)
result = products.json()
try:
name = result["products"][page]["product_name"]
url = result["products"][page]["url"]
description = result["products"][page]["ingredients_text_fr"]
nutriscore = result["products"][page]["nutrition_grades"]
store = result["products"][page]["stores"]
openfoodfact_id = result["products"][page]["id"]
categories = Category.select_all_where("name", name_category)
category_id = 0
for cat in categories:
category_id = cat.id
if (
name != ""
and url != ""
and nutriscore != ""
and store != ""
and openfoodfact_id != ""
): # If a data is not complete, we do not retrieve it
try:
product = Product.get_object("openfoodfact_id", openfoodfact_id)
except Product.ObjectDoesNotExist:
product = Product()
update_product_attr(
product,
{
"name": name,
"nutriscore": nutriscore,
"url": url,
"store": store,
"description": description,
"openfoodfact_id": openfoodfact_id,
"category_id": category_id,
},
)
product.save()
except (IndexError, KeyError, ValueError):
pass