-
Notifications
You must be signed in to change notification settings - Fork 0
/
nutrients.py
40 lines (33 loc) · 1.21 KB
/
nutrients.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
import requests
import os
NUTRITIONIX_API = os.environ.get('NUTRITIONIX_API')
NUTRITIONIX_APP_ID = os.environ.get('NUTRITIONIX_APP_ID')
NUTRITIONIX_ENDPOINT = os.environ.get('NUTRITIONIX_ENDPOINT')
headers = {
"x-app-id": NUTRITIONIX_APP_ID,
"x-app-key": NUTRITIONIX_API,
"x-remote-user-id": "0",
"Content-Type": "application/json"
}
class Nutrition_Information():
def __init__(self, query):
nutrition_params = {
"query": query
}
response = requests.post(url=NUTRITIONIX_ENDPOINT, headers=headers, json=nutrition_params)
response.raise_for_status()
print(str(response.json()))
data = response.json()['foods'][0]
food_title = data['food_name']
serving_quantity = data['serving_qty']
serving_unit = data['serving_unit']
nutrient_info = data['full_nutrients']
caffeine_amount = 0
for nutrient in nutrient_info:
if nutrient['attr_id'] == 262:
# caffeine amount in mg
caffeine_amount = nutrient['value']
self.food = food_title
self.qty = serving_quantity
self.serving_unit = serving_unit
self.caffeine_amt = caffeine_amount