-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
48 lines (40 loc) · 1.54 KB
/
routes.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
import os
from yelpapi import YelpAPI
import requests
from functools import wraps
from flask import Flask, redirect, render_template, request, session, url_for
MY_API_KEY = os.getenv("YELP_API_KEY");
yelp_api = YelpAPI(MY_API_KEY)
def login_required(f):
#https://flask.palletsprojects.com/en/2.1.x/patterns/viewdecorators/
@wraps(f)
def decorated_function(*args, **kwargs):
if session["username"] is None:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
def find(location, categories, price_range):
#Contact API endpoint.
try:
response = yelp_api.search_query(term="restaurants", location=location, categories=categories, price=price_range, sort_by='best_match', limit=6)
except requests.RequestException:
return None
#Parse the data.
try:
results = response["businesses"]
parsed = []
for result in results:
business = {}
business["id"] = result["id"]
business["name"] = result["name"]
business["rating"] = result["rating"]
business["categories"] = result["categories"][0]["title"]
business["price"] = result["price"]
business["image"] = result["image_url"]
address = result["location"]['display_address']
business["location"] = ", ".join(address)
business["website"] = result["url"]
parsed.append(business)
return parsed
except:
return None