Generates dict with prod & fields #4

- Modifies the storage of kept fields : need to keep more fields from a category
        than from a product FIELD_KEPT is turned into a dict like this :
        {'category':[fields, …], 'products':[fields, …]}
- separates online/offline tests
This commit is contained in:
Fred Z 2018-07-31 15:17:36 +02:00
parent 08f1227c01
commit 1c17468fb2
2 changed files with 46 additions and 40 deletions

View File

@ -20,9 +20,18 @@ DB_CONFIG = {
} }
# API # API
FIELD_KEPT = [ FIELD_KEPT = {
'product_name', 'product': [
'stores', 'product_name',
'nutrition_grades', 'nutrition_grades',
'categories_tags' 'categories_tags'
] ],
'category': [
'_id',
'url',
'product_name',
'nutrition_grades',
'categories_tags'
]
}

View File

@ -12,6 +12,7 @@ nurition grade.
""" """
import json import json
import requests import requests
# import pprint
from config import FIELD_KEPT from config import FIELD_KEPT
@ -19,41 +20,37 @@ def get_product(code):
""" """
Call OpenFF API to get data of a single product Call OpenFF API to get data of a single product
:Tests: :Tests ONLINE:
>>> prod_beurre = get_product('3017760000109') >>> prod_beurre = get_product('3017760000109')
>>> prod_oreo = get_product('8410000810004')
>>> prod_false = get_product('1664')
>>> prod_string = get_product('string')
:Tests OFFLINE:
>>> print(prod_beurre['product_name']) >>> print(prod_beurre['product_name'])
Le Véritable Petit Beurre Le Véritable Petit Beurre
>>> print(prod_beurre['stores'])
Super U
>>> print(prod_beurre['nutrition_grades']) >>> print(prod_beurre['nutrition_grades'])
e e
>>> print(prod_beurre['categories_tags']) >>> print(prod_beurre['categories_tags'])
['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'fr:petits-beurres'] ['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'fr:petits-beurres']
>>> prod_oreo = get_product('8410000810004')
>>> print(prod_oreo['code']) >>> print(prod_oreo['code'])
8410000810004 8410000810004
>>> print(prod_oreo['product_name']) >>> print(prod_oreo['product_name'])
Biscuit Oreo Biscuit Oreo
>>> print(prod_oreo['stores'])
Cora,Irma.dk,Leader Price
>>> print(prod_oreo['nutrition_grades']) >>> print(prod_oreo['nutrition_grades'])
e e
>>> print(prod_oreo['categories_tags']) >>> print(prod_oreo['categories_tags'])
['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'en:chocolate-biscuits', 'es:sandwich-cookies'] ['en:sugary-snacks', 'en:biscuits-and-cakes', 'en:biscuits', 'en:chocolate-biscuits', 'es:sandwich-cookies']
>>> prod_false = get_product('1664')
>>> prod_false is False >>> prod_false is False
True True
>>> prod_string = get_product('string')
>>> prod_string is False >>> prod_string is False
True True
""" """
@ -74,7 +71,7 @@ def get_product(code):
if product_json['status'] and response.status_code == 200: if product_json['status'] and response.status_code == 200:
product_kept = {'code': code} product_kept = {'code': code}
for field in FIELD_KEPT: for field in FIELD_KEPT['product']:
product_kept[field] = product_json['product'][field] product_kept[field] = product_json['product'][field]
return product_kept return product_kept
@ -87,24 +84,22 @@ def get_category(name, from_file=False):
""" """
Call OpenFF API to get data of products in a single category Call OpenFF API to get data of products in a single category
:return: Dict filled with products & kept fields
First try, TODO : First try, TODO :
- work offline - work offline with local JSON
- need to get all the products of a category - need to get all the products of a category
- need to keep more fields from a category than from a product, maybe
FIELD_KEPT should be turned into a dict like this :
{'category':[fields, ], 'products':[fields, ]}
:Tests:
# >>> prod_bisc = get_category('biscuits')
>>> prod_bisc = get_category('biscuits', True)
>>> 'count' in prod_bisc
True
:Tests ONLINE:
>>> prod_false = get_category('1664') >>> prod_false = get_category('1664')
>>> prod_false >>> prod_false
False False
>>> 'stores' in prod_bisc['products'][0] # >>> prod_bisc = get_category('biscuits')
:Tests OFFLINE:
>>> prod_bisc = get_category('biscuits', True)
>>> 'count' in prod_bisc
True True
>>> 'product_name' in prod_bisc['products'][0] >>> 'product_name' in prod_bisc['products'][0]
@ -116,15 +111,11 @@ def get_category(name, from_file=False):
>>> 'categories_tags' in prod_bisc['products'][0] >>> 'categories_tags' in prod_bisc['products'][0]
True True
# >>> 'countries' in prod_bisc['products'][0]
# True
# >>> 'id' in prod_bisc['products'][0]
# True
>>> get_category('wrong_file', True) >>> get_category('wrong_file', True)
File load error : sample-category-wrong_file.json File load error : sample-category-wrong_file.json
False False
# >>> pprint.pprint(prod_bisc)
""" """
if from_file: if from_file:
@ -134,7 +125,7 @@ def get_category(name, from_file=False):
# File did not exists # File did not exists
if path.isfile(filename) is False: if path.isfile(filename) is False:
print("File load error : {}".format(filename)) print("File load error : {}".format(filename))
status = False status = 404
category_json = {'count': 0} category_json = {'count': 0}
else: else:
@ -151,11 +142,19 @@ def get_category(name, from_file=False):
if category_json['count'] is not 0 and status == 200: if category_json['count'] is not 0 and status == 200:
category_kept = { category_kept = {
'count': category_json['count'], 'count': category_json['count'],
'products': [{}] 'products': []
} }
for field in FIELD_KEPT: for idx, product_fields in enumerate(category_json['products']):
category_kept['products'][0][field] = category_json['products'][0][field] category_kept['products'].append(dict())
for field in FIELD_KEPT['category']:
if field in product_fields:
category_kept['products'][idx][field] = product_fields[field]
else:
category_kept['products'][idx][field] = False
return category_kept return category_kept
@ -164,7 +163,5 @@ def get_category(name, from_file=False):
if __name__ == "__main__": if __name__ == "__main__":
""" Starting doctests """
import doctest import doctest
doctest.testmod() doctest.testmod()