-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfood.py
80 lines (71 loc) · 2.88 KB
/
food.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
import json
import requests
from pprint import pprint
from macros import Macros
from food_ids import FOOD_IDS, NUTRIENT_IDS, API_KEY
# NOTE: type b is basic, type f is full, s is stats
URL = 'https://api.nal.usda.gov/ndb/V2/reports?ndbno={}&type=s&format=json&api_key={}'
DEFAULT_SERVING = 100 # grams
class Food(object):
"""a food object from an ndbno ID """
def __init__(self, name="", update_dict=None, debug=False):
if update_dict:
self.__dict__.update(update_dict)
m = update_dict['macros']
self.macros = Macros(m['serving_size'],
m['protein_grams'],
m['fat_grams'],
m['carb_grams'])
else:
self.name = name
self.id = FOOD_IDS[name] # ndbno
self.debug = debug
self.macros = self._fetch_food_info()
def __str__(self):
return self.name
def _fetch_food_info(self):
resp = requests.get(URL.format(self.id, API_KEY))
json_dict = resp.json()
if self.debug:
print("============BEGIN RESPONSE:===============")
pprint(json_dict)
print("============END RESPONSE:===============")
food_obj = json_dict['foods'][0]['food']
nutrients = food_obj['nutrients']
try:
serving_grams = nutrients[0]['measures'][0]['eqv']
assert nutrients[0]['measures'][0]['eunit'] == 'g'
except (KeyError, AssertionError):
# Not all foods have a "measures" key
if self.debug:
print("We cannot find the `measures` key, continuing...")
if self.name == "Avocado":
serving_grams = 136
elif self.name == "Whole Milk":
serving_grams = 240 # mililiters not grams
else:
raise
# now build the macros
protein = fat = carbs = 0
for d in nutrients:
if d['nutrient_id'] == NUTRIENT_IDS['protein']:
protein = self._calc_calorie_for_macro(serving_grams, float(d['value']))
elif d['nutrient_id'] == NUTRIENT_IDS['fat']:
fat = self._calc_calorie_for_macro(serving_grams, float(d['value']))
elif d['nutrient_id'] == NUTRIENT_IDS['carbs']:
carbs = self._calc_calorie_for_macro(serving_grams, float(d['value']))
return Macros(serving_grams, protein, fat, carbs)
def _calc_calorie_for_macro(self, serving_size, value):
return round(serving_size / DEFAULT_SERVING * value, 1)
# Tests:
if __name__ == '__main__':
out = []
for k in FOOD_IDS:
try:
print("k is : ", k)
out.append(Food(k))
except Exception as e:
print("cannot make this food: ", k)
print("... because: ", e)
raise
print([f.__dict__ for f in out])