diff --git a/app.py b/app.py index ba59a97..afe43a3 100644 --- a/app.py +++ b/app.py @@ -6,25 +6,31 @@ import sqlite3 import string # for literals import json # for dictionary import hunspell # for spell checking -DEBUG = 2 +DEBUG = 0 """ To-do: +Separate dictionary-dictionary for hunsepll +Search phrase alias +Response Bible book reading +Search highlighting Styles Bible Reading Plan -Bible Viewer +Bible Reader """ def dbg(message): - # if DEBUG is at or above a value of one, a call from debug level one will print red if DEBUG >= 1: print(f'\033[91m{message}\033[0m') def dbg2(message): - # if DEBUG is at or above a value of two, a call from debug level one will print green 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') dbg("Debug Level 1 Enabled") dbg2("Debug Level 2 Enabled") +dbg3("Debug Level 3 Enabled") dictionary_json = "data/1828_Webster_KJV.json" kjv_cur = sqlite3.connect("data/kjv.db").cursor() @@ -40,31 +46,32 @@ idx_verse = 3 idx_text = 4 def lookup_one_verse(verse_id): - dbg("lookup_one_verse({})".format(verse_id)) + dbg2("lookup_one_verse({})".format(verse_id)) return(kjv_cur.execute("SELECT * FROM fts_kjv WHERE id = {};".format(verse_id)).fetchone()) def lookup_bookname(book_id): - dbg2("lookup_bookname({})".format(book_id)) + dbg3("lookup_bookname({})".format(book_id)) result = kjv_cur.execute("SELECT n FROM key_english WHERE b = {};".format(book_id)).fetchone() if result: result = result[0] - dbg("lookup_bookname.result = '{}'".format(result)) + dbg2("lookup_bookname.result = '{}'".format(result)) return result def lookup_book_id(bookname): - dbg2("lookup_book_id({})".format(bookname)) + dbg3("lookup_book_id({})".format(bookname)) result = kjv_cur.execute("SELECT b FROM key_english WHERE n = '{}';".format(bookname)).fetchone() if result: result = result[0] - dbg2("lookup_book_id.result = '{}'".format(result)) + 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() - dbg("lookup_contains({}) {} results".format(kw,len(results))) + dbg2("lookup_contains({}) {} results".format(kw,len(results))) return results def lookup_by_verse_id(start_verse, end_verse = None): @@ -110,16 +117,18 @@ def lookup_contains_exact(kw): def ref_input_cleaning(ref, valid_chars=(string.ascii_letters + string.digits + ":-,")): dbg2("ref_input_cleaning({})".format(ref)) - ref = list(ref.replace(" ","")) + ref = ref.replace(" ","") if ref.count(':') != 1: dbg2("ref_input_cleaning.if ref.count(:): TRUE") - return False + 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] != '-': dbg2("ref_input_cleaning.if ref[-1] TRUE") return False + ref = list(ref) # preserve book number if we can guess the first # character is a number and 3nd is a letter if ref[0].isdigit() and ref[1].isalpha(): @@ -149,6 +158,30 @@ 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__'): + dbg3("parse_query.show = '{}'".format(result)) + return result + + if len(result) > 17: + return result + result = '' + if len(query.split()) == 1: + result += "
" + result += define() + "
" + 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) @@ -175,7 +208,7 @@ def return_dict_plain(ref, defs): response = '' for e in defs: response += e + "

" - return ref.capitalize() + "

" + response + return ref.capitalize() + "

" + response + "\n" def return_dict_rich(ref, defs): header = open('html/header.html', 'r') @@ -184,7 +217,8 @@ def return_dict_rich(ref, defs): response = '' for e in defs: response += e + "

" - return (header.read() + 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(): @@ -197,7 +231,7 @@ def show(): r_ = ref_input_cleaning(r.strip()) if r_: refs.append(r_) - if not ref: + if not refs: return("Invalid Query") verse_results = [] #check if the search is for multiple refs @@ -223,14 +257,14 @@ def show(): ref_bookname = ref_parse_book(book_str) ref_book_id = lookup_book_id(ref_bookname) if not ref_book_id: - return("Book reference invalid. Ex: 1John3:16") + 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) dbg2("show.start_verse_id({})".format(start_verse_id)) dbg2("show.end_verse_id({})".format(end_verse_id)) results = lookup_by_verse_id(start_verse_id, end_verse_id) - dbg2("show.results({})".format(results)) + dbg3("show.results({})".format(results)) for result in results: verse_results.append(result) response_list = response_multi(verse_results) @@ -257,7 +291,7 @@ def response_text(scripture): chapter = str(scripture[idx_chapter]) verse = str(scripture[idx_verse]) text = str(scripture[idx_text]) - return (bookname + " " + chapter + ":" + verse + " " + text) + return (bookname + " " + chapter + ":" + verse + " " + text + "\n") def response_rich(scripture): css = open('styles/human_readable.css', 'r') @@ -353,3 +387,22 @@ def search(): response = jsonify(response_list) dbg2("Search Response: {}".format(response)) return response + +@app.get("/reader") +def bible_reader(): + return send_file("html/reader.html") + # We should return an index of all books with + # drop down menus of each chapter, and a search + # bar at the top for quick seeking, which can use + # show(). Each page should return a header and footer. + # header should have a navigation bar and "next/previous chapter" + # Footer should have next/previous as well. + # Maybe preliminary theming support + +@app.get("/plan") +def reading_plan(): + return send_file("html/reading_plan.html") + # needs 2 sets of data, books and time frame. + # should default to the entire Bible in 1 year. + # should start today() and end today()+1year + # Should return a calender with date and verse range. diff --git a/html/find.html b/html/find.html new file mode 100644 index 0000000..9d4bd98 --- /dev/null +++ b/html/find.html @@ -0,0 +1,13 @@ +
+Home | Quick Search | +Random Verse | +Verse of the Day
+

Quick Search

+
+
+

+ + Rich
+ + + diff --git a/html/header.html b/html/header.html index 428dc70..242b420 100644 --- a/html/header.html +++ b/html/header.html @@ -1,9 +1,4 @@ -
-Home -Bible Search -Bible Verses -Bible Define -Random Verse -Verse of the Day -
+Home | Quick Search | +Random Verse | +Verse of the Day diff --git a/html/index.html b/html/index.html index 934877e..45a61d8 100644 --- a/html/index.html +++ b/html/index.html @@ -13,43 +13,36 @@ html{

KJV API

KJV accessible via web calls

-

Usage

-

URL Root: https://api.1611.social/ (you are here)

- +

Quick Search

+ +
+

-

Verse searching

-

'/show?kw=' will try to match single verses and verse ranges. (not sets) For example:

-
-
-/show?kw=john3:16
-/show?kw=1 John 3:3- (gets the whole rest of the chapter)
-
-
+