JSON flat file to sqlite3 data source querying

This commit is contained in:
tyler committed 2022-12-30 19:28:32 -08:00
1 parent f92fb7ad30
commit ede1c816f1
7 files changed
+49 -500

No files matched your search

+1
View File
@@ -3,6 +3,7 @@ Tested with Dependancy:Version
=======================
python3:3.10.6-1~22.04
python3-flask:2.0.1-2ubuntu1
sqlite3:3.37.2-2ubuntu0.1
bible-kjv:4.37
uwsgi-core: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
=============
The software uses these JSON data stores as external dependancies, saved locally (included)
https://github.com/scrollmapper/bible_databases/blob/master/json/t_kjv.json
https://github.com/scrollmapper/bible_databases/blob/master/json/key_english.json
The software uses a sqlite3 database as external data dependancy, saved locally
and modified to remove false Bible translations (included)
https://github.com/scrollmapper/bible_databases
For additional dependancies see INSTALL.txt
For software dependancies see INSTALL.txt
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.
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
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.
@@ -31,11 +32,11 @@ CONTENTS
========
app.py - main flask program
install/ - folder containing sample configurations
key_english.json - King James Bible books by index in JSON
t_kjv.json - King James Bible in JSON
kjv.db - King James Bible database
index.html - root document help file - user-facing documentation
INSTALL.txt - detailed installation instructions
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
+39 -25
View File
@@ -1,48 +1,50 @@
# app.py
from flask import Flask, request, jsonify, send_file
import random, json
import random
from datetime import date # used for daily verse
import subprocess # use of "bible" system application for parsing and returning Bible in KJV0
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__)
blank_kjv = {"bookname":"zero","chapter":0,"text":"zero","verse":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_id = 0
idx_book = 1
idx_chapter = 2
idx_verse = 3
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):
kjv = {
"bookname": key[scripture[idx_book]]['n'],
verse = {
"bookname": lookup_bookname(scripture[idx_book]),
"chapter": scripture[idx_chapter],
"verse": scripture[idx_verse],
"text": scripture[idx_text],
}
return jsonify(kjv)
return jsonify(verse)
def respond_text(scripture):
book = str(key[scripture[idx_book]]['n'])
bookname = lookup_bookname(scripture[idx_book])
chapter = str(scripture[idx_chapter])
verse = str(scripture[idx_verse])
text = str(scripture[idx_text])
return (book + " " + chapter + ":" + verse + " " + text)
return (bookname + " " + chapter + ":" + verse + " " + text)
@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("/")
@@ -56,14 +58,16 @@ def test():
# json
@app.get("/random")
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))
# plain
@app.get("/t/random")
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))
@@ -76,8 +80,8 @@ def dbg(ref):
def verse_of_the_day():
today = int(str(date.today()).replace('-',''))
#this is deterministic because of today's date being used as the seed
todays_verse_idx = random.seed(today)
scripture = random.choice(kjv)['field']
random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids)))
return(respond_json(scripture))
@@ -86,8 +90,8 @@ def verse_of_the_day():
def verse_of_the_day_text():
today = int(str(date.today()).replace('-',''))
#this is deterministic because of today's date being used as the seed
todays_verse_idx = random.seed(today)
scripture = random.choice(kjv)['field']
random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids)))
return(respond_text(scripture))
@@ -125,6 +129,8 @@ def search_json(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0:
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[7:]
return jsonify(result)
@@ -139,6 +145,8 @@ def search_text(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0:
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[7:]
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")
if len(result) == 0:
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[7:]
complete_result = []
@@ -171,6 +181,8 @@ def search_text_return(keyword):
result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0:
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[7:]
complete_result = []
@@ -191,6 +203,8 @@ def search_text_return_multiple(keywords):
result = str(subprocess.check_output(command, shell=True), "utf-8")
if len(result) == 0:
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[7:]
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
@@ -1 +0,0 @@
{ "resultset": { "row": [{ "field": [1001001, 1, 1, 1, "In the beginning God created the heaven and the earth."] }, { "field": [1001002, 1, 1, 2, "And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."] }, { "field": [1001003, 1, 1, 3, "And God said, Let there be light: and there was light."] }, { "field": [1001004, 1, 1, 4, "And God saw the light, that it was good: and God divided the light from the darkness."] }, { "field": [1001005, 1, 1, 5, "And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."] }, { "field": [1001006, 1, 1, 6, "And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters."] }, { "field": [1001007, 1, 1, 7, "And God made the firmament, and divided the waters which were under the firmament from the waters which were above the firmament: and it was so."] }, { "field": [1001008, 1, 1, 8, "And God called the firmament Heaven. And the evening and the morning were the second day."] }, { "field": [1001009, 1, 1, 9, "And God said, Let the waters under the heaven be gathered together unto one place, and let the dry land appear: and it was so."] }, { "field": [1001010, 1, 1, 10, "And God called the dry land Earth; and the gathering together of the waters called he Seas: and God saw that it was good."] }, { "field": [1001011, 1, 1, 11, "And God said, Let the earth bring forth grass, the herb yielding seed, and the fruit tree yielding fruit after his kind, whose seed is in itself, upon the earth: and it was so."] }, { "field": [1001012, 1, 1, 12, "And the earth brought forth grass, and herb yielding seed after his kind, and the tree yielding fruit, whose seed was in itself, after his kind: and God saw that it was good."] }, { "field": [1001013, 1, 1, 13, "And the evening and the morning were the third day."] }, { "field": [1001014, 1, 1, 14, "And God said, Let there be lights in the firmament of the heaven to divide the day from the night; and let them be for signs, and for seasons, and for days, and years:"] }, { "field": [1001015, 1, 1, 15, "And let them be for lights in the firmament of the heaven to give light upon the earth: and it was so."] }, { "field": [1001016, 1, 1, 16, "And God made two great lights; the greater light to rule the day, and the lesser light to rule the night: he made the stars also."] }, { "field": [1001017, 1, 1, 17, "And God set them in the firmament of the heaven to give light upon the earth,"] }, { "field": [1001018, 1, 1, 18, "And to rule over the day and over the night, and to divide the light from the darkness: and God saw that it was good."] }, { "field": [1001019, 1, 1, 19, "And the evening and the morning were the fourth day."] }, { "field": [1001020, 1, 1, 20, "And God said, Let the waters bring forth abundantly the moving creature that hath life, and fowl that may fly above the earth in the open firmament of heaven."] }, { "field": [1001021, 1, 1, 21, "And God created great whales, and every living creature that moveth, which the waters brought forth abundantly, after their kind, and every winged fowl after his kind: and God saw that it was good."] }, { "field": [1001022, 1, 1, 22, "And God blessed them, saying, Be fruitful, and multiply, and fill the waters in the seas, and let fowl multiply in the earth."] }, { "field": [1001023, 1, 1, 23, "And the evening and the morning were the fifth day."] }, { "field": [1001024, 1, 1, 24, "And God said, Let the earth bring forth the living creature after his kind, cattle, and creeping thing, and beast of the earth after his kind: and it was so."] }, { "field": [1001025, 1, 1, 25, "And God made the beast of the earth after his kind, and cattle after their kind, and every thing that creepeth upon the earth after his kind: and God saw that it was good."] }, { "field": [1001026, 1, 1, 26, "And God said, Let us make man in our image, after our likeness: and let them have dominion over the fish of the sea, and over the fowl of the air, and over the cattle, and over all the earth, and over every creeping thing that creepeth upon the earth."] }, { "field": [1001027, 1, 1, 27, "So God created man in his own image, in the image of God created he him; male and female created he them."] }, { "field": [1001028, 1, 1, 28, "And God blessed them, and God said unto them, Be fruitful, and multiply, and replenish the earth, and subdue it: and have dominion over the fish of the sea, and over the fowl of the air, and over every living thing that moveth upon the earth."] }, { "field": [1001029, 1, 1, 29, "And God said, Behold, I have given you every herb bearing seed, which is upon the face of all the earth, and every tree, in the which is the fruit of a tree yielding seed; to you it shall be for meat."] }, { "field": [1001030, 1, 1, 30, "And to every beast of the earth, and to every fowl of the air, and to every thing that creepeth upon the Line truncated