In [117]:
import numpy as np
import pandas as pd
import requests
import json

In [118]:
df=pd.read_csv('Assets/pesto_ids', names=['recipe_id'])

In [119]:
df.head(2)


Out[119]:
recipe_id
0 Pesto-1698418
1 The-Best-Homemade-Pesto-1572415

In [120]:
key_id= '_app_id=79663a75&_app_key=02b233108f476f3110e0f65437c4d6dd'
toy_id = 'The-Best-Homemade-Pesto-1572415'
url='http://api.yummly.com/v1/api/recipe/'

response = requests.get(url + toy_id + '?' + key_id)

In [121]:
response.status_code


Out[121]:
200

In [122]:
# retrieve all recipe details for all pesto recipes
def get_recipe(i):
    response = requests.get(url + i + '?' + key_id)
    return response.json()
recipes=[]
for i in df['recipe_id']:
    recipes.append(get_recipe(i))

In [123]:
len(recipes)


Out[123]:
999

In [165]:
pprint(recipes[2].keys())


[u'totalTime',
 u'ingredientLines',
 u'attribution',
 u'name',
 u'prepTimeInSeconds',
 u'rating',
 u'cookTimeInSeconds',
 u'numberOfServings',
 u'yield',
 u'nutritionEstimates',
 u'source',
 u'flavors',
 u'images',
 u'attributes',
 u'cookTime',
 u'id',
 u'prepTime',
 u'totalTimeInSeconds']

In [294]:
#for each recipe get the id create a new dictionary of selected attributes and append into a list

recipe_details=[]
for recipe in recipes:
    _dict={}
    #import pdb; pdb.set_trace()
    _dict['id']=recipe['id']
    _dict['ingredientCount']= len(recipe['ingredientLines'])
    _dict['numberOfServings']= recipe['numberOfServings']
    if 'prepTimeInSeconds' in recipe.keys():
        _dict['prepTimeInSeconds']= recipe['prepTimeInSeconds']
    else:
        _dict['prepTimeInSeconds']= None
    if 'cookTimeInSeconds' in recipe.keys():
        _dict['cookTimeInSeconds']= recipe['cookTimeInSeconds']
    else:
        _dict['cookTimeInSeconds']= None
    _dict['totalTimeInSeconds']=recipe['totalTimeInSeconds']
    
    
    recipe_details.append(_dict)

In [297]:
# create a dataframe from list
details=pd.DataFrame(recipe_details)
#create list of columns
cols = list(details)
#arrange column positions
cols=['id','numberOfServings','ingredientCount','prepTimeInSeconds','cookTimeInSeconds','totalTimeInSeconds']
details=details.ix[:,cols]


['cookTimeInSeconds', 'id', 'ingredientCount', 'numberOfServings', 'prepTimeInSeconds', 'totalTimeInSeconds']

In [296]:
details


Out[296]:
id numberOfServings ingredientCount prepTimeInSeconds cookTimeInSeconds totalTimeInSeconds
0 Pesto-1698418 1 9 600.0 300.0 900.0
1 The-Best-Homemade-Pesto-1572415 4 7 NaN NaN 1500.0
2 Pea-and-Pesto-Risotto-1704441 6 7 300.0 2400.0 2700.0
3 Basic-Pesto-1190165 2 5 NaN 900.0 900.0
4 Carrot-Pesto-1702985 5 8 900.0 600.0 1500.0
5 Perfect-Pesto-1694499 4 6 NaN NaN 900.0
6 Pesto-1628539 4 8 NaN NaN 900.0
7 Cilantro-Pesto-1696494 1 9 NaN NaN 900.0
8 Roasted-Garlic-Paleo-Pesto-1700022 4 7 300.0 2700.0 3000.0
9 Fettuccine-Pesto-Pasta-1704560 4 9 NaN NaN 1500.0
10 Pesto-Martha-Stewart_4 1 7 900.0 NaN 1200.0
11 Pesto-Christmas-Tree-Pizza-768354 24 3 1200.0 NaN 2400.0
12 Pesto-Sauce-1696681 1 7 NaN NaN 1200.0
13 Pesto-347711 8 6 600.0 NaN 1200.0
14 Pesto-1230800 4 6 600.0 NaN 600.0
15 Pesto-Orzo-with-Tomatoes-1702712 4 4 NaN NaN 1500.0
16 Pesto-Chicken-Crescent-Ring-768366 8 4 900.0 NaN 2700.0
17 Easy-Pesto-1700167 4 9 NaN NaN 1200.0
18 Kale-_-Pistachio-Pesto-1705865 1 7 600.0 300.0 900.0
19 Shrimp-with-Pesto-and-Prosciutto-1700509 15 3 300.0 300.0 600.0
20 Pesto-1620761 4 7 NaN NaN 900.0
21 Rosemary-Pesto-1631884 4 7 NaN NaN 900.0
22 Classic-Pesto-1694726 1 6 600.0 NaN 600.0
23 Basic-Basil-Pesto-1620458 4 8 1200.0 NaN 1200.0
24 Basic-Basil-Pesto-1705738 4 8 NaN NaN 900.0
25 Basil-Pesto-1687339 1 8 NaN NaN 1500.0
26 Pesto-1045526 4 12 NaN NaN 900.0
27 Baked-Pesto-Chicken-1687730 4 5 NaN NaN 2400.0
28 Arugula-Green-Garlic-Pesto-1701959 4 7 NaN NaN 900.0
29 Pesto-1576085 250 8 300.0 180.0 480.0
... ... ... ... ... ... ...
969 Lightened-Up-Pesto-in-Minutes-978143 4 6 NaN NaN 1200.0
970 Cheesy-Vegan-Basil-Pesto-with-Hemp-Seeds-1342683 16 8 900.0 NaN 900.0
971 Parmesan-Pesto-Muffins-1547648 4 9 900.0 900.0 1800.0
972 Easy-Pesto-Stuffed-Mushrooms-966133 5 6 NaN NaN 1800.0
973 Lean-Green-Portobello_-Pesto-_-Artichoke-Panin... 4 7 NaN NaN 1200.0
974 Mizuna-Pesto-1362546 1 7 NaN NaN 1500.0
975 Prosciutto-and-pesto-pizza-347224 8 5 900.0 NaN 2100.0
976 4-Ingredient-Slow-Cooker-Pesto-Ranch-Chicken-1... 4 4 300.0 14400.0 14700.0
977 Italian-Tortellini-Skewers-with-Pesto-1273312 4 7 NaN NaN 1800.0
978 Creamy-Pesto-Gnocchi-with-Vegetables-1616599 4 6 NaN NaN 1500.0
979 Broccoli-Pesto-Pasta-Salad-1641579 4 8 NaN NaN 1800.0
980 Pesto-Bacon-Guacamole-1288871 6 9 NaN NaN 1500.0
981 Cilantro-Pesto-My-Recipes 6 7 NaN NaN 1500.0
982 Pesto-Pinwheels-1069650 4 8 NaN NaN 1500.0
983 Lamskoteletten-Met-Pesto-En-Pasta-Met-Lentegro... 4 11 300.0 1200.0 1200.0
984 Pesto-Sliders-MyRecipes-212036 4 5 NaN NaN 1500.0
985 MOIST-_-TENDER-CHICKEN-SALAD-with-sun-dried-to... 4 14 600.0 10800.0 11400.0
986 Pesto-Corn-Salad-1233122 5 7 NaN NaN 2100.0
987 Engine-2-Kale-Pesto-1355192 2 6 NaN NaN 1500.0
988 Lemon-Basil-Pesto-1111496 4 7 NaN NaN 2100.0
989 A-Vegan-Basil-Mint-Parsley-_Pesto_-1021270 4 6 NaN NaN 2400.0
990 Ramp-Pesto-569765 1 8 NaN NaN 1500.0
991 Pesto-and-Cheese-Stuffed-Zucchini-1066854 4 5 NaN NaN 3000.0
992 Chive-_-Lemon-Pesto-763060 4 8 NaN NaN 1200.0
993 Cashew-Pesto-Pasta-1637084 4 9 300.0 600.0 900.0
994 Savory-Pesto-Quinoa-Breakfast-Bowls-1426046 2 15 300.0 300.0 600.0
995 Basil-pesto-339012 1 7 300.0 NaN 1200.0
996 Easy-Squash-Noodles-With-Pesto-1521881 4 7 300.0 600.0 900.0
997 Rigatoni-With-Pesto-and-Chickpeas-1155873 4 5 300.0 NaN 1200.0
998 Green-Powerhouse-Pesto-Plate-440191 4 11 NaN NaN 4200.0

999 rows × 6 columns


In [ ]:
# Add data to dicts for courses, flavors and cuisines
    courses_dict[item.get('id')] = item['attributes'].get('course')
    flavors_dict[item.get('id')] = item.get('flavors')
    cuisine_dict[item.get('id')] = item['attributes'].get('cuisine')
    ingredients_dict[item.get('id')] = [x.lower() for x in item.get('ingredients')]