2018-07-24 14:27:37 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Author: freezed <freezed@users.noreply.github.com> 2018-07-24
|
|
|
|
Version: 0.1
|
|
|
|
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
Call OpenFF API to populate a local MariaDB/MySQL database with product data
|
|
|
|
This DB will serve an CLI client which gives alternative products with better
|
|
|
|
nurition grade.
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
|
2018-07-24 18:40:31 +00:00
|
|
|
# CONFIG
|
|
|
|
FIELD_KEPT = [
|
|
|
|
'product_name',
|
|
|
|
'stores',
|
|
|
|
'nutrition_grades',
|
2018-07-25 21:24:35 +00:00
|
|
|
'categories_tags'
|
2018-07-24 18:40:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-07-24 14:27:37 +00:00
|
|
|
def get_product(code):
|
|
|
|
"""
|
|
|
|
Call OpenFF API to get data of a single product
|
|
|
|
|
|
|
|
:Tests:
|
2018-07-25 17:22:01 +00:00
|
|
|
>>> prod_beurre = get_product('3017760000109')
|
|
|
|
>>> print(prod_beurre['product_name'])
|
2018-07-24 14:27:37 +00:00
|
|
|
Le Véritable Petit Beurre
|
|
|
|
|
2018-07-25 17:22:01 +00:00
|
|
|
>>> print(prod_beurre['stores'])
|
2018-07-24 14:27:37 +00:00
|
|
|
Super U
|
|
|
|
|
2018-07-25 17:22:01 +00:00
|
|
|
>>> print(prod_beurre['nutrition_grades'])
|
2018-07-24 14:27:37 +00:00
|
|
|
e
|
|
|
|
|
2018-07-25 17:22:01 +00:00
|
|
|
>>> print(prod_beurre['categories_tags'])
|
2018-07-24 14:27:37 +00:00
|
|
|
['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'fr:petits-beurres']
|
2018-07-25 17:22:01 +00:00
|
|
|
|
|
|
|
>>> prod_oreo = get_product('8410000810004')
|
2018-07-25 22:10:24 +00:00
|
|
|
>>> print(prod_oreo['code'])
|
|
|
|
8410000810004
|
|
|
|
|
2018-07-25 17:22:01 +00:00
|
|
|
>>> print(prod_oreo['product_name'])
|
|
|
|
Biscuit Oreo
|
|
|
|
|
|
|
|
>>> print(prod_oreo['stores'])
|
|
|
|
Cora,Irma.dk,Leader Price
|
|
|
|
|
|
|
|
>>> print(prod_oreo['nutrition_grades'])
|
|
|
|
e
|
|
|
|
|
|
|
|
>>> print(prod_oreo['categories_tags'])
|
|
|
|
['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'en:chocolate-biscuits', 'es:sandwich-cookies']
|
|
|
|
|
|
|
|
>>> prod_false = get_product('1664')
|
|
|
|
>>> prod_false is False
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> prod_string = get_product('string')
|
|
|
|
>>> prod_string is False
|
|
|
|
True
|
2018-07-24 14:27:37 +00:00
|
|
|
"""
|
|
|
|
|
2018-07-25 21:24:35 +00:00
|
|
|
try:
|
|
|
|
int(code)
|
2018-07-25 17:22:01 +00:00
|
|
|
|
2018-07-25 22:10:24 +00:00
|
|
|
except ValueError: # as except_detail:
|
2018-07-25 21:24:35 +00:00
|
|
|
# print("Exception: «{}»".format(except_detail))
|
|
|
|
return False
|
2018-07-24 18:40:31 +00:00
|
|
|
|
2018-07-25 21:24:35 +00:00
|
|
|
else:
|
|
|
|
response = requests.get(
|
|
|
|
"https://fr.openfoodfacts.org/api/v0/product/{}.json".format(code)
|
|
|
|
)
|
2018-07-25 22:10:24 +00:00
|
|
|
product_json = json.loads(response.text)
|
2018-07-25 21:24:35 +00:00
|
|
|
|
2018-07-25 22:10:24 +00:00
|
|
|
if product_json['status'] and response.status_code == 200:
|
|
|
|
product_kept = {'code': code}
|
2018-07-25 21:24:35 +00:00
|
|
|
|
|
|
|
for field in FIELD_KEPT:
|
2018-07-25 22:10:24 +00:00
|
|
|
product_kept[field] = product_json['product'][field]
|
2018-07-25 21:24:35 +00:00
|
|
|
|
2018-07-25 22:10:24 +00:00
|
|
|
return product_kept
|
2018-07-25 21:24:35 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
return False
|
2018-07-24 18:40:31 +00:00
|
|
|
|
|
|
|
|
2018-07-24 14:27:37 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
""" Starting doctests """
|
|
|
|
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|