-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
67 lines (57 loc) · 2.03 KB
/
Copy pathapi.py
File metadata and controls
67 lines (57 loc) · 2.03 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
"""API openfoodfact"""
import requests
from constant import CATEGORIES_COUNT, PAGE_COUNT
from products import Product
class API:
"""API"""
categories = []
products = []
@staticmethod
def load():
"""loading data"""
API.clear()
API.__get_cat()
API.__get_products()
@staticmethod
def clear():
"""clear all data"""
API.categories.clear()
API.products.clear()
@staticmethod
def __get_cat():
print("Récupération des catégories... ")
data = requests.get("https://fr.openfoodfacts.org/categories.json")
data = data.json()
value = CATEGORIES_COUNT
for cat in data["tags"]:
API.categories.append(cat)
value -= 1
if value <= 0:
print("Done")
return
print("Done")
@staticmethod
def __get_products():
"""get data from openfoodfact API"""
i = 0
for categories_values in API.categories:
print("Récupération des produits " + str(i+1) + " / " + str(len(API.categories)))
for page in range(1, PAGE_COUNT + 1):
data = requests.get(categories_values["url"] + "/" + str(page) + ".json")
data_json = data.json()
products_data_json = data_json["products"]
for prod in products_data_json:
product = Product([
prod.get("product_name_fr", ""),
prod.get("url"),
prod.get("stores", ""),
prod.get("nutriscore_grade", "X"),
i + 1
])
if prod.get("product_name_fr", "") != "" \
and prod.get("generic_name_fr", "") != "unknown" \
and prod.get("nutriscore_grade", "X") != "X" \
and prod.get("stores", "") != "":
API.products.append(product)
i += 1
print("Done")