-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
196 lines (151 loc) · 6.92 KB
/
application.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
import os
import re
from flask import Flask, jsonify, render_template, request, Response
import psycopg2
from psycopg2 import sql
import json
from helpers import column2array, arrayfilter
from flask_compress import Compress
from flask_sslify import SSLify
# compression
COMPRESS_MIMETYPES = ['text/html', 'text/css', 'text/xml', 'application/json', 'application/javascript']
COMPRESS_LEVEL = 6
COMPRESS_MIN_SIZE = 500
# Set up database
DATABASE_URL = os.environ['DATABASE_URL']
connection = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = connection.cursor()
genrearray = column2array("genres", cursor)
countryarray = column2array("country", cursor)
castarray = column2array("castmember", cursor)
writersarray = column2array("writers", cursor)
directorsarray = column2array("directors", cursor)
# Configure application
app = Flask(__name__)
Compress(app)
sslify = SSLify(app)
# Ensure responses aren't cached
# @app.after_request
# def after_request(response):
# response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
# response.headers["Expires"] = 0
# response.headers["Pragma"] = "no-cache"
# return response
@app.route('/<category>/<filters>/')
def bechdelresults(category, filters):
filterstring = "%" + filters + "%"
try:
cursor.execute(sql.SQL("SELECT SUM(rating), COUNT(rating) FROM list WHERE {} ILIKE %s").format(sql.Identifier(category)), (filterstring,))
except:
return ("No known movies for: " + filters + " in " + category)
result = cursor.fetchone()
return ("The average bechdel score for movies with " + filters + " in " + category + " is " + str(result[0]/result[1]) + " with " + str(result[0]) + " movies.")
@app.route("/api/<method>", methods=["POST", "GET"])
def apirequest(method):
"""Returns development of bechdel score over the decades for search term and category"""
supportedMethods = ["getTotalScore", "getDecadeScores", "getYearlyScores"]
if method not in supportedMethods:
return Response(json.dumps({"error" : method + " method not supported"}), mimetype='application/json', status=400)
data = {
"method" : method,
"category" : "",
"filterTerm" : "",
"totalRated" : 0,
"totalAverage" : 0,
}
# Ensure category was submitted
category = request.values.get("category")
if not category:
return Response(json.dumps({"error" : "no category was submitted"}), mimetype='application/json', status=400)
# Ensure filterterm was submitted
filterterm = request.values.get("filterterm")
if not filterterm:
return Response(json.dumps({"error" : "no filterterm was submitted"}), mimetype='application/json', status=400)
# setting up dict object
data["category"] = category
data["filterTerm"] = filterterm
filterstring = "%" + filterterm + "%"
sqlsearchstring = """SELECT COUNT(*) total,
SUM(case when rating = 0 then 1 else 0 end) rating0,
SUM(case when rating = 1 then 1 else 0 end) rating1,
SUM(case when rating = 2 then 1 else 0 end) rating2,
SUM(case when rating = 3 then 1 else 0 end) rating3
FROM list
WHERE {}
ILIKE %s"""
try:
cursor.execute(sql.SQL(sqlsearchstring).format(sql.Identifier(category)), (filterstring,))
except:
return Response(json.dumps({"error" : "Problem with the database request. Maybe category " + category + " is not valid?"}), mimetype='application/json', status=400)
(totalrated, totalrating0, totalrating1, totalrating2, totalrating3) = cursor.fetchone()
if int(totalrated) >0:
data["totalRated"] = totalrated
data["totalAverage"] = "{:.10}".format((totalrating1 + 2*totalrating2 + 3*totalrating3)/totalrated)
data["totalRatingOf0"] = totalrating0
data["totalRatingOf1"] = totalrating1
data["totalRatingOf2"] = totalrating2
data["totalRatingOf3"] = totalrating3
if method == "getTotalScore":
return Response(json.dumps(data), mimetype='application/json', status=200)
if method == "getDecadeScores":
sorter = "decade"
if method == "getYearlyScores":
sorter = "year"
sqlsearchstring = "SELECT "+ sorter + "," + sqlsearchstring[6:] # remove the select argument, so year / decade can be inserted
sqlsearchstring += " GROUP BY " + sorter + " ORDER BY " + sorter # add the grouping
# following for "getDecadeScores" and "getYearlyScores" methods
try:
cursor.execute(sql.SQL(sqlsearchstring).format(sql.Identifier(category)), (filterstring,))
except:
return Response(json.dumps({"error" : "Problem in the 'getDecadeScores' or 'getYearlyScores' methods"}), mimetype='application/json', status=400)
data[sorter + 's'] ={}
for entry in cursor.fetchall():
data[sorter + 's'][entry[0]] = {}
data[sorter + 's'][entry[0]]["totalRated"] = entry[1]
data[sorter + 's'][entry[0]]["totalAverage"] = "{:.10}".format((entry[3] + 2*entry[4] + 3*entry[5])/entry[1])
data[sorter + 's'][entry[0]]["totalRatingOf0"] = entry[2]
data[sorter + 's'][entry[0]]["totalRatingOf1"] = entry[3]
data[sorter + 's'][entry[0]]["totalRatingOf2"] = entry[4]
data[sorter + 's'][entry[0]]["totalRatingOf3"] = entry[5]
return Response(json.dumps(data), mimetype='application/json', status=200)
@app.route('/api')
def showAPI():
return render_template("api.html")
@app.route('/about')
def showAbout():
return render_template("about.html")
@app.route('/')
def index():
return render_template("index.html")
@app.route('/search/<category>/<query>/<limit>')
def search(category,query,limit):
if limit.isdigit():
limit = int(limit)
else:
return Response(json.dumps({"error" : "limit must be ineger"}), mimetype='application/json', status=400)
filteredarray = []
targetarray = []
if category == "genres":
targetarray = genrearray
elif category == "country":
targetarray = countryarray
elif category == "castmember":
targetarray = castarray
elif category == "writers":
targetarray = writersarray
elif category == "directors":
targetarray = directorsarray
filteredarray = arrayfilter(targetarray, query)
returndict = {'totalresults' : len(filteredarray), 'category' : category, 'query' : query, 'limit' : limit, 'results' : filteredarray[:int(limit)]}
return Response(json.dumps(returndict), mimetype='application/json', status=200)
@app.route('/serviceWorker.js', methods=['GET'])
def sw():
return app.send_static_file('serviceWorker.js')
@app.route('/offline.html', methods=['GET'])
def offline():
return app.send_static_file('offline.html')
@app.route('/manifest.json', methods=['GET'])
def manifest():
return app.send_static_file('manifest.json')
if __name__ == '__main__':
app.run(debug = True)