-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnutritionix.py
40 lines (25 loc) · 864 Bytes
/
nutritionix.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 json
import requests
from urllib.parse import urljoin
import config
BASE_URL = 'https://trackapi.nutritionix.com/v2/'
HEADER = {
'X-APP-ID': config.X_APP_ID,
'X-APP-KEY': config.X_APP_KEY,
}
def search(search_term):
"""Perfrom a simple instant search on the nutrionix API"""
params = {'query': search_term, 'detailed': 'True'}
endpoint = urljoin(BASE_URL, 'search/instant')
r = requests.get(endpoint, params=params, headers=HEADER)
resp = r.json()
return resp
def search_branded_item(item):
"""Performs a search to obtain nutrients of a branded food item,
given its NIX item ID"""
endpoint = urljoin(BASE_URL, 'search/item')
params = {'nix_item_id': item}
r = requests.get(endpoint, params=params, headers=HEADER)
resp = r.json()
food_info = resp['foods'][0]
return food_info