API Wrapper for USDA Food Consumption Databases


In [168]:
import requests

In [169]:
class FCD(object):
    FORMAT = "json"
    API_KEY = "ENTER YOUR API KEY HERE!!!"
    API_URL = "http://api.nal.usda.gov/ndb/{}/?format={}&api_key={}"
    def __init__(self):
        super(FCD, self).__init__()

    @staticmethod
    def get_url(command):
        return FCD.API_URL.format(command, FCD.FORMAT, FCD.API_KEY)
        
    @staticmethod
    def find(name):
        """
        searchs for the given food
        
        :return: returns a list of matching food objects 
        """
        base_url = FCD.get_url("search")
        url = base_url + "&q={}".format(name)
        json_response = requests.get(url).json()["list"]["item"]
        return json_response
    
    @staticmethod
    def get_report(ndbno):
        base_url = FCD.get_url("reports")
        url = base_url + "&type=f&ndbno={}".format(ndbno)
        json_response = requests.get(url).json()["report"]
        return json_response

    @staticmethod
    def get_nutrients(ndbno):
        report = FCD.get_report(ndbno)
        return report["food"]["nutrients"]
    
    @staticmethod
    def get_measures(ndbno):
        nutrients = FCD.get_nutrients(ndbno)
        return set(m["label"] for n in nutrients for m in n["measures"])

Searching food by keywords


In [150]:
for food in FCD.find("apple slice"):
    print("- {} -{}".format(food["ndbno"], food["name"]))


- 45037461 -MICHIGAN APPLES, APPLE SLICES WITH CARAMEL, UPC: 071228174323
- 45037466 -MICHIGAN APPLES, APPLE SLICES WITH GRAPES, UPC: 071228174316
- 45114816 -GRANDMA HOERNER'S, BIG SLICE, KETTLE COOKED APPLES SLICES, CHUNKS & SAUCE, CHERRY VANILLA, UPC: 018522884145
- 45114792 -GRANDMA HOERNER'S, BIG SLICE, KETTLE COOKED APPLES; SLICES, CHUNKS, & SAUCE, CINNAMON FRENCH TOAST, UPC: 018522884176
- 45114818 -GRANDMA HOERNER'S, BIG SLICE, KETTLE COOKED APPLES; SLICES, CHUNKS & SAUCE, ORANGE GINGER, UPC: 018522884152
- 45105319 -CINNAMON APPLE SLICES DRIED FRUIT, UPC: 085239071366
- 45106128 -CINNAMON APPLE SLICES FREEZE DRIED FRUIT, UPC: 085239802045
- 45050451 -APPLE SLICES FREEZE DRIED FRUIT, UPC: 085239174180
- 45105695 -APPLE SLICES FREEZE DRIED FRUIT, UPC: 085239801741
- 45028519 -GREEN ACRES, FUJI APPLE SLICES IN LIGHT SYRUP, UPC: 076963519835
- 45010410 -CRUNCH PAK, APPLE SLICES, UPC: 732313142206
- 45064671 -CRUNCH PAK, ORGANIC SWEET APPLE SLICES, UPC: 732313142435
- 45064682 -CRUNCH PAK, SWEET APPLE SLICES, UPC: 732313122031
- 45094318 -O ORGANICS, ORGANIC SWEET APPLE SLICES, UPC: 079893402048
- 45094270 -BELLA VIVA ORCHARDS, PURE & NATURAL APPLE SLICES, UPC: 701024111054
- 09007 -Apples, canned, sweetened, sliced, drained, unheated
- 09008 -Apples, canned, sweetened, sliced, drained, heated
- 45048208 -DIPPIN' STIX, SLICED APPLES & CARAMEL WITH PEANUTS, UPC: 649632000813
- 45091286 -DIPPIN' STIX, SLICED APPLES, PEANUT BUTTER & CHOCOLATE, UPC: 649632001230
- 45091341 -REICHEL, DIPPIN' STICKS, SLICED APPLES & CARAMEL, UPC: 649632000806
- 45098025 -SISTERS FRUIT COMPANY, RED DELICIOUS SLICED APPLE CHIPS, LIGHT & CRISPY, UPC: 857670004063
- 45104853 -PRO2SNAX, SLICED APPLES WITH MILD CHEDDAR CHEESE, UPC: 649632001414
- 45036949 -MEIJER, SLICED FUJI APPLES JUICE, UPC: 713733845731
- 45098184 -BIG SLICE, KETTLE COOKED APPLES, BANANA, MANGO, HEMP SEED, UPC: 018522031563
- 45114778 -GRANDMA HOERNER'S, BIG SLICE, KETTLE COOKED APPLES, BLUEBERRY, POMEGRANATE, UPC: 018522884121
- 45114779 -GRANDMA HOERNER'S, BIG SLICE, KETTLE COOKED APPLES, APRICOT, UPC: 018522884114
- 45021259 -PATRICK CUDAHY, THICK SLICED BACON WITH SWEET APPLE-WOOD, UPC: 043200614028
- 45012707 -PATRICK CUDAHY, BACON, ORIGINAL, REGULAR SLICED, NATURALLY SMOKED WITH SWEET APPLE-WOOD,, UPC: 043200614073

