-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
165 lines (129 loc) · 4.73 KB
/
index.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import math
import os
from flask import Flask
from flask import json
from flask import jsonify
from flask import request
from flask_cors import CORS, cross_origin
app = Flask(__name__)
cors = CORS(app)
@app.route('/api/hello')
def api():
return "Hey"
# Returns a list of ingredients for a recipe
def parseRecipeIngredients(recipe):
ingredients = recipe['ingredients'].copy()
for i in range(len(ingredients)):
ingredients[i] = ingredients[i].split(" ")[1:]
ingredients[i] = ' '.join(ingredients[i])
return ingredients
# Returns the ranks for each recipe based on relevance to a list of ingredients
def getRecipeRanks(ingredients, recipes):
ranks = [0 for i in range(len(recipes))]
for r in range(len(ranks)):
rank = 0
parsedIngredients = parseRecipeIngredients(recipes[r])
for ingredient in ingredients:
for i in parsedIngredients:
if ingredient in i:
rank += 1
ranks[r] = rank
return ranks
# returns a list of recipes catered to a list of ingredients
@app.route('/api/getRecipesFromIngredients')
@cross_origin()
def getRecipesFromIngredients():
ingredients = request.args.getlist('ingredients')
diet = request.args.get("diet")
ranks = getRecipeRanks(ingredients, allRecipes)
recipes = allRecipes
recipe_rank_tuples=[(recipes[i],ranks[i]) for i in range(len(ranks))]
for i in range(0,len(recipe_rank_tuples)-1):
for j in range(len(recipe_rank_tuples)-1):
if(recipe_rank_tuples[j][1]>recipe_rank_tuples[j+1][1]):
temp = recipe_rank_tuples[j]
recipe_rank_tuples[j] = recipe_rank_tuples[j+1]
recipe_rank_tuples[j+1] = temp
final_recipes = [t[0] for t in recipe_rank_tuples if t[1] > 0]
final_recipe_titles = []
recipes_to_return = []
for recipe in final_recipes:
if recipe["title"] not in final_recipe_titles:
final_recipe_titles.append(recipe["title"])
recipes_to_return.append(recipe)
recipes_to_return = pruneDiet(diet, recipes_to_return)
return jsonify(recipes_to_return)
def pruneDiet(diet, initialRecipes):
recipes_to_return = []
print(diet)
if diet == "":
return initialRecipes
for recipe in initialRecipes:
if recipe["diet"] == diet:
recipes_to_return.append(recipe)
return recipes_to_return
# returns a list of ingredient objects
@app.route('/api/getAllIngredients')
@cross_origin()
def getAllIngredients():
global allIngredients
filename = os.path.join(app.static_folder, './data/ingredients.json')
with open(filename) as ingredients_file:
allIngredients = json.load(ingredients_file)
return jsonify(allIngredients)
###---- INITIALISING JSON FILES FOR INGREDIENTS AND RECIPES ----###
def initIngredients():
global allIngredients
filename = os.path.join(app.static_folder, './data/ingredients.json')
with open(filename) as ingredients_file:
allIngredients = json.load(ingredients_file)
def initRecipes():
global allRecipes
filename = os.path.join(app.static_folder, './data/recipes.json')
with open(filename) as recipes_file:
allRecipes = json.load(recipes_file)
initIngredients()
initRecipes()
###-------------------------------------------------------------###
# returns the whole ingredient object, given its ID (ingredients.json)
@app.route('/api/getIngredientObj/<id>', methods = ['GET', 'POST'])
def getIngredientObj(id):
id = int(id)
categories = allIngredients['Categories']
for cat in categories:
if cat['id'] == math.floor(id / 100):
for ingredient in cat['ingredients']:
if ingredient['id'] == id:
return ingredient
# returns the whole recipe object, given its ID (recipes.json)
@app.route('/api/getRecipeObj/<id>', methods = ['GET', 'POST'])
def getRecipeObj(id):
id = int(id)
for r in allRecipes:
if r['id'] == id:
return str(r['ingredients'])
else:
return ""
# returns a title of an ingredient, given its ID
@app.route('/api/getIngredientName/<id>', methods = ['GET', 'POST'])
def getIngredientName(id):
return (getIngredientObj(id))['title']
# returns a list of ingredients, given a recipe ID
@app.route('/api/getRecipeIngredients/<id>', methods = ['GET', 'POST'])
def getRecipeIngredients(id):
return (getRecipeObj(id))['ingredients']
@app.route('/')
def home():
return 'Home Page Route'
@app.route('/about')
def about():
return 'About Page Route'
@app.route('/portfolio')
def portfolio():
return 'Portfolio Page Route'
@app.route('/contact')
def contact():
return 'Contact Page Route'
# Run the application
if __name__ == '__main__':
app.run()