/
server.py
439 lines (326 loc) · 11.7 KB
/
server.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
from flask import (Flask, render_template, redirect, flash, session, request)
# from flask_debugtoolbar import DebugToolbarExtension
from jinja2 import StrictUndefined
from model import connect_to_db, db, User
from flickr_functions import (get_photos, get_url, get_location)
from saving_photos import (get_pic, save_pic, remove_pic, check_saved)
import os
import requests
google_maps_api_key = os.environ['GOOGLE_KEY']
from geocode_functions import reverse_geo_location
app = Flask(__name__)
saved = None
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
app.jinja_env.undefined = StrictUndefined
@app.route('/')
def index():
"""Homepage."""
if session.get('user_id'):
return redirect("/user/%s" % session['user_id'])
return render_template("homepage.html")
@app.route('/register', methods=['GET'])
def register_form():
"""Show registration form."""
return render_template("register-form.html")
@app.route('/register', methods=["POST"])
def process_registration():
"""Process registration for new users."""
email = request.form["email"]
password = request.form["password"]
first_name = request.form["first_name"]
last_name = request.form["last_name"]
new_user = User(email=email, first_name=first_name, last_name=last_name, password=password)
db.session.add(new_user)
db.session.commit()
flash('Welcome to Pocket Passport!')
flash("User %s added." % email)
return redirect("/")
@app.route('/login', methods=['GET'])
def login_form():
"""Show form for user login."""
return render_template("login-form.html")
@app.route('/login', methods=["POST"])
def process_login():
"""Processes login for users."""
email = request.form["email"]
password = request.form["password"]
user = User.query.filter_by(email=email).first()
if not user:
flash('No email found, please try again.')
return redirect("/login")
if user.password != password:
flash('Incorrect password')
return redirect("/login")
session['user_id'] = user.user_id
return redirect("/user/%s" % (user.user_id))
@app.route('/user/<user_id>')
def user_profile(user_id):
"""Show user profile."""
user = User.query.get(user_id)
#gets users saved pics by ID
users_saved_pics = get_pic(user_id)
return render_template("user-profile.html",
user=user,
users_saved_pics=users_saved_pics)
@app.route('/search-results')
def search_city():
"""Return categories after location search."""
search = request.args.get('location')
name = search.capitalize()
return render_template("categories.html",
name=name,
search=search
)
@app.route('/food')
def search_food():
"""Return photo results from location search + food"""
search = request.args.get('locate')
name = search.capitalize()
# returns search location plus the tags
image_urls = get_photos(search, "food, restaurants")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/landmarks')
def search_landmarks():
"""Return photo results from location search + landmarks"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "landmarks")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/fashion')
def search_fashion():
"""Return photo results from location search + fashion"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "style, fashion")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/coffee')
def search_coffee():
"""Return photo results from location search + coffee"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "coffee, cafe")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/hikes')
def search_hikes():
"""Return photo results from location search + hikes"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "hiking, trails")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/bars')
def search_bars():
"""Return photo results from bar search + hikes"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "bars, clubs")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/nightlife')
def search_nightlife():
"""Return photo results from bar search + hikes"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "nightlife")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/tourist')
def search_tourist():
"""Return photo results from search + tourist attractions"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "tourist attractions")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
@app.route('/shops')
def search_shops():
"""Return photo results from search + tourist attractions"""
search = request.args.get('locate')
name = search
image_urls = get_photos(search, "shops")
index = 0
for image in image_urls:
if check_saved(image_urls[index][0]):
image.append("unlike")
index += 1
else:
image.append("like")
index += 1
return render_template("search-results.html",
name=name,
image_urls=image_urls,
search=search
)
def get_yelp_access_token():
"""Get yelp business api access token"""
app_id = '1aZ_5OVCRb0P7v3unYNIqA'
app_secret = 'gXJgDy4eUkJZB4MM3vBWX1w7SHaeRiHGNhcL3cIv48wE9AFG8eT4IYPAtMZ5vOJm'
data = {'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': app_secret}
token = requests.post('https://api.yelp.com/oauth2/token', data=data)
access_token = token.json()['access_token']
return access_token
@app.route('/photo-info/<int:photo_id>')
def get_photo_info(photo_id):
"""Returns photo and additional information page"""
# check if photo is saved to the user's profile
saved = check_saved(photo_id)
# use flickr api getinfo to get url of photo
img_src = get_url(photo_id)
# use flickr api getlocation to get location data of photo
location = get_location(photo_id)
# get lat and lng from flickr location api call
lat = location['lat']
lng = location['lng']
address = reverse_geo_location(lat, lng)
access_token = get_yelp_access_token()
# use yelp api to search business info from lat/lng data
url = 'https://api.yelp.com/v3/businesses/search'
headers = {'Authorization': 'bearer %s' % access_token}
params = {'limit': 1, 'sort_by': 'distance', 'radius': 10000, 'location': address}
resp = requests.get(url=url, params=params, headers=headers)
# get result in json format
result = resp.json()['businesses']
if len(result) == 0:
yelplink = "No yelp data available"
else:
yelplink = result[0]['url']
yelprating = result[0]['rating']
yelpname = result[0]['name']
return render_template("photo-info.html",
photo_id=photo_id,
img_src=img_src,
saved=saved,
lat=lat,
lng=lng,
yelplink=yelplink,
yelprating=yelprating,
yelpname=yelpname,
address=address,
google_maps_api_key=google_maps_api_key
)
@app.route('/save-pic', methods=["POST"])
def save_to_db():
"""Saves photo to database."""
img_src = request.form.get("src")
photo_id = request.form.get("id")
user_id = session['user_id']
save_pic(img_src=img_src, photo_id=photo_id, user_id=int(user_id))
return "OK"
@app.route('/remove-pic', methods=["POST"])
def remove_photo():
"""Removes photo from database."""
photo_id = request.form.get("id")
user_id = session['user_id']
remove_pic(photo_id, int(user_id))
return "OK"
@app.route('/logout')
def logout():
"""Remove user_id from session and redirect to the home page."""
del session['user_id']
flash('You have successfully logged out.')
return redirect("/")
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the point
# that we invoke the DebugToolbarExtension
app.debug = True
connect_to_db(app)
# DebugToolbarExtension(app)
# DEBUG_TB_INTERCEPT_REDIRECTS = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
PORT = int(os.environ.get("PORT", 5000))
app.run(port=PORT, host='0.0.0.0')