/
server.py
149 lines (114 loc) · 5.73 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
from jinja2 import StrictUndefined
from flask import Flask, render_template, request as flaskrequest, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from model import Location, Keyword, connect_to_db
import os
from book_cosine_similarity import *
from get_local_venues import *
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
app.jinja_env.undefined = StrictUndefined
api_key = os.environ['LIBRARYTHING_DEVELOP_KEY']
@app.route("/")
def index():
"""Display the homepage."""
return render_template("index.html")
@app.route("/search", methods=["GET"])
def search_for_books():
"""Search for books through the homepage by location."""
user_location_query = flaskrequest.args.get('search-input')
print "user query, ", user_location_query
radius = flaskrequest.args.get('radius')
print "radius1, ", radius
radius = int(radius)
print "radius, ", radius
print user_location_query
jsonify_search_result_list = []
book_obj_list = Location.get_books_associated_with_location(radius, user_location_query)
book_obj_list = [book_obj for book_obj in book_obj_list if book_obj.description]
book_obj_list = [book_obj for book_obj in book_obj_list if len(book_obj.description.split(" ")) > 20]
print book_obj_list
for book_object in book_obj_list:
book_dict = {}
keyword_list = [word.keyword for word in book_object.keywords]
keywords = filter(None, keyword_list)
author_list = [author.author_name for author in book_object.authors if book_object.authors]
book_dict["title"] = book_object.title.replace('"', "'")
print book_dict["title"]
book_dict["subtitle"] = book_object.subtitle
book_dict["authors"] = ", ".join(author_list)
book_dict["description"] = book_object.description.replace('"', "'")
book_dict["thumbnailUrl"] = book_object.thumbnail_url
book_dict["previewLink"] = book_object.preview_link
book_dict["keywords"] = keywords
print book_dict["keywords"]
jsonify_search_result_list.append(book_dict)
print "search complete"
return jsonify(name=jsonify_search_result_list)
@app.route("/search/kmeans")
def get_kmeans_graph():
"""GETs user location query, runs location through a Location class
function called 'get books associated with location' from my Model.py
and returns a list of book objects associated with a location with the
user's input radius. Books are filtered out based on whether they have
a description longer than 20 words."""
user_location_query = flaskrequest.args.get('search-input')
print "user_location_query for get kmeans", user_location_query
radius = flaskrequest.args.get('radius')
print "radius for get kmeans", radius
book_obj_list = Location.get_books_associated_with_location(radius, user_location_query)
book_obj_list = [book_obj for book_obj in book_obj_list if book_obj.description]
book_obj_list = [book_obj for book_obj in book_obj_list if len(book_obj.description.split(" ")) > 20]
# from the book_cosine_similarity file
kmeans_cluster_html = returns_kmeans_cluster_graph(book_obj_list)
return jsonify(kmeans=kmeans_cluster_html)
@app.route("/keyword", methods=["GET"])
def books_associated_with_keyword_page():
"""GET request from clicking on keyword, keyword is used to query database
for other books associated with the keyword and returns info in a ajax GET
in the form a jsonified list of dictionaries
containing info on each book. """
keyword = flaskrequest.args.get('keyword')
jsonify_search_result_list = []
keyword_object_list = Keyword.query.filter_by(keyword=keyword).all()
book_obj_list_of_list = [key_obj.books for key_obj in keyword_object_list]
book_obj_list = [book_obj for sublist in book_obj_list_of_list for book_obj in sublist]
book_obj_list = [book_obj for book_obj in book_obj_list if book_obj.description]
book_obj_list = [book_obj for book_obj in book_obj_list if len(book_obj.description.split(" ")) > 20]
print book_obj_list
for book_object in book_obj_list:
book_dict = {}
keyword_list = [word.keyword for word in book_object.keywords]
keywords = filter(None, keyword_list)
author_list = [author.author_name for author in book_object.authors if book_object.authors]
book_dict["title"] = book_object.title
print book_dict["title"]
book_dict["subtitle"] = book_object.subtitle
book_dict["authors"] = ", ".join(author_list)
book_dict["description"] = book_object.description
book_dict["thumbnailUrl"] = book_object.thumbnail_url
book_dict["previewLink"] = book_object.preview_link
book_dict["keywords"] = keywords
print book_dict["keywords"]
jsonify_search_result_list.append(book_dict)
print "search complete"
return jsonify(keywordbooks=jsonify_search_result_list, keyword=keyword)
@app.route('/location', methods=['GET'])
def get_location_return_nearby_venues():
"""GET request takes user location in form of latitude and longitude,
passes through 'return local veunes' function in the get_local_venues.py
file and returns a list of dictionaries containing info on local bookstores
within a 3 mi radius."""
latitude = flaskrequest.args.get('lat')
longitude = flaskrequest.args.get('lon')
local_venues_list = return_local_venues(latitude, longitude)
return jsonify(localVenues=local_venues_list)
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 = False
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run()