forked from TandoorRecipes/open-tandoor-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_data.py
133 lines (109 loc) · 3.63 KB
/
update_data.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import os
import sys
import requests
from utils import save_data
TANDOOR_API_KEY = os.getenv('TANDOOR_API_KEY', '')
REQUEST_HEADERS = {
'Authorization': f'Bearer {TANDOOR_API_KEY}'
}
ENDPOINTS = [
'open-data-unit',
'open-data-property',
'open-data-category',
'open-data-store',
'open-data-food',
'open-data-conversion',
]
def get_unit_object(e):
return {
'name': e['name'],
'plural_name': e['plural_name'],
'type': e['type'],
'base_unit': e['base_unit'],
'comment': e['comment'],
}
def get_property_object(e):
return {
'name': e['name'],
'unit': e['unit'],
'comment': e['comment'],
}
def get_category_object(e):
return {
'name': e['name'],
'comment': e['comment'],
}
def get_food_object(e):
properties = {
'food_amount': e['properties_food_amount'],
'food_unit': e['properties_food_unit']['slug'],
'source': e['properties_source'],
'type_values': [],
}
for p in e['properties']:
properties['type_values'].append({
'property_type': p['property']['slug'],
'property_value': p['property_amount'],
})
return {
'name': e['name'],
'plural_name': e['plural_name'],
'store_category': e['store_category']['slug'],
# 'preferred_unit_metric': e['preferred_unit_metric']['slug'],
# 'preferred_packaging_unit_metric': e['preferred_shopping_unit_metric']['slug'],
# 'preferred_unit_imperial': e['preferred_unit_imperial']['slug'],
# 'preferred_packaging_unit_imperial': e['preferred_shopping_unit_imperial']['slug'],
'properties': properties,
'fdc_id': e['fdc_id'],
'comment': e['comment'],
}
def get_conversion_object(e):
return {
'food': e['food']['slug'],
'base_amount': e['base_amount'],
'base_unit': e['base_unit']['slug'],
'converted_amount': e['converted_amount'],
'converted_unit': e['converted_unit']['slug'],
'source': e['source'],
'comment': e['comment'],
}
def get_store_object(e):
categories = []
for c in e['category_to_store']:
categories.append(c['category']['slug'])
return {
'name': e['name'],
'categories': categories,
'comment': e['comment'],
}
def update_data():
for endpoint in ENDPOINTS:
object_type = endpoint.replace('open-data-', '')
response = requests.get(f'https://app.tandoor.dev/api/{endpoint}/', headers=REQUEST_HEADERS)
type_data = {}
json_response = json.loads(response.content)
for e in json_response:
version = e['version']['code']
if version not in type_data:
type_data[version] = {'data': {}}
parsed_object = None
if object_type == 'unit':
parsed_object = get_unit_object(e)
if object_type == 'property':
parsed_object = get_property_object(e)
if object_type == 'category':
parsed_object = get_category_object(e)
if object_type == 'store':
parsed_object = get_store_object(e)
if object_type == 'food':
parsed_object = get_food_object(e)
if object_type == 'conversion':
parsed_object = get_conversion_object(e)
type_data[version]['data'][e['slug']] = parsed_object
for v in list(type_data.keys()):
save_data(object_type, type_data[v]['data'], language=v)
if TANDOOR_API_KEY == '':
print('Missing Tandoor API Key')
sys.exit(1)
update_data()