-
Notifications
You must be signed in to change notification settings - Fork 2
/
yummly_API.py
49 lines (38 loc) · 1.63 KB
/
yummly_API.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
import requests
import json
from pprint import pprint
app_id = '1032528d' #HARD CODED, will EXTRACT OUT
app_key = '86f7d4445c3089d85c8cc6a398de3827' # HARD CODED, WILL EXTRACT OUT
recipes_url = 'http://api.yummly.com/v1/api/recipes?'
def build_recipe_list_request(ingredients): #ingredients list from Machine Learning
api_url = 'http://api.yummly.com/v1/api/recipes?' + \
'_app_id=' + app_id + '&' + '_app_key=' + app_key + \
'&requirePictures=True' + '&allowedCuisine[]=cuisine^cuisine-french'
for ingredient in ingredients:
api_url += '&allowedIngredient[]=' + str(ingredient)
return api_url
def get_yummly_json(api_url): #should return JSONP not JSON
yummly_response = requests.get(api_url)
json_objects = yummly_response.json()
return json_objects
def build_recipe_steps_request(recipe_id):
api_url = "http://api.yummly.com/v1/api/recipe/" + recipe_id + \
"?_app_id=1032528d&_app_key=86f7d4445c3089d85c8cc6a398de3827"
return api_url
#ENTRY POINT
#input: List of ingredients:
def get_recipes(ingredients):
recipe_ids = get_yummly_json(build_recipe_list_request(ingredients))
# print recipe_ids
if 'matches' not in recipe_ids:
return None
list_of_recipes = recipe_ids['matches']
#tuples of (name, url_to_src_recipe, image_url)
recipe_information = []
for each in list_of_recipes:
recipe_val = get_yummly_json(build_recipe_steps_request(each['id']))
name = recipe_val['name']
url_to_src = recipe_val['source']['sourceRecipeUrl'].encode('utf-8')
img_url = recipe_val['images'][0]['hostedLargeUrl'].encode('utf-8')
recipe_information.append((name, url_to_src, img_url))
return recipe_information