Fetching measures for a particular food


In [152]:
selected_ndbno = "01009"
FCD.get_measures(selected_ndbno)


Out[152]:
{'cubic inch',
 'cup, diced',
 'cup, melted',
 'cup, shredded',
 'oz',
 'slice (1 oz)'}

In [157]:
selected_measure = "cup, diced"
quantity = 2

Fetching nutrients and calculating intake


In [131]:
def calculate_consumption(ndbno, measure, quantity):
    nutrients = APIWrapper.get_nutrients(ndbno)
    intake = []
    for nutrient in nutrients:
        for i_measure in nutrient["measures"] :
            if i_measure["label"] == measure and i_measure["value"] != 0 :
                intake.append({
                        "label": nutrient["name"], 
                        "unit": nutrient["unit"], 
                        "intake": i_measure["value"] * quantity
                    })
    return intake

In [158]:
calculate_consumption(selected_ndbno, selected_measure, quantity)


Out[158]:
[{'intake': 97.74, 'label': 'Water', 'unit': 'g'},
 {'intake': 1066.0, 'label': 'Energy', 'unit': 'kcal'},
 {'intake': 4458.0, 'label': 'Energy', 'unit': 'kJ'},
 {'intake': 60.38, 'label': 'Protein', 'unit': 'g'},
 {'intake': 87.94, 'label': 'Total lipid (fat)', 'unit': 'g'},
 {'intake': 9.8, 'label': 'Ash', 'unit': 'g'},
 {'intake': 8.16, 'label': 'Carbohydrate, by difference', 'unit': 'g'},
 {'intake': 1.26, 'label': 'Sugars, total', 'unit': 'g'},
 {'intake': 0.68, 'label': 'Glucose (dextrose)', 'unit': 'g'},
 {'intake': 0.32, 'label': 'Lactose', 'unit': 'g'},
 {'intake': 0.26, 'label': 'Galactose', 'unit': 'g'},
 {'intake': 1874.0, 'label': 'Calcium, Ca', 'unit': 'mg'},
 {'intake': 0.36, 'label': 'Iron, Fe', 'unit': 'mg'},
 {'intake': 72.0, 'label': 'Magnesium, Mg', 'unit': 'mg'},
 {'intake': 1202.0, 'label': 'Phosphorus, P', 'unit': 'mg'},
 {'intake': 200.0, 'label': 'Potassium, K', 'unit': 'mg'},
 {'intake': 1724.0, 'label': 'Sodium, Na', 'unit': 'mg'},
 {'intake': 9.6, 'label': 'Zinc, Zn', 'unit': 'mg'},
 {'intake': 0.08, 'label': 'Copper, Cu', 'unit': 'mg'},
 {'intake': 0.072, 'label': 'Manganese, Mn', 'unit': 'mg'},
 {'intake': 75.2, 'label': 'Selenium, Se', 'unit': 'µg'},
 {'intake': 92.2, 'label': 'Fluoride, F', 'unit': 'µg'},
 {'intake': 0.076, 'label': 'Thiamin', 'unit': 'mg'},
 {'intake': 1.13, 'label': 'Riboflavin', 'unit': 'mg'},
 {'intake': 0.156, 'label': 'Niacin', 'unit': 'mg'},
 {'intake': 1.082, 'label': 'Pantothenic acid', 'unit': 'mg'},
 {'intake': 0.174, 'label': 'Vitamin B-6', 'unit': 'mg'},
 {'intake': 72.0, 'label': 'Folate, total', 'unit': 'µg'},
 {'intake': 72.0, 'label': 'Folate, food', 'unit': 'µg'},
 {'intake': 72.0, 'label': 'Folate, DFE', 'unit': 'µg'},
 {'intake': 43.6, 'label': 'Choline, total', 'unit': 'mg'},
 {'intake': 1.8, 'label': 'Betaine', 'unit': 'mg'},
 {'intake': 2.9, 'label': 'Vitamin B-12', 'unit': 'µg'},
 {'intake': 872.0, 'label': 'Vitamin A, RAE', 'unit': 'µg'},
 {'intake': 872.0, 'label': 'Retinol', 'unit': 'µg'},
 {'intake': 224.0, 'label': 'Carotene, beta', 'unit': 'µg'},
 {'intake': 3278.0, 'label': 'Vitamin A, IU', 'unit': 'IU'},
 {'intake': 1.88, 'label': 'Vitamin E (alpha-tocopherol)', 'unit': 'mg'},
 {'intake': 1.6, 'label': 'Vitamin D (D2 + D3)', 'unit': 'µg'},
 {'intake': 1.6, 'label': 'Vitamin D3 (cholecalciferol)', 'unit': 'µg'},
 {'intake': 64.0, 'label': 'Vitamin D', 'unit': 'IU'},
 {'intake': 6.4, 'label': 'Vitamin K (phylloquinone)', 'unit': 'µg'},
 {'intake': 49.808, 'label': 'Fatty acids, total saturated', 'unit': 'g'},
 {'intake': 1.674, 'label': '4:0', 'unit': 'g'},
 {'intake': 1.416, 'label': '6:0', 'unit': 'g'},
 {'intake': 0.89, 'label': '8:0', 'unit': 'g'},
 {'intake': 2.164, 'label': '10:0', 'unit': 'g'},
 {'intake': 2.468, 'label': '12:0', 'unit': 'g'},
 {'intake': 7.934, 'label': '14:0', 'unit': 'g'},
 {'intake': 0.84, 'label': '15:0', 'unit': 'g'},
 {'intake': 22.77, 'label': '16:0', 'unit': 'g'},
 {'intake': 0.504, 'label': '17:0', 'unit': 'g'},
 {'intake': 8.96, 'label': '18:0', 'unit': 'g'},
 {'intake': 0.126, 'label': '20:0', 'unit': 'g'},
 {'intake': 0.04, 'label': '22:0', 'unit': 'g'},
 {'intake': 0.022, 'label': '24:0', 'unit': 'g'},
 {'intake': 24.41, 'label': 'Fatty acids, total monounsaturated', 'unit': 'g'},
 {'intake': 0.782, 'label': '14:1', 'unit': 'g'},
 {'intake': 1.33, 'label': '16:1 undifferentiated', 'unit': 'g'},
 {'intake': 0.266, 'label': '16:1 t', 'unit': 'g'},
 {'intake': 0.156, 'label': '17:1', 'unit': 'g'},
 {'intake': 19.544, 'label': '18:1 undifferentiated', 'unit': 'g'},
 {'intake': 2.154, 'label': '18:1 t', 'unit': 'g'},
 {'intake': 0.172, 'label': '20:1', 'unit': 'g'},
 {'intake': 0.002, 'label': '22:1 undifferentiated', 'unit': 'g'},
 {'intake': 0.002, 'label': '24:1 c', 'unit': 'g'},
 {'intake': 3.752, 'label': 'Fatty acids, total polyunsaturated', 'unit': 'g'},
 {'intake': 3.092, 'label': '18:2 undifferentiated', 'unit': 'g'},
 {'intake': 0.3, 'label': '18:3 undifferentiated', 'unit': 'g'},
 {'intake': 0.018, 'label': '20:2 n-6 c,c', 'unit': 'g'},
 {'intake': 0.098, 'label': '20:3 undifferentiated', 'unit': 'g'},
 {'intake': 0.14, 'label': '20:4 undifferentiated', 'unit': 'g'},
 {'intake': 0.026, 'label': '20:5 n-3 (EPA)', 'unit': 'g'},
 {'intake': 0.024, 'label': '22:4', 'unit': 'g'},
 {'intake': 0.044, 'label': '22:5 n-3 (DPA)', 'unit': 'g'},
 {'intake': 0.002, 'label': '22:6 n-3 (DHA)', 'unit': 'g'},
 {'intake': 2.42, 'label': 'Fatty acids, total trans', 'unit': 'g'},
 {'intake': 2.42, 'label': 'Fatty acids, total trans-monoenoic', 'unit': 'g'},
 {'intake': 262.0, 'label': 'Cholesterol', 'unit': 'mg'},
 {'intake': 1.444, 'label': 'Tryptophan', 'unit': 'g'},
 {'intake': 2.756, 'label': 'Threonine', 'unit': 'g'},
 {'intake': 3.184, 'label': 'Isoleucine', 'unit': 'g'},
 {'intake': 5.118, 'label': 'Leucine', 'unit': 'g'},
 {'intake': 2.706, 'label': 'Lysine', 'unit': 'g'},
 {'intake': 1.444, 'label': 'Methionine', 'unit': 'g'},
 {'intake': 0.324, 'label': 'Cystine', 'unit': 'g'},
 {'intake': 2.836, 'label': 'Phenylalanine', 'unit': 'g'},
 {'intake': 2.926, 'label': 'Tyrosine', 'unit': 'g'},
 {'intake': 3.706, 'label': 'Valine', 'unit': 'g'},
 {'intake': 1.444, 'label': 'Arginine', 'unit': 'g'},
 {'intake': 1.444, 'label': 'Histidine', 'unit': 'g'},
 {'intake': 1.982, 'label': 'Alanine', 'unit': 'g'},
 {'intake': 4.578, 'label': 'Aspartic acid', 'unit': 'g'},
 {'intake': 12.5, 'label': 'Glutamic acid', 'unit': 'g'},
 {'intake': 1.444, 'label': 'Glycine', 'unit': 'g'},
 {'intake': 6.592, 'label': 'Proline', 'unit': 'g'},
 {'intake': 2.06, 'label': 'Serine', 'unit': 'g'}]

In [167]:
FCD.find("MICHIGAN APPLES, APPLE SLICES WITH CARAMEL,  ")


Out[167]:
[{'ds': 'BL',
  'group': 'Branded Food Products Database',
  'name': 'MICHIGAN APPLES, APPLE SLICES WITH CARAMEL, UPC: 071228174323',
  'ndbno': '45037461',
  'offset': 0},
 {'ds': 'BL',
  'group': 'Branded Food Products Database',
  'name': 'MICHIGAN APPLES, APPLE SLICES WITH GRAPES, UPC: 071228174316',
  'ndbno': '45037466',
  'offset': 1},
 {'ds': 'BL',
  'group': 'Branded Food Products Database',
  'name': "DIPPIN' STIX, SLICED APPLES & CARAMEL WITH PEANUTS, UPC: 649632000813",
  'ndbno': '45048208',
  'offset': 2},
 {'ds': 'BL',
  'group': 'Branded Food Products Database',
  'name': "REICHEL, DIPPIN' STICKS, SLICED APPLES & CARAMEL, UPC: 649632000806",
  'ndbno': '45091341',
  'offset': 3}]

In [170]:
FCD.get_nutrients("45005389")


Out[170]:
[{'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '150'}],
  'name': 'Energy',
  'nutrient_id': '208',
  'unit': 'kcal',
  'value': '500'},
 {'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '5.00'}],
  'name': 'Protein',
  'nutrient_id': '203',
  'unit': 'g',
  'value': '16.67'},
 {'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '10.00'}],
  'name': 'Total lipid (fat)',
  'nutrient_id': '204',
  'unit': 'g',
  'value': '33.33'},
 {'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '12.00'}],
  'name': 'Carbohydrate, by difference',
  'nutrient_id': '205',
  'unit': 'g',
  'value': '40.00'},
 {'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '2.0'}],
  'name': 'Fiber, total dietary',
  'nutrient_id': '291',
  'unit': 'g',
  'value': '6.7'},
 {'group': 'Proximates',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '1.00'}],
  'name': 'Sugars, total',
  'nutrient_id': '269',
  'unit': 'g',
  'value': '3.33'},
 {'group': 'Minerals',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '20'}],
  'name': 'Calcium, Ca',
  'nutrient_id': '301',
  'unit': 'mg',
  'value': '67'},
 {'group': 'Minerals',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '1.08'}],
  'name': 'Iron, Fe',
  'nutrient_id': '303',
  'unit': 'mg',
  'value': '3.60'},
 {'group': 'Minerals',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '440'}],
  'name': 'Sodium, Na',
  'nutrient_id': '307',
  'unit': 'mg',
  'value': '1467'},
 {'group': 'Vitamins',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '0.0'}],
  'name': 'Vitamin C, total ascorbic acid',
  'nutrient_id': '401',
  'unit': 'mg',
  'value': '0.0'},
 {'group': 'Vitamins',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '0'}],
  'name': 'Vitamin A, IU',
  'nutrient_id': '318',
  'unit': 'IU',
  'value': '0'},
 {'group': 'Lipids',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '1.50'}],
  'name': 'Fatty acids, total saturated',
  'nutrient_id': '606',
  'unit': 'g',
  'value': '5.00'},
 {'group': 'Lipids',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '0.00'}],
  'name': 'Fatty acids, total trans',
  'nutrient_id': '605',
  'unit': 'g',
  'value': '0.00'},
 {'group': 'Lipids',
  'measures': [{'eqv': 30.0, 'label': 'cup', 'qty': 0.33, 'value': '0'}],
  'name': 'Cholesterol',
  'nutrient_id': '601',
  'unit': 'mg',
  'value': '0'}]

