JSON flat file to sqlite3 data source querying
This commit is contained in:
@@ -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 = []
|
||||
|
||||
Reference in New Issue
Block a user