In [ ]:
import collections
import requests
In [ ]:
Ingredient = collections.namedtuple("Ingredient", "name weight")
class IngredientList(object):
def __init__(self, ingredients):
self.ingredients = ingredients
def __repr__(self):
return '<IngredientList : %s>' % self.ingredients
In [ ]:
params = {'app_id':'2b0b3871',
'app_key':'a8631ce0fea0c72a0d1bfdcdc59824ec',
'q':'snickerdoodle',
'to':'100',}
In [ ]:
result = requests.get('https://api.edamam.com/search', params=params)
In [ ]:
json = result.json()
In [ ]:
def key(hit):
return hit['recipe']['calories']/hit['recipe']['yield']
In [ ]:
sorted(json['hits'], key=key, reverse=True);
In [ ]:
recipes = []
for hit in json['hits']:
print hit['recipe']
In [ ]:
counter = collections.Counter([
(ingredient['food'], ingredient['weight']/hit['recipe']['yield']) for hit in json['hits']
for ingredient in hit['recipe']['ingredients']])
In [ ]:
d = collections.defaultdict(int)
for (key, amount) in counter.elements():
d[key] += amount
In [ ]:
for key in set(t[0] for t in counter.keys()):
d[key] = d[key]/100.0
In [ ]:
for ingredient, amount in sorted(d.items(), key=lambda s: s[1], reverse=True)[:15]:
print ingredient, ':', amount
In [ ]: