-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusda_db_tool.py
415 lines (381 loc) · 12.5 KB
/
usda_db_tool.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from fuzzywuzzy import fuzz, process
import json
import requests
import sqlite3
class Client:
"""
The API client with all 4 endpoints (ep) to the USDA NDB API: 1) food_report, 2) list, 3) nutrients, 4) search
For more information on the request parameters and response body, read the documentation of the USDA NDB API at:
https://ndb.nal.usda.gov/ndb/api/doc
Alternatively, full db dumps are available in ASCII and Microsoft Access files at:
https://www.ars.usda.gov/northeast-area/beltsville-md/beltsville-human-nutrition-research-center/nutrient-data-laboratory/docs/sr28-download-files/
"""
BASE_URL = 'https://api.nal.usda.gov/ndb'
FORMAT = 'json'
def __init__(self, api_key):
self.api_key = api_key
def food_report(self, ndbno, type='f', **kwargs):
"""
Return a list of USDA nutrient (USDA food ID, USDA nutrient ID, value per 100g, unit) tuples,
API documentation for the food_report endpoint, including parameters can be found at:
https://ndb.nal.usda.gov/ndb/doc/apilist/API-FOOD-REPORTV2.md
"""
kwargs.update({'ndbno': ndbno, 'type': type})
data = self._call('V2/reports', **kwargs)
nutrient_ids_values = [(ndbno,
data['foods'][0]['food']['nutrients'][i]['nutrient_id'],
data['foods'][0]['food']['nutrients'][i]['value'])
for i in range(len(data['foods'][0]['food']['nutrients']))]
return nutrient_ids_values
def list(self, **kwargs):
return self._call('list', **kwargs)
def nutrients(self, **kwargs):
return self._call('nutrients', **kwargs)
def search(self, q, ds='Standard Reference', sort='n', originalq=None, **kwargs):
"""
Return selected food's USDA food database ID (ndbno).
API documentation for the search endpoint, including parameters can be found at:
https://ndb.nal.usda.gov/ndb/doc/apilist/API-SEARCH.md
"""
kwargs.update({'ds': ds, 'q': q, 'sort': sort})
data = self._call('search', **kwargs)
def prompt(top_choices):
try:
match = int(input('\n Please select the matching food for {0} (0-4):'.format(q)))
# get ndbo code associated with the fuzzy-matched food
#if match is last in list, rerun choices
i = top_choices[match][0][1]
ndbno = (data['list']['item'][i]['ndbno'])
if originalq:
return (ndbno, originalq)
return (ndbno, q)
except:
print('Please enter a valid input. Input must be a number 0-{0}'.format(len(top_choices)))
return prompt(top_choices)
def choices():
# fuzzy match food name
try:
choices = [(data['list']['item'][i]['name'], i) for i in range(len(data['list']['item']))]
top_choices = process.extract(q, choices, scorer=fuzz.token_sort_ratio, limit=20)
for i, option in enumerate(top_choices):
print(i, option[0][0])
# print(len(top_choices), 'more options')
return prompt(top_choices)
except TypeError as e:
if 'NoneType' in str(e):
print('Call failed. Please confirm that your API is correct and that your parameter inputs are allowed.'
'You entered the following parameters: ', kwargs,
'.\nSee the documentation at https://ndb.nal.usda.gov/ndb/doc/apilist/API-SEARCH.md'
'for a list of accepted parameters. ')
except (IndexError, KeyError):
print("No search results found for: {0}.".format(q))
replacement_y_n = input('\n Would you like to try a replacement term? (Y/N): ')
if replacement_y_n.lower() in ['y', 'yes']:
replacement_term = input('\n What new term would you like to use?: ')
if originalq:
return client.search(replacement_term, originalq=originalq)
else:
return client.search(replacement_term, originalq=q)
return choices()
def _call(self, ep, **kwargs):
url = '{0}/{1}'.format(self.BASE_URL, ep)
kwargs.update({'format': self.FORMAT, 'api_key': self.api_key})
try:
response = requests.get(url, params=kwargs)
if response.status_code != 200:
raise requests.exceptions.HTTPError
odict_response = json.loads(response.text)
return odict_response
except requests.exceptions.RequestException as e:
print('Uh oh: ', e)
def add_food(self, ndbno_term):
if ndbno_term is not None:
ndbno = ndbno_term[0]
term = ndbno_term[1]
with sqlite3.connect('food_db.db') as conn:
c = conn.cursor()
try:
c.execute('INSERT INTO usda_food_mapping (usda_food_id, food_name) VALUES (?, ?)', (ndbno, term))
except sqlite3.IntegrityError as e:
if 'usda_food_mapping.food_name' in str(e):
c.execute('UPDATE usda_food_mapping SET usda_food_id = ? WHERE food_name = ?', (ndbno, term))
print('Food ID updated')
elif 'usda_food_mapping.usda_food_id' in str(e):
c.execute('UPDATE usda_food_mapping SET food_name = ? WHERE usda_food_id = ?', (term, ndbno))
print('Food name updated')
else:
raise e
def add_food_nutrients(self, nutrient_ids_values):
with sqlite3.connect('food_db.db') as conn:
c = conn.cursor()
for i in nutrient_ids_values:
try:
# todo If ndbno associated with a food's name is changed, this will succeed, when it should...
# todo ...update the previous entry based on the food's name.
c.execute('INSERT INTO food_nutrient (usda_food_id, usda_nutrient_id, nutrient_value)'
'VALUES (?, ?, ?)', (i[0], i[1], i[2]))
except sqlite3.IntegrityError:
c.execute('UPDATE food nutrient '
'SET nutrient_value = ?,'
'WHERE usda_food_id = ?, usda_nutrient_id = ?', (i[2], i[0], i[1]))
print('Food\'s nutrient values updated')
if __name__ == "__main__":
client = Client('RTSEQFwKOVfLzueKSOLQpzydvlVoOkqIyBIUcaDo')
# your code goes here
l = ['Agave Nectar', 'Almond', 'Almond Milk', 'Anchovy',
'Apple',
'Apple Juice',
'Apricot',
'Artichokes',
'Asparagus',
'Aspartame',
'Aubergine (Eggplant)',
'Avocado',
'Bamboo Shoots',
'Bananas',
'Barley',
'Basil',
'Beef',
'Beer - Ale',
'Beer - Gluten Free',
'Beer - Lager',
'Beer - Non Alcoholic',
'Beer - Stout',
'Beetroot',
'Black Bean',
'Blackberries',
'Blackcurrants',
'Blueberries',
'Brandy',
'Brazil Nuts',
'Broad Beans',
'Broccoli',
'Brown Sugar',
'Brussels Sprouts',
'Buckwheat',
'Butter',
'Cabbage, Red',
'Cabbage, White',
'Capers',
'Carrot Juice',
'Carrots',
'Cashews',
'Cauliflower',
'Celery',
'Chard',
'Cheese - Goats',
'Cheese - Hard',
'Cheese - Other',
'Cheese - Soft',
'Cherries',
'Chestnuts',
'Chia Seed',
'Chicken',
'Chicken Egg',
'Chickpeas',
'Chives',
'Chocolate - Dark',
'Chocolate - Milk',
'Chocolate - White',
'Cider',
'Coconut',
'Coconut Milk',
'Coconut Water',
'Cod',
'Coffee',
'Coffee - Decaffinated',
'Cola',
'Coriander',
'Corn Syrup',
'Courgette (Zucchini)',
'Cous Cous',
'Cow Milk - Lactose Free',
'Cow Milk - Semi-Skimmed',
'Cow Milk - Skimmed',
'Cow Milk - Whole',
'Crab',
'Cranberries',
'Cranberry Juice',
'Crayfish',
'Cream',
'Cucumber',
'Dairy Free Spread',
'Dates',
'Duck',
'Duck Egg',
'Elderberries',
'Elderflower',
'Energy Drink',
'Figs',
'Filtered Water',
'Flax Seed',
'Fructose',
'Garlic',
'Gin',
'Ginger',
'Ginger Beer',
'Goat Milk',
'Golden Syrup',
'Goose',
'Goose Egg',
'Gooseberries',
'Grape Juice',
'Grapefruit',
'Grapefruit Juice',
'Grapes',
'Haddock',
'Hake',
'Hazelnut',
'Hazelnut Milk',
'Herring',
'Honey',
'Hot Chocolate',
'Ice Cream',
'Kale',
'Kidney Beans',
'Kippers',
'Kiwi',
'Lamb',
'Leeks',
'Lemon',
'Lemonade',
'Lentils',
'Lettuce',
'Lime',
'Liqueur - Coffee',
'Liqueur - Cream',
'Liqueur - Fruit',
'Liqueur - Herbal',
'Liqueur - Whisky',
'Loganberries',
'Macadamia',
'Mackerel',
'Maize (Corn)',
'Mango',
'Maple Syrup',
'Melon, Cantaloupe',
'Melon, Honeydew',
'Monkfish',
'Mushroom',
'Mussels',
'Nectarine',
'Oats',
'Olive Oil Spread',
'Olives',
'Onions',
'Orange',
'Orange Juice (with bits)',
'Orange Juice (without bits)',
'Oysters',
'Pak Choi',
'Parsley',
'Parsnips',
'Passion Fruit',
'Passion Fruit Juice',
'Peach',
'Peanuts',
'Pear',
'Peas',
'Pecans',
'Peppers (Capsicum)',
'Peppers, Chili',
'Pine nut',
'Pineapple',
'Pineapple Juice',
'Pistachio',
'Plaice',
'Plums',
'Pollock',
'Pomegranate',
'Pomegranate Juice',
'Poppy',
'Pork',
'Potatoes, White',
'Prawns',
'Prunes',
'Pumpkin',
'Pumpkin Seed',
'Quail Egg',
'Quinoa',
'Quorn',
'Rabbit',
'Radish',
'Raisins',
'Raspberries',
'Redcurrants',
'Rhubarb',
'Rice Milk',
'Rice, Brown',
'Rice, White',
'Rice, Wild',
'Rum',
'Runner Beans',
'Rye',
'Saccharin',
'Salmon',
'Sardines',
'Scallops',
'Sea Bass',
'Sea Bream',
'Seaweed',
'Sesame',
'Shrimp',
'Soda Water',
'Sole',
'Soy Nuts',
'Soya Milk',
'Soybeans',
'Spinach',
'Squash',
'Squash (drink)',
'Squid',
'Star Fruit',
'Stevia',
'Strawberries',
'Sucralose',
'Sucrose',
'Sugar',
'Sunflower Seeds',
'Sweet Potato',
'Tangerine',
'Tap Water',
'Tea - Decaffinated',
'Tea - Fruit',
'Tea - Green',
'Tea - Herbal',
'Tequila',
'Tofu',
'Tomato Juice',
'Tomatoes',
'Tonic Water',
'Treacle',
'Trout',
'Tuna',
'Turkey',
'Turnips',
'Veal',
'Vegetable Spread',
'Venison',
'Vodka',
'Walnut',
'Water Chestnut',
'Watercress',
'Watermelon',
'Wheat',
'Whiskey',
'Wine - Champagne',
'Wine - Pink',
'Wine - Red',
'Wine - Sparkling',
'Wine - White',
'Yoghurt - Plain']
skipped = []
for term in l:
try:
ndbno_term = client.search(term)
client.add_food(ndbno_term)
nutrient_ids_values = client.food_report(ndbno_term[0])
client.add_food_nutrients(nutrient_ids_values)
except:
skipped.append(term)
print(skipped)