In [171]:
calculate_consumption("45005389","cup",1)


Out[171]:
[{'intake': '150', 'label': 'Energy', 'unit': 'kcal'},
 {'intake': '5.00', 'label': 'Protein', 'unit': 'g'},
 {'intake': '10.00', 'label': 'Total lipid (fat)', 'unit': 'g'},
 {'intake': '12.00', 'label': 'Carbohydrate, by difference', 'unit': 'g'},
 {'intake': '2.0', 'label': 'Fiber, total dietary', 'unit': 'g'},
 {'intake': '1.00', 'label': 'Sugars, total', 'unit': 'g'},
 {'intake': '20', 'label': 'Calcium, Ca', 'unit': 'mg'},
 {'intake': '1.08', 'label': 'Iron, Fe', 'unit': 'mg'},
 {'intake': '440', 'label': 'Sodium, Na', 'unit': 'mg'},
 {'intake': '0.0', 'label': 'Vitamin C, total ascorbic acid', 'unit': 'mg'},
 {'intake': '0', 'label': 'Vitamin A, IU', 'unit': 'IU'},
 {'intake': '1.50', 'label': 'Fatty acids, total saturated', 'unit': 'g'},
 {'intake': '0.00', 'label': 'Fatty acids, total trans', 'unit': 'g'},
 {'intake': '0', 'label': 'Cholesterol', 'unit': 'mg'}]

In [ ]: