JSON flat file to sqlite3 data source querying

This commit is contained in:
2022-12-30 19:28:32 -08:00
parent f92fb7ad30
commit ede1c816f1
7 changed files with 49 additions and 500 deletions
+1
View File
@@ -3,6 +3,7 @@ Tested with Dependancy:Version
======================= =======================
python3:3.10.6-1~22.04 python3:3.10.6-1~22.04
python3-flask:2.0.1-2ubuntu1 python3-flask:2.0.1-2ubuntu1
sqlite3:3.37.2-2ubuntu0.1
bible-kjv:4.37 bible-kjv:4.37
uwsgi-core:2.0.20-4 uwsgi-core:2.0.20-4
uwsgi-plugin-python3:2.0.20-4 uwsgi-plugin-python3:2.0.20-4
+7 -6
View File
@@ -7,11 +7,11 @@ verses and a quote of the day.
EXTERNAL DATA EXTERNAL DATA
============= =============
The software uses these JSON data stores as external dependancies, saved locally (included) The software uses a sqlite3 database as external data dependancy, saved locally
https://github.com/scrollmapper/bible_databases/blob/master/json/t_kjv.json and modified to remove false Bible translations (included)
https://github.com/scrollmapper/bible_databases/blob/master/json/key_english.json https://github.com/scrollmapper/bible_databases
For additional dependancies see INSTALL.txt For software dependancies see INSTALL.txt
INSTALLATION INSTALLATION
@@ -24,6 +24,7 @@ The program may be invoked by running kjv-api.sh in a non-root account, or via
systemd daemon process in a non-root account. systemd daemon process in a non-root account.
Alternately for testing, the program may be run on another port by running: Alternately for testing, the program may be run on another port by running:
$ uwsgi_python3 --http-socket <listen address>:<port> --wsgi-file /opt/kjv-api/app.py --callable app $ uwsgi_python3 --http-socket <listen address>:<port> --wsgi-file /opt/kjv-api/app.py --callable app
or by executing kjv-api-test.sh which will listen on 0.0.0.0:1612
Visiting the base directory will show the user-facing documentation via web browser or cURL. Visiting the base directory will show the user-facing documentation via web browser or cURL.
@@ -31,11 +32,11 @@ CONTENTS
======== ========
app.py - main flask program app.py - main flask program
install/ - folder containing sample configurations install/ - folder containing sample configurations
key_english.json - King James Bible books by index in JSON kjv.db - King James Bible database
t_kjv.json - King James Bible in JSON
index.html - root document help file - user-facing documentation index.html - root document help file - user-facing documentation
INSTALL.txt - detailed installation instructions INSTALL.txt - detailed installation instructions
kjv-api.sh - script that properly runs flask through uwsgi kjv-api.sh - script that properly runs flask through uwsgi
kjv-api-test.sh - script that properly runs flask through uwsgi on port 1612 and global listening
README.txt - this file README.txt - this file
+39 -25
View File
@@ -1,48 +1,50 @@
# app.py # app.py
from flask import Flask, request, jsonify, send_file from flask import Flask, request, jsonify, send_file
import random, json import random
from datetime import date # used for daily verse from datetime import date # used for daily verse
import subprocess # use of "bible" system application for parsing and returning Bible in KJV0 import subprocess # use of "bible" system application for parsing and returning Bible in KJV0
import shlex # input security import shlex # input security
import sqlite3
kjv_db = sqlite3.connect("kjv.db")
kjv_cur = kjv.cursor()
verse_ids = kjv_cur.execute("SELECT id FROM t_kjv;").fetchall()
app = Flask(__name__) app = Flask(__name__)
blank_kjv = {"bookname":"zero","chapter":0,"text":"zero","verse":0} idx_id = 0
blank_key = {'b': 0, 'c': 0, 'n': 'Blank', 't': 'XX', 'g': 0}
kjv = json.load(open('/opt/kjv-api/t_kjv.json','r'))
kjv = kjv['resultset']['row']
kjv.insert(0,blank_kjv)
key = json.load(open('/opt/kjv-api/key_english.json','r'))
key = key['resultset']['keys']
key.insert(0,blank_key)
idx_book = 1 idx_book = 1
idx_chapter = 2 idx_chapter = 2
idx_verse = 3 idx_verse = 3
idx_text = 4 idx_text = 4
def lookup_one_verse(id):
return(kjv_cur.execute("SELECT * FROM t_kjv WHERE id = {};".format(id)).fetchone())
def lookup_bookname(id):
return kjv_cur.execute("SELECT n FROM key_english WHERE b = {}".format(id)).fetchone()
def respond_json(scripture): def respond_json(scripture):
kjv = { verse = {
"bookname": key[scripture[idx_book]]['n'], "bookname": lookup_bookname(scripture[idx_book]),
"chapter": scripture[idx_chapter], "chapter": scripture[idx_chapter],
"verse": scripture[idx_verse], "verse": scripture[idx_verse],
"text": scripture[idx_text], "text": scripture[idx_text],
} }
return jsonify(kjv) return jsonify(verse)
def respond_text(scripture): def respond_text(scripture):
book = str(key[scripture[idx_book]]['n']) bookname = lookup_bookname(scripture[idx_book])
chapter = str(scripture[idx_chapter]) chapter = str(scripture[idx_chapter])
verse = str(scripture[idx_verse]) verse = str(scripture[idx_verse])
text = str(scripture[idx_text]) text = str(scripture[idx_text])
return (book + " " + chapter + ":" + verse + " " + text) return (bookname + " " + chapter + ":" + verse + " " + text)
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(e): 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) return jsonify(message)
@app.get("/") @app.get("/")
@@ -56,14 +58,16 @@ def test():
# json # json
@app.get("/random") @app.get("/random")
def random_verse_json(): def random_verse_json():
scripture = random.SystemRandom().choice(kjv)['field'] rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id))
return(respond_json(scripture)) return(respond_json(scripture))
# plain # plain
@app.get("/t/random") @app.get("/t/random")
def random_verse_plain(): def random_verse_plain():
scripture = random.SystemRandom().choice(kjv)['field'] rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id))
return(respond_text(scripture)) return(respond_text(scripture))
@@ -76,8 +80,8 @@ def dbg(ref):
def verse_of_the_day(): def verse_of_the_day():
today = int(str(date.today()).replace('-','')) today = int(str(date.today()).replace('-',''))
#this is deterministic because of today's date being used as the seed #this is deterministic because of today's date being used as the seed
todays_verse_idx = random.seed(today) random.seed(today)
scripture = random.choice(kjv)['field'] scripture = lookup_one_verse(random.choice(verse_ids)))
return(respond_json(scripture)) return(respond_json(scripture))
@@ -86,8 +90,8 @@ def verse_of_the_day():
def verse_of_the_day_text(): def verse_of_the_day_text():
today = int(str(date.today()).replace('-','')) today = int(str(date.today()).replace('-',''))
#this is deterministic because of today's date being used as the seed #this is deterministic because of today's date being used as the seed
todays_verse_idx = random.seed(today) random.seed(today)
scripture = random.choice(kjv)['field'] scripture = lookup_one_verse(random.choice(verse_ids)))
return(respond_text(scripture)) return(respond_text(scripture))
@@ -125,6 +129,8 @@ def search_json(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8") result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0: if len(result) == 0:
raise Exception("You have entered an incorrect syntax or your query returned no results.") raise Exception("You have entered an incorrect syntax or your query returned no results.")
if result.contains("??word"):
raise Exception("Not found.")
result = result.split() result = result.split()
result = result[7:] result = result[7:]
return jsonify(result) return jsonify(result)
@@ -139,6 +145,8 @@ def search_text(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8") result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0: if len(result) == 0:
raise Exception("You have entered an incorrect syntax or your query returned no results.") raise Exception("You have entered an incorrect syntax or your query returned no results.")
if result.contains("??word"):
raise Exception("Not found.")
result = result.split() result = result.split()
result = result[7:] result = result[7:]
result = '<br>'.join(map(str, result)) result = '<br>'.join(map(str, result))
@@ -154,6 +162,8 @@ def search_json_return(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8") result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0: if len(result) == 0:
raise Exception("You have entered an incorrect syntax or your query returned no results.") raise Exception("You have entered an incorrect syntax or your query returned no results.")
if result.contains("??word"):
raise Exception("Not found.")
result = result.split() result = result.split()
result = result[7:] result = result[7:]
complete_result = [] complete_result = []
@@ -171,6 +181,8 @@ def search_text_return(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8") result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0: if len(result) == 0:
raise Exception("You have entered an incorrect syntax or your query returned no results.") raise Exception("You have entered an incorrect syntax or your query returned no results.")
if result.contains("??word"):
raise Exception("Not found.")
result = result.split() result = result.split()
result = result[7:] result = result[7:]
complete_result = [] complete_result = []
@@ -191,6 +203,8 @@ def search_text_return_multiple(keywords):
result = str(subprocess.check_output(command, shell=True), "utf-8") result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0: if len(result) == 0:
raise Exception("You have entered an incorrect syntax or your query returned no results.") raise Exception("You have entered an incorrect syntax or your query returned no results.")
if result.contains("??word"):
raise Exception("Not found.")
result = result.split() result = result.split()
result = result[7:] result = result[7:]
complete_result = [] complete_result = []
-468
View File
@@ -1,468 +0,0 @@
{
"resultset": {
"keys": [
{
"b": 1,
"c": 50,
"n": "Genesis",
"t": "OT",
"g": 1
},
{
"b": 2,
"c": 40,
"n": "Exodus",
"t": "OT",
"g": 1
},
{
"b": 3,
"c": 27,
"n": "Leviticus",
"t": "OT",
"g": 1
},
{
"b": 4,
"c": 36,
"n": "Numbers",
"t": "OT",
"g": 1
},
{
"b": 5,
"c": 34,
"n": "Deuteronomy",
"t": "OT",
"g": 1
},
{
"b": 6,
"c": 24,
"n": "Joshua",
"t": "OT",
"g": 2
},
{
"b": 7,
"c": 21,
"n": "Judges",
"t": "OT",
"g": 2
},
{
"b": 8,
"c": 4,
"n": "Ruth",
"t": "OT",
"g": 2
},
{
"b": 9,
"c": 31,
"n": "1 Samuel",
"t": "OT",
"g": 2
},
{
"b": 10,
"c": 24,
"n": "2 Samuel",
"t": "OT",
"g": 2
},
{
"b": 11,
"c": 22,
"n": "1 Kings",
"t": "OT",
"g": 2
},
{
"b": 12,
"c": 25,
"n": "2 Kings",
"t": "OT",
"g": 2
},
{
"b": 13,
"c": 29,
"n": "1 Chronicles",
"t": "OT",
"g": 2
},
{
"b": 14,
"c": 36,
"n": "2 Chronicles",
"t": "OT",
"g": 2
},
{
"b": 15,
"c": 10,
"n": "Ezra",
"t": "OT",
"g": 2
},
{
"b": 16,
"c": 13,
"n": "Nehemiah",
"t": "OT",
"g": 2
},
{
"b": 17,
"c": 10,
"n": "Esther",
"t": "OT",
"g": 2
},
{
"b": 18,
"c": 42,
"n": "Job",
"t": "OT",
"g": 3
},
{
"b": 19,
"c": 150,
"n": "Psalms",
"t": "OT",
"g": 3
},
{
"b": 20,
"c": 31,
"n": "Proverbs",
"t": "OT",
"g": 3
},
{
"b": 21,
"c": 12,
"n": "Ecclesiastes",
"t": "OT",
"g": 3
},
{
"b": 22,
"c": 8,
"n": "Song of Solomon",
"t": "OT",
"g": 3
},
{
"b": 23,
"c": 66,
"n": "Isaiah",
"t": "OT",
"g": 4
},
{
"b": 24,
"c": 52,
"n": "Jeremiah",
"t": "OT",
"g": 4
},
{
"b": 25,
"c": 5,
"n": "Lamentations",
"t": "OT",
"g": 4
},
{
"b": 26,
"c": 48,
"n": "Ezekiel",
"t": "OT",
"g": 4
},
{
"b": 27,
"c": 12,
"n": "Daniel",
"t": "OT",
"g": 4
},
{
"b": 28,
"c": 14,
"n": "Hosea",
"t": "OT",
"g": 4
},
{
"b": 29,
"c": 3,
"n": "Joel",
"t": "OT",
"g": 4
},
{
"b": 30,
"c": 9,
"n": "Amos",
"t": "OT",
"g": 4
},
{
"b": 31,
"c": 1,
"n": "Obadiah",
"t": "OT",
"g": 4
},
{
"b": 32,
"c": 4,
"n": "Jonah",
"t": "OT",
"g": 4
},
{
"b": 33,
"c": 7,
"n": "Micah",
"t": "OT",
"g": 4
},
{
"b": 34,
"c": 3,
"n": "Nahum",
"t": "OT",
"g": 4
},
{
"b": 35,
"c": 3,
"n": "Habakkuk",
"t": "OT",
"g": 4
},
{
"b": 36,
"c": 3,
"n": "Zephaniah",
"t": "OT",
"g": 4
},
{
"b": 37,
"c": 2,
"n": "Haggai",
"t": "OT",
"g": 4
},
{
"b": 38,
"c": 14,
"n": "Zechariah",
"t": "OT",
"g": 4
},
{
"b": 39,
"c": 4,
"n": "Malachi",
"t": "OT",
"g": 4
},
{
"b": 40,
"c": 28,
"n": "Matthew",
"t": "NT",
"g": 5
},
{
"b": 41,
"c": 16,
"n": "Mark",
"t": "NT",
"g": 5
},
{
"b": 42,
"c": 24,
"n": "Luke",
"t": "NT",
"g": 5
},
{
"b": 43,
"c": 21,
"n": "John",
"t": "NT",
"g": 5
},
{
"b": 44,
"c": 28,
"n": "Acts",
"t": "NT",
"g": 6
},
{
"b": 45,
"c": 16,
"n": "Romans",
"t": "NT",
"g": 7
},
{
"b": 46,
"c": 16,
"n": "1 Corinthians",
"t": "NT",
"g": 7
},
{
"b": 47,
"c": 13,
"n": "2 Corinthians",
"t": "NT",
"g": 7
},
{
"b": 48,
"c": 6,
"n": "Galatians",
"t": "NT",
"g": 7
},
{
"b": 49,
"c": 6,
"n": "Ephesians",
"t": "NT",
"g": 7
},
{
"b": 50,
"c": 4,
"n": "Philippians",
"t": "NT",
"g": 7
},
{
"b": 51,
"c": 4,
"n": "Colossians",
"t": "NT",
"g": 7
},
{
"b": 52,
"c": 5,
"n": "1 Thessalonians",
"t": "NT",
"g": 7
},
{
"b": 53,
"c": 3,
"n": "2 Thessalonians",
"t": "NT",
"g": 7
},
{
"b": 54,
"c": 6,
"n": "1 Timothy",
"t": "NT",
"g": 7
},
{
"b": 55,
"c": 4,
"n": "2 Timothy",
"t": "NT",
"g": 7
},
{
"b": 56,
"c": 3,
"n": "Titus",
"t": "NT",
"g": 7
},
{
"b": 57,
"c": 1,
"n": "Philemon",
"t": "NT",
"g": 7
},
{
"b": 58,
"c": 13,
"n": "Hebrews",
"t": "NT",
"g": 7
},
{
"b": 59,
"c": 5,
"n": "James",
"t": "NT",
"g": 7
},
{
"b": 60,
"c": 5,
"n": "1 Peter",
"t": "NT",
"g": 7
},
{
"b": 61,
"c": 3,
"n": "2 Peter",
"t": "NT",
"g": 7
},
{
"b": 62,
"c": 5,
"n": "1 John",
"t": "NT",
"g": 7
},
{
"b": 63,
"c": 1,
"n": "2 John",
"t": "NT",
"g": 7
},
{
"b": 64,
"c": 1,
"n": "3 John",
"t": "NT",
"g": 7
},
{
"b": 65,
"c": 1,
"n": "Jude",
"t": "NT",
"g": 7
},
{
"b": 66,
"c": 22,
"n": "Revelation",
"t": "NT",
"g": 8
}
]
}
}
+2
View File
@@ -0,0 +1,2 @@
#!/bin/sh
uwsgi_python3 --http-socket 0.0.0.0:1612 --wsgi-file /opt/kjv-api/app.py --callable app
BIN
View File
Binary file not shown.
-1
View File
File diff suppressed because one or more lines are too long