-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (134 loc) · 6.37 KB
/
main.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
#main.py
import webapp2
from random import shuffle
#libraries for APIs
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext import ndb
import json
import jinja2
import os
import urllib
import user_models
jinja_current_directory = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
my_file = open('app-secrets.json')
my_secrets = my_file.read()
SECRETS_DICT = json.loads(my_secrets)
my_file.close()
class WelcomePage(webapp2.RequestHandler):
def get(self):
welcome_template = jinja_current_directory.get_template('templates/welcome.html')
self.response.write(welcome_template.render({'login_url': users.create_login_url('/main')}))
def post(self):
self.redirect("/main")
class MainPage(webapp2.RequestHandler):
def get(self):
template_var = {}
global user
user = users.get_current_user()
if user:
nickname = user.nickname()
user_id = str(user.user_id())
logout_url = users.create_logout_url('/')
template_var = {
"logout_url": logout_url,
"nickname": nickname
}
# if (user_models.User.query().filter(user_models.User.nickname == nickname).fetch()[0] == nickname):
# pass
# else: WIP MAKING SURE YOU CANT REGISTER TWICE BC RN YOU CAN
global current_user_key
current_user_key = user_models.User(user_id=user_id, nickname=nickname, recipes=[]).put()
else:
self.redirect('/welcome')
main_template = jinja_current_directory.get_template('templates/main.html')
self.response.write(main_template.render(template_var))
def post(self):
#recipe API
global APP_ID
APP_ID = SECRETS_DICT['api-id1']
global APP_KEY
APP_KEY = "84103e81ef614ae430c3bc458d36ff16"
urlfetch.set_default_fetch_deadline(60) #this sets the deadline
url=("https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=" + urllib.quote(self.request.get("foodlist").replace(" ", "")) + "&number=5&ranking=2")
print url
result = urlfetch.fetch( #this goes to the endpoint and grabs the json
# url="https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=" + urllib.quote(self.request.get("foodlist")) + "&number=5&ranking=1",
url,
headers={
"X-Mashape-Key": SECRETS_DICT['mashape-key'],
"X-Mashape-Host": SECRETS_DICT['mashape-host'],
},)
print url
recipe_list = json.loads(result.content)
#print recipe_list #parses json
all_recipe_names = []
all_recipe_images = []
for recipenames in recipe_list:
all_recipe_names.append(recipenames["title"])
#print all_recipe_names[0]
final_recipe_dict = {}
final_recipe_list= []
for recipe in all_recipe_names:
quoted_recipe = urllib.quote(recipe)
# print recipe
recipe_search_info = urlfetch.fetch("https://api.edamam.com/search?q=" + quoted_recipe + "&app_id=" + APP_ID +"&app_key=" + APP_KEY + "&to=1")
print json.loads(recipe_search_info.content)["hits"][0]["recipe"]["url"]
results_dict= json.loads(recipe_search_info.content)
final_recipe_list.append( {
'name': recipe,
'img_url': results_dict["hits"][0]["recipe"]["image"],
'recipe_url': results_dict["hits"][0]["recipe"]["url"],
})
template_vars = {
'input_ingredient': self.request.get('foodlist'),
'recipes': final_recipe_list,
}
welcome_template = jinja_current_directory.get_template('templates/results.html')
self.response.write(welcome_template.render(template_vars))
class FavoritesPage(webapp2.RequestHandler):
def get(self):
recipe_instructions_template = jinja_current_directory.get_template('templates/favorites.html')
self.response.write(recipe_instructions_template.render())
def post(self):
global APP_ID
APP_ID = SECRETS_DICT['api-id1']
global APP_KEY
APP_KEY = "84103e81ef614ae430c3bc458d36ff16"
final_recipe_list= []
urlfetch.set_default_fetch_deadline(60) #this sets the deadline
quoted_recipe = urllib.quote(self.request.get("favoritesearch"))
recipe_search_info = urlfetch.fetch("https://api.edamam.com/search?q=" + quoted_recipe + "&app_id=" + APP_ID +"&app_key=" + APP_KEY + "&to=1")
print json.loads(recipe_search_info.content)["hits"][0]["recipe"]["url"]
results_dict= json.loads(recipe_search_info.content)
#print results_dict
final_recipe_list.append( {
'name': results_dict["q"],
'img_url': results_dict["hits"][0]["recipe"]["image"],
'recipe_url': results_dict["hits"][0]["recipe"]["url"],
})
user_model_key = user_models.Recipe(
title=final_recipe_list[0]['name'], image=final_recipe_list[0]['img_url'],
url=final_recipe_list[0]['recipe_url']).put()
current_user = user_models.User.query().filter(user_models.User.user_id == users.get_current_user().user_id()).get()
current_user.recipes.append(user_model_key)
current_user.put()
print user_models.User.query().filter(user_models.User.user_id == users.get_current_user().user_id()).get()
recipe_instructions_template = jinja_current_directory.get_template('templates/favorites.html')
self.response.write(recipe_instructions_template.render())
bool = True
for recipes in current_user.recipes:
if bool:
self.response.write("<br><br><br><br><br><br><br><br><br><br><br>" + '<a href="'+ recipes.get().url + '">' + recipes.get().title + "</a> ")
bool = False
else:
self.response.write('<a href="'+ recipes.get().url + '">' + recipes.get().title + "</a> ")
# user_models.User.query().filter(user_models.User.user_id == self.request.get("key")).get().recipes = (user_model_key,)
app = webapp2.WSGIApplication([
('/', WelcomePage),
('/main', MainPage),
('/favorites', FavoritesPage),
], debug=True)