diff --git a/app.py b/app.py
index afe43a3..df61a7b 100644
--- a/app.py
+++ b/app.py
@@ -1,11 +1,12 @@
# app.py
from flask import Flask, request, jsonify, send_file
import random
-from datetime import date # used for daily verse
+from datetime import date # used for daily verse
import sqlite3
-import string # for literals
-import json # for dictionary
-import hunspell # for spell checking
+import string # for literals
+import json # for dictionary
+import hunspell # for spell checking
+
DEBUG = 0
"""
@@ -19,15 +20,22 @@ Bible Reading Plan
Bible Reader
"""
+
def dbg(message):
if DEBUG >= 1:
- print(f'\033[91m{message}\033[0m')
+ print(f"\033[91m{message}\033[0m")
+
+
def dbg2(message):
if DEBUG >= 2:
- print(f' \033[32m{message}\033[0m')
+ print(f" \033[32m{message}\033[0m")
+
+
def dbg3(message):
if DEBUG >= 3:
- print(f' \033[35m{message}\033[0m')
+ print(f" \033[35m{message}\033[0m")
+
+
dbg("Debug Level 1 Enabled")
dbg2("Debug Level 2 Enabled")
dbg3("Debug Level 3 Enabled")
@@ -45,40 +53,60 @@ idx_chapter = 2
idx_verse = 3
idx_text = 4
+
def lookup_one_verse(verse_id):
dbg2("lookup_one_verse({})".format(verse_id))
- return(kjv_cur.execute("SELECT * FROM fts_kjv WHERE id = {};".format(verse_id)).fetchone())
+ return kjv_cur.execute(
+ "SELECT * FROM fts_kjv WHERE id = {};".format(verse_id)
+ ).fetchone()
+
def lookup_bookname(book_id):
dbg3("lookup_bookname({})".format(book_id))
- result = kjv_cur.execute("SELECT n FROM key_english WHERE b = {};".format(book_id)).fetchone()
+ result = kjv_cur.execute(
+ "SELECT n FROM key_english WHERE b = {};".format(book_id)
+ ).fetchone()
if result:
result = result[0]
dbg2("lookup_bookname.result = '{}'".format(result))
return result
+
def lookup_book_id(bookname):
dbg3("lookup_book_id({})".format(bookname))
- result = kjv_cur.execute("SELECT b FROM key_english WHERE n = '{}';".format(bookname)).fetchone()
+ result = kjv_cur.execute(
+ "SELECT b FROM key_english WHERE n = '{}';".format(bookname)
+ ).fetchone()
if result:
result = result[0]
dbg3("lookup_book_id.result = '{}'".format(result))
return result
+
def lookup_fts(kwds):
dbg3("lookup_fts.kwds = '{}'".format(kwds))
return kjv_cur.execute("SELECT * FROM fts_kjv('{}')".format(kwds)).fetchall()
+
def lookup_contains(kw):
- results = kjv_cur.execute("SELECT * FROM fts_kjv WHERE t LIKE '%{}%';".format(kw)).fetchall()
- dbg2("lookup_contains({}) {} results".format(kw,len(results)))
+ results = kjv_cur.execute(
+ "SELECT * FROM fts_kjv WHERE t LIKE '%{}%';".format(kw)
+ ).fetchall()
+ dbg2("lookup_contains({}) {} results".format(kw, len(results)))
return results
-def lookup_by_verse_id(start_verse, end_verse = None):
+
+def lookup_by_verse_id(start_verse, end_verse=None):
if not end_verse:
- results = kjv_cur.execute("SELECT * FROM fts_kjv WHERE id = {};".format(start_verse)).fetchall()
+ results = kjv_cur.execute(
+ "SELECT * FROM fts_kjv WHERE id = {};".format(start_verse)
+ ).fetchall()
else:
- results = kjv_cur.execute("SELECT * FROM fts_kjv WHERE id BETWEEN {} AND {};".format(start_verse, end_verse)).fetchall()
+ results = kjv_cur.execute(
+ "SELECT * FROM fts_kjv WHERE id BETWEEN {} AND {};".format(
+ start_verse, end_verse
+ )
+ ).fetchall()
return results
@@ -86,13 +114,14 @@ def get_verse_id(book_id, chapter, verse):
# verse_id is 00000000 (00 000 000) (book + chapter + verse)
chapterid = chapter.zfill(3)
verseid = verse.zfill(3)
- return str(book_id)+chapterid+verseid
+ return str(book_id) + chapterid + verseid
+
def check_lone_word(text, kw):
kw_length = len(kw)
kw_location = text.find(kw)
try:
- if (text[kw_location-1]).isalpha():
+ if (text[kw_location - 1]).isalpha():
return False
except IndexError:
# If it's an indexerror, it's likely that
@@ -100,32 +129,34 @@ def check_lone_word(text, kw):
# of the text.
pass
try:
- if (text[kw_location+kw_length]).isalpha():
+ if (text[kw_location + kw_length]).isalpha():
return False
except IndexError:
pass
return True
+
def lookup_contains_exact(kw):
preresult = lookup_contains(kw)
results = []
for result in preresult:
if check_lone_word(result[idx_text], kw):
results.append(result)
- dbg("lookup_contains({}) {} results".format(kw,len(results)))
+ dbg("lookup_contains({}) {} results".format(kw, len(results)))
return results
+
def ref_input_cleaning(ref, valid_chars=(string.ascii_letters + string.digits + ":-,")):
dbg2("ref_input_cleaning({})".format(ref))
- ref = ref.replace(" ","")
- if ref.count(':') != 1:
+ ref = ref.replace(" ", "")
+ if ref.count(":") != 1:
dbg2("ref_input_cleaning.if ref.count(:): TRUE")
dbg2(" -> Assuming user wanted entire chapter")
ref += ":-"
if ref.count("-") > 1:
dbg2("ref_input_cleaning.if ref.count(-): TRUE")
return False
- if not ref[-1].isdigit() and ref[-1] != '-':
+ if not ref[-1].isdigit() and ref[-1] != "-":
dbg2("ref_input_cleaning.if ref[-1] TRUE")
return False
ref = list(ref)
@@ -134,7 +165,7 @@ def ref_input_cleaning(ref, valid_chars=(string.ascii_letters + string.digits +
if ref[0].isdigit() and ref[1].isalpha():
prefix = ref.pop(0) + " "
else:
- prefix = ''
+ prefix = ""
# clean the rest of the reference
cleaned_ref = []
for character in ref:
@@ -143,14 +174,18 @@ def ref_input_cleaning(ref, valid_chars=(string.ascii_letters + string.digits +
# Was there a problem?
if len(cleaned_ref) == 0:
return False
- return prefix + ''.join(cleaned_ref).capitalize()
+ return prefix + "".join(cleaned_ref).capitalize()
-def search_input_cleaning(query, valid_chars=string.ascii_letters + string.digits + "+^\" "):
+
+def search_input_cleaning(
+ query, valid_chars=string.ascii_letters + string.digits + '+^" '
+):
cleaned_query = []
for character in query:
if character in valid_chars:
cleaned_query.append(character)
- return ''.join(cleaned_query)
+ return "".join(cleaned_query)
+
def ref_parse_book(ref):
for book in reversed(book_names):
@@ -158,67 +193,78 @@ def ref_parse_book(ref):
return book
return False
+
@app.get("/find")
def parse_query():
query = request.args.get("kw", None)
if not query:
return send_file("html/find.html")
result = show()
- if not hasattr(result, '__len__'):
+ if not hasattr(result, "__len__"):
dbg3("parse_query.show = '{}'".format(result))
return result
if len(result) > 17:
return result
- result = ''
+ result = ""
if len(query.split()) == 1:
result += "
"
result += define() + "
"
+ result += open("js/collapsible.js", "r").read() + ""
dbg3("parse_query.define = '{}'".format(result))
result += search()
dbg3("parse_query.search = '{}'".format(result))
return result
+
@app.get("/define")
def define():
ref = request.args.get("kw", None)
- if ref: ref = ref.strip()
+ if ref:
+ ref = ref.strip()
if not ref:
return send_file("html/dictionary.html")
- with open(dictionary_json, 'r') as data:
+ with open(dictionary_json, "r") as data:
dictionary = json.load(data)
ref = ref.lower()
if not ref in dictionary.keys():
- h = hunspell.HunSpell("data/kjv.dic","data/kjv.aff")
+ h = hunspell.HunSpell("data/kjv.dic", "data/kjv.aff")
suggestions = h.suggest(ref)
- return "{} not found, try one of the following: {}".format(ref, ', '.join(suggestions))
+ return "{} not found, try one of the following: {}".format(
+ ref, ", ".join(suggestions)
+ )
arg_view = request.args.get("view", None)
if arg_view == "plain":
response = return_dict_plain(ref, dictionary[ref])
elif arg_view == "rich":
response = return_dict_rich(ref, dictionary[ref])
else:
- response = jsonify({ref:dictionary[ref]})
+ response = jsonify({ref: dictionary[ref]})
return response
+
def return_dict_plain(ref, defs):
- response = ''
+ response = ""
for e in defs:
response += e + "
"
return ref.capitalize() + "
" + response + "\n"
+
def return_dict_rich(ref, defs):
- header = open('html/header.html', 'r')
- css = open('styles/human_readable.css', 'r')
- response_html = open('html/response.html', 'r')
- response = ''
+ header = open("html/header.html", "r")
+ css = open("styles/human_readable.css", "r")
+ response_html = open("html/response.html", "r")
+ response = ""
for e in defs:
response += e + "
"
-# return (header.read() + response_html.read().format(passage = ref.capitalize(), text = response) + css.read())
- return (response_html.read().format(passage = ref.capitalize(), text = response) + css.read())
+ # return (header.read() + response_html.read().format(passage = ref.capitalize(), text = response) + css.read())
+ return (
+ response_html.read().format(passage=ref.capitalize(), text=response)
+ + css.read()
+ )
+
@app.get("/show")
def show():
@@ -227,28 +273,31 @@ def show():
return send_file("html/show.html")
dbg("show({})".format(ref))
refs = []
- for r in ref.split(','):
+ for r in ref.split(","):
r_ = ref_input_cleaning(r.strip())
if r_:
refs.append(r_)
if not refs:
- return("Invalid Query")
+ return "Invalid Query"
verse_results = []
- #check if the search is for multiple refs
+ # check if the search is for multiple refs
for ref in refs:
dbg("show.reference({})".format(ref))
- maybe_chapter = ref.split(':')[0][3:]
- ref_chapter = ''
+ maybe_chapter = ref.split(":")[0][3:]
+ ref_chapter = ""
for maybedigit in maybe_chapter:
if maybedigit.isdigit():
ref_chapter += maybedigit
- ref_verse = ref.split(':')[1]
- if ref.__contains__('-'):
- if ref.endswith('-'):
+ ref_verse = ref.split(":")[1]
+ if ref.__contains__("-"):
+ if ref.endswith("-"):
start_verse = ref_verse.split("-")[0]
- end_verse = '500' # just get all the verses if it ends with a dash
+ end_verse = "500" # just get all the verses if it ends with a dash
else:
- start_verse, end_verse = ref_verse.split("-")[0], ref_verse.split("-")[1]
+ start_verse, end_verse = (
+ ref_verse.split("-")[0],
+ ref_verse.split("-")[1],
+ )
else:
start_verse = ref_verse
end_verse = None
@@ -257,7 +306,7 @@ def show():
ref_bookname = ref_parse_book(book_str)
ref_book_id = lookup_book_id(ref_bookname)
if not ref_book_id:
- return("Invalid Query")
+ return "Invalid Query"
start_verse_id = get_verse_id(ref_book_id, ref_chapter, start_verse)
if end_verse:
end_verse_id = get_verse_id(ref_book_id, ref_chapter, end_verse)
@@ -284,85 +333,97 @@ def response_dict(scripture):
"verse": scripture[idx_verse],
"text": scripture[idx_text],
}
- return (verse)
+ return verse
+
def response_text(scripture):
bookname = lookup_bookname(scripture[idx_book])
chapter = str(scripture[idx_chapter])
verse = str(scripture[idx_verse])
text = str(scripture[idx_text])
- return (bookname + " " + chapter + ":" + verse + " " + text + "\n")
+ return bookname + " " + chapter + ":" + verse + " " + text + "\n"
+
def response_rich(scripture):
- css = open('styles/human_readable.css', 'r')
- response_html = open('html/response.html', 'r')
+ css = open("styles/human_readable.css", "r")
+ response_html = open("html/response.html", "r")
# (passage, text) is the html formatting arguments
bookname = lookup_bookname(scripture[idx_book])
chapter = str(scripture[idx_chapter])
verse = str(scripture[idx_verse])
text = str(scripture[idx_text])
- passage = (bookname + " " + chapter + ":" + verse)
- return (response_html.read().format(passage = passage, text = text) + css.read())
+ passage = bookname + " " + chapter + ":" + verse
+ return response_html.read().format(passage=passage, text=text) + css.read()
+
def response_single(scripture):
dbg2("response_single({})".format(scripture))
- arg_view = request.args.get("view", 'json')
+ arg_view = request.args.get("view", "json")
dbg("->requested_format = {}".format(arg_view))
if arg_view == "plain":
- return(response_text(scripture))
+ return response_text(scripture)
elif arg_view == "rich":
- header = open('html/header.html', 'r')
- return(header.read() + response_rich(scripture))
+ header = open("html/header.html", "r")
+ return header.read() + response_rich(scripture)
else:
response = response_dict(scripture)
return jsonify(response)
+
def response_multi(scriptures):
response = []
- arg_view = request.args.get("view", 'json')
+ arg_view = request.args.get("view", "json")
dbg("search ->requested_format = {}".format(arg_view))
if arg_view == "plain":
for scripture in scriptures:
response.append(response_text(scripture))
elif arg_view == "rich":
- response.append(open('html/header.html', 'r').read())
+ response.append(open("html/header.html", "r").read())
for scripture in scriptures:
response.append(response_rich(scripture))
else:
for scripture in scriptures:
response.append(response_dict(scripture))
- return(response)
+ return response
+
@app.errorhandler(404)
def page_not_found(e):
- message = {"error": "You're lost! Isaiah 41:10 - Fear thou not; for I am with thee: be not dismayed; for I am thy God: I will strengthen thee; yea, I will help thee; yea, I will uphold thee with the right hand of my righteousness."}
+ message = {
+ "error": "You're lost! Isaiah 41:10 - Fear thou not; for I am with thee: be not dismayed; for I am thy God: I will strengthen thee; yea, I will help thee; yea, I will uphold thee with the right hand of my righteousness."
+ }
return jsonify(message)
+
@app.get("/")
def idx():
return send_file("html/index.html")
+
@app.get("/salvation")
def salvation():
return send_file("html/heaven.html")
-#backwards-compatibility
-@app.get('/t/random', defaults={'view':'plain'})
-@app.get('/random')
+
+# backwards-compatibility
+@app.get("/t/random", defaults={"view": "plain"})
+@app.get("/random")
def random_verse():
rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id)
- return(response_single(scripture))
+ return response_single(scripture)
-#backwards-compatibility
-@app.get('/t/votd', defaults={'view':'plain'})
-@app.get('/votd')
+
+# backwards-compatibility
+@app.get("/t/votd", defaults={"view": "plain"})
+@app.get("/votd")
def verse_of_the_day():
- today = int(str(date.today()).replace('-',''))
- #this is deterministic because of today's date being used as the seed
+ today = int(str(date.today()).replace("-", ""))
+ # this is deterministic because of today's date being used as the seed
random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids))
- return(response_single(scripture))
+ return response_single(scripture)
+
@app.get("/search")
def search():
@@ -376,9 +437,11 @@ def search():
return send_file("html/search.html")
results = lookup_fts(cleaned_keywords)
if not results:
- h = hunspell.HunSpell("data/kjv.dic","data/kjv.aff")
+ h = hunspell.HunSpell("data/kjv.dic", "data/kjv.aff")
suggestions = h.suggest(cleaned_keywords)
- return "{} not found, try one of the following: {}".format(cleaned_keywords, ', '.join(suggestions))
+ return "{} not found, try one of the following: {}".format(
+ cleaned_keywords, ", ".join(suggestions)
+ )
response_list = response_multi(results)
arg_view = request.args.get("view", None)
if arg_view == "plain" or arg_view == "rich":
@@ -388,6 +451,7 @@ def search():
dbg2("Search Response: {}".format(response))
return response
+
@app.get("/reader")
def bible_reader():
return send_file("html/reader.html")
@@ -399,6 +463,7 @@ def bible_reader():
# Footer should have next/previous as well.
# Maybe preliminary theming support
+
@app.get("/plan")
def reading_plan():
return send_file("html/reading_plan.html")