-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
472 lines (411 loc) · 17.7 KB
/
main.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/env python
# Bible + Exception Imports
from bibles import *
from multi_bible_search import BibleSearch
# Internal imports
from compress import Compress
from navigate import NavigatePassage, NavigateRel, NavigateVersion
from flask import Flask, jsonify, render_template, redirect, Response, session, url_for, request, abort, make_response
from functools import cache
from hashlib import sha256
from itertools import zip_longest
from random import randint
import re
import sys
import time
from typing import Tuple, Union
DEBUG = False
def create_app() -> Flask:
app = Flask(__name__)
app.config['SECRET_KEY'] = sha256(time.localtime().__str__().encode("utf-8")).__str__()
Compress(app)
if DEBUG:
app.jinja_env.auto_reload = True
minify = re.compile(r'<!--.*?-->|(\s{2,}\B)|\n')
start = time.perf_counter()
searcher = BibleSearch()
# Read the ESV API key
api_key: str = ""
try:
with open("esv-api-key.txt", "r") as key_in:
key: str = key_in.read()
if key != "<key-goes-here>":
api_key = key
except IOError:
# This is mostly for testing
try:
api_key = sys.argv[1]
except IndexError:
pass
bibles = {
'ACV': ACV(),
'AKJV': AKJV(),
'AMP': AMP(),
'ASV': ASV(),
'BBE': BBE(),
'BSB': BSB(),
'CSB': CSB(),
'Darby': Darby(),
'DRA': DRA(),
'EBR': EBR(),
'ESV': ESV() if not len(api_key) else ESV(api_key),
'GNV': GNV(),
'KJV': KJV(),
'KJV 1611': KJV1611(),
'LSV': LSV(),
'MSG': MSG(),
'NASB 1995': NASB1995(),
'NET': NET(),
'NIV 1984': NIV1984(),
'NIV 2011': NIV2011(),
'NKJV': NKJV(),
'NLT': NLT(),
'RNKJV': RNKJV(),
'RSV': RSV(),
'RWV': RWV(),
'UKJV': UKJV(),
'WEB': WEB(),
'YLT': YLT()
}
end = time.perf_counter()
print(f"Loaded Bibles and search in {end - start:.6f} seconds")
"""
# Fix for session expiry
@app.before_request
def make_session_permanent():
session.permanent = True
"""
@app.route('/', methods=['GET', 'POST'])
@app.route('/index.html', methods=['GET', 'POST'])
def main_page() -> Union[str, Response]:
"""
Main page for selecting version(s) and passage
:return: Main menu page
"""
error_mess = None
form = NavigatePassage()
version_select = NavigateVersion()
if form.validate_on_submit():
session['select_version'] = versions = version_select.select_version.data
session['select_book'] = book = form.book.data
session['select_chapter'] = chapter_dat = form.chapter.data
if chapter_dat and form.chapter.data and \
bibles['KJV'].has_passage(book, int(chapter_dat)):
if len(versions) == 1:
return redirect(url_for('chapter'))
elif len(versions) > 1:
return redirect(url_for('chapter_split'))
else:
return abort(400)
elif form.chapter.data:
error_mess = "Please submit the book first"
html: str = render_template('index.html', title='Home', debug=DEBUG, form=form, error_mess=error_mess,
version_select=version_select, book=session.get('select_book', 'Genesis'),
chapter=session.get('select_chapter', '1'))
return minify.sub('', html)
@app.route('/chapter', methods=['GET', 'POST'])
@app.route('/chapter.html', methods=['GET', 'POST'])
def chapter() -> Union[str, Response]:
"""
Reading view of a passage in a selected version
:return: Page of the selected passage
"""
book_sel: str = session.get('select_book') if session.get('select_book') is not None else "Genesis"
chapter_sel: str = session.get('select_chapter') if session.get('select_chapter') else "1"
form = NavigateRel()
passage_form = NavigatePassage()
passage_form.book.default = book_sel
version_select = NavigateVersion()
if form.validate_on_submit():
if form.next_button.data:
form.next_button.data = False
session['select_book'], session['select_chapter'] = bibles['KJV'].next_passage(book_sel, chapter_sel)
elif form.previous_button.data:
form.previous_button.data = False
session['select_book'], session['select_chapter'] = bibles['KJV'].previous_passage(book_sel, chapter_sel)
book_sel = session.get('select_book') if session.get('select_book') is not None else "Genesis"
chapter_sel = session.get('select_chapter') if session.get('select_chapter') else "1"
if passage_form.validate_on_submit() and passage_form.submit.data:
passage_form.submit.data = False
session['select_book'] = book_sel = passage_form.book.data
session['select_chapter'] = chapter_sel = passage_form.chapter.data
if len(version_select.data['select_version']) > 1 and isinstance(version_select.data['select_version'], list):
session['select_version'] = version_select.select_version.data
return redirect(url_for('chapter_split'))
else:
session['select_version'] = version_select.select_version.data
version_sel: list = session.get('select_version')[0] if \
session.get('select_version') else 'ESV'
version_select.select_version.data = [version_sel]
if version_sel in bibles.keys():
try:
content: dict = bibles[version_sel].get_passage(book_sel, int(chapter_sel))
except PassageInvalid or ValueError:
return abort(400)
else:
content = {"book": "Invalid version", "chapter": "",
"verses": {"": ["Please clear your cookies and try again"]}}
html = render_template('chapter.html', title=book_sel + " " + chapter_sel, debug=DEBUG, form=form,
content=content, version=version_sel, passage_form=passage_form,
version_select=version_select)
return minify.sub('', html)
@app.route('/chapter_split', methods=['GET', 'POST'])
@app.route('/chapter_split.html', methods=['GET', 'POST'])
def chapter_split() -> Union[str, Response]:
"""
Split between selected versions
:return: Split view page of a passage in selected versions
"""
book_sel: str = session.get('select_book') if session.get('select_book') is not None else "Genesis"
chapter_sel: str = session.get('select_chapter') if session.get('select_chapter') else "1"
form = NavigateRel()
passage_form = NavigatePassage()
passage_form.book.default = book_sel
version_select = NavigateVersion()
if form.validate_on_submit():
if form.next_button.data:
form.next_button.data = False
session['select_book'], session['select_chapter'] = bibles['KJV'].next_passage(book_sel, chapter_sel)
elif form.previous_button.data:
form.previous_button.data = False
session['select_book'], session['select_chapter'] = bibles['KJV'].previous_passage(book_sel, chapter_sel)
book_sel = session.get('select_book') if session.get('select_book') is not None else "Genesis"
chapter_sel = session.get('select_chapter') if session.get('select_chapter') else "1"
if passage_form.validate_on_submit() and passage_form.submit.data:
passage_form.submit.data = False
session['select_book'] = book_sel = passage_form.book.data
session['select_chapter'] = chapter_sel = passage_form.chapter.data
if len(version_select.data['select_version']) == 1 or isinstance(version_select.data['select_version'], str):
session['select_version'] = version_select.select_version.data
return redirect(url_for('chapter'))
else:
session['select_version'] = version_select.select_version.data
version_sel: list = session.get('select_version') if session.get('select_version') \
else ['ESV', 'KJV']
version_select.select_version.data = version_sel
content: list = []
for version in version_sel:
if version in bibles.keys():
try:
tmp_content = bibles[version].get_passage(book_sel, int(chapter_sel))
content.append([verse for heading, verses in tmp_content.get('verses').items() for verse in verses])
except PassageInvalid or ValueError:
return abort(400)
else:
content.append(["Invalid version", "Please clear your cookies and try again"])
content = list(zip_longest(*content, fillvalue=""))
html = render_template('chapter_split.html', title=book_sel + " " + chapter_sel, debug=DEBUG, form=form,
content=content, chapter_num=chapter_sel, version=version_sel, passage_form=passage_form,
version_select=version_select, book=book_sel)
return minify.sub('', html)
@cache
@app.route('/grid', methods=['GET'])
@app.route('/grid.html', methods=['GET'])
def grid() -> Response:
"""
A grid of Bible passages.
:return: HTML page of iframes for Bible passages.
"""
response = make_response(minify.sub('', render_template("grid.html", debug=DEBUG, versions=bibles.keys())))
response.cache_control.max_age = 60 * 60 * 24 * 7
return response # minify.sub('', render_template("grid.html", debug=debug, versions=bibles.keys()))
@cache
@app.route('/copyright', methods=['GET'])
@app.route('/copyright.html', methods=['GET'])
def copyright_notice() -> Response:
"""
Copyright notice page
:return: Copyright notice
"""
response = make_response(minify.sub('', render_template("copyright.html", debug=DEBUG)))
response.cache_control.max_age = 60 * 60 * 24 * 7
response.cache_control.public = True
return response
@app.route('/chapters/<book>', methods=['GET'])
def chapters(book) -> Response:
"""
Get the number of chapters for the given book.
:param book: Book to get the chapters of.
:return: JSON object of the format {"num_chapters": int}
"""
try:
num_chapters = bibles['KJV'].books_of_the_bible[book]
except KeyError:
num_chapters = 1
# return the number of chapters as a JSON response
return jsonify({'num_chapters': num_chapters})
@app.route('/embed', methods=['GET'])
def embed() -> Response:
"""
Generate passage embeddings for the grid of passages.
:return: The HTML for the iframe.
"""
# Get the parameters
version = request.args.get('version', 'ESV')
book = request.args.get('book', 'Genesis')
chapter_ref = request.args.get('chapter', '1')
# Get the passage, returning 400 for invalid references
try:
content: dict = bibles[version].get_passage(book, int(chapter_ref))
except PassageInvalid or ValueError or KeyError:
return abort(400)
response = make_response(minify.sub('', render_template('embed.html', title=book + " " + chapter_ref, debug=DEBUG,
content=content, version=version)))
response.cache_control.max_age = 60 * 60 * 24 * 7
return response
@app.route('/goto/', methods=['GET'])
def goto() -> Response:
"""
Go to a passage as specified by the query parameters.
:return: Redirect to the chapter page.
"""
# Get the parameters
version = request.args.get('version', 'ESV')
book = request.args.get('book', 'Genesis')
chapter_ref = request.args.get('chapter', '1')
# Use those for the session, validated elsewhere.
session['select_version'] = [version]
session['select_book'], session['select_chapter'] = book, chapter_ref
return redirect(url_for("chapter"))
@app.route('/search_endpoint/', methods=['GET'])
def search_endpoint() -> Response:
"""
Endpoint queried by the frontend search.
:return: Search results with references and verse content.
"""
version = request.args.get('version')
query = request.args.get('query')
if version not in bibles.keys():
return abort(400)
# Make sure the query is a reasonable length (max is 528 in the KJV for reference).
if len(query) > 700:
query = query[:700]
results = searcher.search(query, version=version, max_results=100)
references = {}
for result in results:
space_index = result.rfind(" ")
colon_index = result.rfind(":")
book, chapter_ref, verse_ref = \
result[0:space_index], int(result[space_index:colon_index]), result[colon_index + 1:] + " "
verses = bibles[version].get_passage(book, chapter_ref)['verses']
for heading in verses.keys():
for verse in verses[heading]:
if verse.startswith(verse_ref):
if result in references:
references[result] += verse[verse.find(" ") + 1:]
else:
references[result] = verse[verse.find(" ") + 1:]
reference_list = [(ref, val) for ref, val in references.items()]
return jsonify({'results': reference_list})
@cache
@app.route('/search', methods=['GET'])
def search() -> Response:
response = make_response(minify.sub('', render_template('search.html', title='search', debug=DEBUG, versions=bibles.keys())))
response.cache_control.max_age = 60 * 60 * 24 * 7
return response
@cache
@app.route('/manifest.json', methods=['GET'])
def manifest() -> Response:
return jsonify(
{
"lang": "en-us",
"name": "Self-hosted Bible",
"short_name": "Self-hosted Bible",
"start_url": request.url_root,
"id": request.url_root,
"display": "standalone",
"description": "A self-hosted webapp of various Bible versions including the KJV, ESV, and ASV.",
"orientation": "any",
"background_color": "#212435",
"theme_color": "#000",
"icons": [
{
"src": "static/favicon.svg",
"sizes": "any",
"type": "image/svg"
},
{
"src": "static/favicon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "static/favicon-512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"screenshots" : [
{
"src": "static/mobile_screenshot.jpg",
"sizes": "740x1460",
"type": "image/jpeg",
"form_factor": "narrow",
"label": "Home screen of mobile version"
},
{
"src": "static/home.jpg",
"sizes": "1118x863",
"type": "image/jpeg",
"form_factor": "wide",
"label": "Home screen of desktop version"
}
]
}
)
@app.route('/500', methods=['GET'])
@app.errorhandler(500)
def server_error(e=None) -> Tuple[str, int]:
"""
Error 500 handler
:param e: error
:return: error 500 page
"""
str(e)
return (minify.sub('',
render_template("500.html",
image=f"500_im_{randint(1, 5)}.jpg")),
500)
@app.route('/404', methods=['GET'])
@app.errorhandler(404)
def not_found(e = "") -> Tuple[str, int]:
"""
Error 404 handler
:param e: error
:return: error 404 page
"""
str(e)
return minify.sub('', render_template("404.html")), 404
@app.route('/400', methods=['GET'])
@app.errorhandler(400)
def bad_request(e="") -> Tuple[str, int]:
"""
Error 400 handler
:param e: error
:return: error 400 page
"""
str(e)
return minify.sub('', render_template("400.html")), 400
@app.errorhandler(414)
def request_too_long(e) -> Tuple[str, int]:
"""
Error 414 handler
:param e: error
:return: error 414 page
"""
str(e)
return minify.sub('', render_template("414.html")), 414
@app.route('/health', methods=['GET'])
def health() -> str:
"""
Docker Health check
:return: "Healthy: OK"
"""
return "Healthy: OK"
return app
if __name__ == '__main__':
DEBUG = True
# Dev start is also: `flask --app main.py run`
app_create: Flask = create_app()
app_create.run()
# serve(app, host="0.0.0.0", port=5000)