-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
86 lines (65 loc) · 2.16 KB
/
Copy pathapp.py
File metadata and controls
86 lines (65 loc) · 2.16 KB
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
from flask import Flask, jsonify, json, request
from flask import render_template
from yelp_scrapper import *
import copy
app = Flask(__name__)
global generator
global pq
@app.route('/')
def home():
''' render the main page with search box for users to input restaurant name
of her/his interest.
'''
return render_template("home.html")
@app.route('/result', methods=['POST'])
def process():
''' create YelpScrapper instance with user's input (hopefully proper
retaurant name). Then, access information of the scrapper instance.
Pass those info to result() method that renders the result.html page
using provided info and Jinja2
'''
global generator
global pq
scrapper = YelpScrapper(request.form["restaurant"])
info = scrapper.processed_info
pq = info["rating_pq"]
q = Q.PriorityQueue()
q.queue = copy.deepcopy(pq.queue)
''' generator created '''
generator = get_review(q)
a = next(generator)
''' print all reviews sorted by closeness to averate nlp rating'''
return result(info["actual_rating"],
info["expected_rating"], str(scrapper), a[1])
def get_review(q):
global pq
while not q.empty():
yield q.get()
q.queue = copy.deepcopy(pq.queue)
yield q.get()
@app.route('/getReview', methods=['POST'])
def get_next_review():
global generator
global pq
review = "No review found :("
try:
review = next(generator)
except StopIteration:
q = Q.PriorityQueue()
q.queue = copy.deepcopy(pq.queue)
generator = get_review(q)
return jsonify({"review": next(generator)})
def result(a_rating, e_rating, rest_name, rest_review):
''' render the result page with actual Yelp rating, our estimated rating,
retaurant's name with "go back" button to allow users to return to the main
page.
'''
return render_template("result.html", actual_rating=a_rating,
estimated_rating=e_rating,
restaurant_name=rest_name,
restaurant_review=rest_review)
def main():
app.debug = True
app.run()
if __name__ == "__main__":
main()