-
Notifications
You must be signed in to change notification settings - Fork 1
/
Menu_Analyzer.py
152 lines (97 loc) · 4.31 KB
/
Menu_Analyzer.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import json
import requests
from Simplify import *
####### Shweta! --> Head over to the end of the document for the main program and the options. I suggest you collapse all also. ###################
headers = {'accept': 'application/json'}
apiKey = "3eSo7F2R4Z7ID4E6vT7u79S4c8qRLFk7"
## simple class that represents an ingredient in a dish.
class Ingredient:
def __init__(self, itemDict):
#print("creating new object")
self.name = str(itemDict['Name']).lower().strip()
def __str__(self):
return self.name
def __repr__(self):
return self.name
##collection of ingredients. A Menu is a collection of Dish objects.
class Dish:
def __init__(self, dishName, ingredients):
self.dishName = str(dishName).lower().strip()
self.ingredients = ingredients
def __str__(self):
if(self.ingredients==-1):
return "NO RECIPE FOUND"
return self.dishName
def __repr__(self):
if(self.ingredients==-1):
return "NO RECIPE FOUND"
return self.dishName
def containsSome(self, category):
for ingredient in self.ingredients:
if (match(ingredient,category)):
return 1
return 0
## returns a list of ingredients given a RecipeID
def getIngredientsFromRecipeID(dishID):
#url url used to look up the ingredients given a dishID.
url = "http://api.bigoven.com/recipe/" + str(dishID) + "?api_key=" + apiKey
#http://api.bigoven.com/recipe/167626?api_key=3eSo7F2R4Z7ID4E6vT7u79S4c8qRLFk7
#print("INGREDIENT SEARCH URL:\t"+url)
#gets a JSON response from the given URL.
response = requests.get(url, headers=headers)
#(optional) get the header type
#print("HEADER TYPE:\t"+response.headers['content-type'])
#make a dictionnary from the json response.
data = response.json()
#create an empty list
ingredientList = list()
# Since the Ingredients are actually dictionnaries, we must iterate over each element of the dictionnary.
for entry in data['Ingredients']:
newIngredient = Ingredient(entry)
ingredientList.append(newIngredient)
#print("INGREDIENTS for dish with RecipeID "+str(dishID)+" are : \t"+ str(ingredientList))
return ingredientList
#http://api.bigoven.com/recipes?pg=1&rpp=25&title_kw=pizza&api_key=3eSo7F2R4Z7ID4E6vT7u79S4c8qRLFk7&sort=quality
def getDishIdFromName(dishName):
#Used to find the RecipeID of a given Dish name.
url = "http://api.bigoven.com/recipes?pg=1&rpp=25&title_kw="+ dishName.lower() + "&api_key="+apiKey +"&sort=quality"
#print("dish SEARCH URL:\t"+url)
#gets a JSON response from the given URL.
response = requests.get(url, headers=headers)
#(optional) get the header type
#print("HEADER TYPE:\t"+response.headers['content-type'])
#make a json object from the response.
data = response.json()
#return -1 if there are no search results for the given dish name.
if(data['ResultCount']==0):
#print("There are no dishes with that name")
return -1;
#print(data)
#print("RECIPE ID RESULT FOR STR "+dishName+": \t"+str(data['Results'][0]['RecipeID']))
return data['Results'][0]['RecipeID']
def getIngredientsFromName(dishName):
return getIngredientsFromRecipeID(getDishIdFromName(dishName.strip().lower()))
## returns the ingredients required for this recipe
def getIngredientList(dishName):
id = getDishIdFromName(dishName)
if(id == -1):
#print("Cannot find a Recipe for "+ dishName+" in BigOven's database")
ingredients = -1
return -1
else:
ingredients = getIngredientsFromRecipeID(id)
#print(ingredients)
return ingredients
#comment out to use User Input.
#dishName = input("Please enter a Recipe Name: ")
#getList(dishName)
def createDishList(listOfDishNames):
#for now its a list. TODO: make it the right data structure.
dishList = list()
for dishName in listOfDishNames:
newDish = Dish(dishName, getIngredientList(dishName))
#print(str(newDish)+" ; INGREDIENTS: "+str(newDish.ingredients))
if(newDish.ingredients!=-1):
dishList.append(newDish)
print(str(len(dishList))+" items in menu")
return dishList;