218 lines
7.1 KiB
Python
218 lines
7.1 KiB
Python
# app.py
|
|
from flask import Flask, request, jsonify, send_file
|
|
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_db.cursor()
|
|
verse_ids = [i[0] for i in kjv_cur.execute("SELECT id FROM t_kjv;").fetchall()]
|
|
|
|
app = Flask(__name__)
|
|
|
|
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()[0]
|
|
|
|
def response_json(scripture):
|
|
verse = {
|
|
"bookname": lookup_bookname(scripture[idx_book]),
|
|
"chapter": scripture[idx_chapter],
|
|
"verse": scripture[idx_verse],
|
|
"text": scripture[idx_text],
|
|
}
|
|
return (jsonify(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)
|
|
|
|
def response_rich(scripture):
|
|
css = open('human_readable.css', 'r')
|
|
response_html = open('response.html', 'r')
|
|
# passage, text
|
|
|
|
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())
|
|
|
|
def response(scripture):
|
|
arg_view = request.args.get("view", None)
|
|
if arg_view == "plain":
|
|
return(response_text(scripture))
|
|
elif arg_view == "rich":
|
|
return(response_rich(scripture))
|
|
else:
|
|
return(response_json(scripture))
|
|
|
|
@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."}
|
|
return jsonify(message)
|
|
|
|
@app.get("/")
|
|
def idx():
|
|
return send_file("index.html")
|
|
|
|
#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(scripture))
|
|
|
|
#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
|
|
random.seed(today)
|
|
scripture = lookup_one_verse(random.choice(verse_ids))
|
|
return(response(scripture))
|
|
|
|
|
|
|
|
@app.get("/qwerty")
|
|
def test():
|
|
style = css.read()
|
|
return (style + "Hello! Testing!")
|
|
|
|
# plain
|
|
@app.get("/<passage>")
|
|
def passage_json(passage):
|
|
try:
|
|
command = "bible -f {}".format(shlex.quote(passage))
|
|
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.")
|
|
return jsonify(result)
|
|
except Exception as e:
|
|
return jsonify(str(e))
|
|
|
|
# pretty
|
|
@app.get("/p/<passage>")
|
|
def passage_text(passage):
|
|
try:
|
|
command = "bible {}".format(shlex.quote(passage))
|
|
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.")
|
|
return result
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
|
|
#search
|
|
@app.get("/s/<keyword>")
|
|
def search_json(keyword):
|
|
try:
|
|
command = "bible -f ??{}".format(shlex.quote(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)
|
|
except Exception as e:
|
|
return jsonify(str(e))
|
|
|
|
#search
|
|
@app.get("/sp/<keyword>")
|
|
def search_text(keyword):
|
|
try:
|
|
command = "bible -f ??{}".format(shlex.quote(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))
|
|
return result
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
#search text json
|
|
@app.get("/sl/<keyword>")
|
|
def search_json_return(keyword):
|
|
try:
|
|
command = "bible -f ??{}".format(shlex.quote(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 = []
|
|
for verse in result:
|
|
complete_result.append(passage_text(verse))
|
|
return jsonify(complete_result)
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
#search pretty
|
|
@app.get("/spl/<keyword>")
|
|
def search_text_return(keyword):
|
|
try:
|
|
command = "bible -f ??{}".format(shlex.quote(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 = []
|
|
for verse in result:
|
|
complete_result.append(passage_text(verse))
|
|
result = '<br>'.join(map(str, complete_result))
|
|
return result
|
|
except Exception as e:
|
|
return str(e)
|
|
|
|
#search pretty multiple
|
|
@app.get("/splm/<keywords>")
|
|
def search_text_return_multiple(keywords):
|
|
if len(keywords.split('&')) > 1:
|
|
pass
|
|
try:
|
|
command = "bible -f ??{}".format(shlex.quote(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 = []
|
|
for verse in result:
|
|
complete_result.append(passage_text(verse))
|
|
result = '<br>'.join(map(str, complete_result))
|
|
return result
|
|
except Exception as e:
|
|
return str(e)
|