-
Notifications
You must be signed in to change notification settings - Fork 17
/
spidyquotes.py
259 lines (209 loc) · 7.79 KB
/
spidyquotes.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Run app
"""
import datetime
import os
import json
import uuid
import random
import string
import base64
from flask import Flask, render_template, request, jsonify, session, redirect, url_for, make_response
from collections import Counter, defaultdict
from flask_limiter import Limiter
from slugify import slugify
app = Flask(__name__)
app.secret_key = 'yabba dabba doo!'
app.config['SESSION_TYPE'] = 'filesystem'
DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
QUOTES = [json.loads(l) for l in open(os.path.join(DATA_DIR, 'quotesdb.jl'))]
for quote in QUOTES:
quote['author']['slug'] = slugify(quote.get('author', {}).get('name'))
AUTHORS = {}
for l in open(os.path.join(DATA_DIR, 'authorsdb.jl')):
author = json.loads(l)
AUTHORS[slugify(author.get('name'))] = author
ITEMS_PER_PAGE = 10
DEFAULT_DELAY = 10000
def quotes_by_author_and_tags():
authors = defaultdict(lambda: defaultdict(list))
for quote in QUOTES:
name = quote.get('author', {}).get('name', '')
for tag in quote.get('tags', []):
authors[name][tag].append(quote.get('text'))
return authors
QUOTES_BY_AUTHOR_AND_TAGS = quotes_by_author_and_tags()
def top_ten_tags():
tags = (tag for d in QUOTES for tag in d['tags'])
return Counter(tags).most_common(10)
TOP_TEN_TAGS = top_ten_tags()
def get_quotes_for_page(page, tag=None):
page = int(page)
if tag:
quotes = [q for q in QUOTES if tag in q['tags']]
else:
quotes = QUOTES
start, end = (page - 1) * ITEMS_PER_PAGE, page * ITEMS_PER_PAGE
has_next_page = len(quotes) > int(page) * ITEMS_PER_PAGE
return dict(
quotes=quotes[start:end],
has_next=has_next_page,
tag=tag,
page=page,
)
@app.route("/")
@app.route("/page/<page>/")
@app.route("/tag/<tag>/")
@app.route("/tag/<tag>/page/<page>/")
def index(tag=None, page=1):
params = get_quotes_for_page(page=page, tag=tag)
return render_template('index.html',
top_ten_tags=TOP_TEN_TAGS,
authenticated='username' in session,
**params)
@app.route("/tableful/")
@app.route("/tableful/page/<page>/")
@app.route("/tableful/tag/<tag>/")
@app.route("/tableful/tag/<tag>/page/<page>/")
def tableful(tag=None, page=1):
params = get_quotes_for_page(page=page, tag=tag)
return render_template('tableful.html',
top_ten_tags=TOP_TEN_TAGS,
**params)
@app.route("/js/")
@app.route("/js/page/<page>/")
def data_in_js(page=1):
params = get_quotes_for_page(page=page)
params['formatted_quotes'] = json.dumps(params['quotes'], indent=4)
return render_template('data_in_js.html', **params)
@app.route("/js-onclick/")
@app.route("/js-onclick/page/<page>/")
def data_in_js_onclick(page=1):
"""Similar to data_in_js, but in this endpoint the pagination buttons are
based on JS.
"""
params = get_quotes_for_page(page=page)
params['formatted_quotes'] = json.dumps(params['quotes'], indent=4)
return render_template('data_in_js_onclick.html', **params)
@app.route("/js-delayed/")
@app.route("/js-delayed/page/<page>/")
def data_in_js_delayed(page=1):
"""Similar to data_in_js, but in this endpoint there is a javascript delay
before displaying quotes.
"""
params = get_quotes_for_page(page=page)
params['formatted_quotes'] = json.dumps(params['quotes'], indent=4)
try:
params['delay'] = int(request.args.get('delay'))
except Exception:
params['delay'] = DEFAULT_DELAY
return render_template('data_in_js_delayed.html', **params)
@app.route("/api/quotes")
def api_quotes():
page = int(request.args.get('page', 1))
tag = request.args.get('tag')
data = get_quotes_for_page(page=page, tag=tag)
data['top_ten_tags'] = TOP_TEN_TAGS
return jsonify(data)
@app.route("/scroll")
def scroll():
return render_template('ajax.html')
@app.route("/random")
def random_quote():
i = random.randrange(0, len(QUOTES))
return render_template('index.html', quotes=[QUOTES[i]])
@app.route("/fingerprint", methods=["GET", "POST"])
def fingerprint_check():
if request.method == 'POST':
data = request.get_json()
result = data['result']
confidenceScore = result['confidence']['score']
# clicked = data.get('clicked')
if confidenceScore >= 0.4:
response = make_response(jsonify({"allowed": True}))
expire_time = datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
response.set_cookie('score', value=str(confidenceScore),
expires=expire_time)
response.set_cookie('visitorId', value=result['visitorId'], expires=expire_time)
return response
else:
return jsonify({"allowed": False})
else:
score = request.cookies.get('score')
visitor_id = request.cookies.get('visitorId')
if visitor_id and score and float(score) >= 0.6:
return render_template('ajax.html')
return render_template('fs-js.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
error = ''
if request.method == 'POST':
if not request.form.get('username'):
error = 'please, provide your username.'
elif request.form.get('csrf_token') != session.get('csrf_token'):
error = 'invalid CRSF token.'
else:
session['username'] = request.form.get('username')
return redirect(url_for('index'))
session['csrf_token'] = ''.join(
random.sample(string.ascii_letters, len(string.ascii_letters))
)
return render_template(
'login_page.html', csrf_token=session['csrf_token'], error=error
)
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
@app.route('/search.aspx', methods=['GET'])
def search():
authors = QUOTES_BY_AUTHOR_AND_TAGS.keys()
viewstate = base64.b64encode(
uuid.uuid4().hex.encode('utf-8') + b',' + ','.join(authors).encode('utf-8')
).decode('utf-8')
return render_template(
'filter.html',
authors=authors,
viewstate=viewstate
)
@app.route('/filter.aspx', methods=['POST'])
def filter():
selected_author = request.form.get('author')
viewstate_data = base64.b64decode(request.form.get('__VIEWSTATE')).decode('utf-8').split(',')
if selected_author not in viewstate_data:
return redirect(url_for('search'))
selected_tag = request.form.get('tag')
if selected_tag:
viewstate_data.append(selected_tag)
quotes = QUOTES_BY_AUTHOR_AND_TAGS.get(selected_author, {}).get(selected_tag)
return render_template(
'filter.html',
quotes=quotes,
selected_author=selected_author,
selected_tag=selected_tag,
authors=QUOTES_BY_AUTHOR_AND_TAGS.keys(),
tags=QUOTES_BY_AUTHOR_AND_TAGS.get(selected_author, {}).keys(),
viewstate=base64.b64encode(','.join(viewstate_data).encode('utf-8')).decode('utf-8'),
)
@app.route('/author/<authorslug>/')
def author_details(authorslug):
return render_template(
'author.html',
author=AUTHORS.get(authorslug)
)
if os.getenv('DYNO'):
print('running in heroku, enabling limit of 1 request per second...')
Limiter(app, global_limits=["1 per second"])
else:
print('NOT running in heroku...')
if '__main__' == __name__:
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--host')
parser.add_argument('--throttle', action='store_true')
args = parser.parse_args()
if args.throttle:
limiter = Limiter(app, global_limits=["1 per second"])
app.run(debug=args.debug, host=args.host)