-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (104 loc) · 3.07 KB
/
app.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
from flask import Flask, url_for, render_template, request, session
from modules.Business import GatherBusinesses, Business
from modules.Graphs import Graph
import pygal
import json
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
BUSINESSES = []
SEARCH = {'term': None, 'location': None}
GRAPHS = None
"""
Before each request from the web,
we check if the user is in session so that
it gets it's personal unique search.
"""
@app.before_request
def setup():
global BUSINESSES, SEARCH
if 'user' not in session:
SEARCH = {}
"""
Main page. It returns a page with two fields
that the user must give input (food term and location).
Once the user submits a search the program returns
all the results about the search
"""
@app.route('/', methods=['GET', 'POST'])
def index():
# Store session of user
session['user'] = 'user'
if request.method == 'POST' and \
request.form['term'] != '' and \
request.form['location'] != '':
# Get search inputs
SEARCH['term'] = request.form['term']
SEARCH['location'] = request.form['location']
# Get businesses
analyze_businesses()
if BUSINESSES != [] and SEARCH != {}:
top = get_top()
return render_template('index.html', search=True,
topsent=top[0],
toprating=top[1],
businesses=BUSINESSES,
rchart=GRAPHS.rating_bar,
schart=GRAPHS.sentiment_bar,
srchart=GRAPHS.comparison_bar,
location=SEARCH['location'], term=SEARCH['term'])
else:
return render_template('index.html')
"""
View specific reviews made by customers
along with the rating and the sentiment_score
that was calculated.
"""
@app.route('/reviews/<bid>')
def business(bid):
print('\n\nBusinesses:', BUSINESSES)
for b in BUSINESSES:
if bid == b.id:
return render_template('routing/business.html',
business=b)
"""
About Page
"""
@app.route('/about')
def about():
return render_template('about.html')
"""
Get businesses from Yelp API and create
graphs using the data collected.
"""
def analyze_businesses():
global BUSINESSES
term, location = SEARCH['term'], SEARCH['location']
b = GatherBusinesses(term, location)
BUSINESSES = b.business_list
get_graphs()
"""
Get the top business based on ratings
and the top business based on sentiment_score.
"""
def get_top():
tops, topr = None, None
s, r = 0, 0
for b in BUSINESSES:
if float(b.sentiment_score) > s:
s = float(b.sentiment_score)
tops = b
if float(b.rating) > r:
r = float(b.rating)
topr = b
return [tops, topr]
"""
Use the Graph class to construct the three
graphs: sentiment_score graph, rating graph,
and the comparsion graph.
"""
def get_graphs():
global GRAPHS
GRAPHS = Graph(BUSINESSES)
if __name__ == '__main__':
app.run(debug=True)