first commit
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
# app.py
|
||||
from flask import Flask, request, jsonify, send_file
|
||||
import random, json
|
||||
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
|
||||
|
||||
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_book = 1
|
||||
idx_chapter = 2
|
||||
idx_verse = 3
|
||||
idx_text = 4
|
||||
|
||||
def respond_json(scripture):
|
||||
kjv = {
|
||||
"bookname": key[scripture[idx_book]]['n'],
|
||||
"chapter": scripture[idx_chapter],
|
||||
"verse": scripture[idx_verse],
|
||||
"text": scripture[idx_text],
|
||||
}
|
||||
return jsonify(kjv)
|
||||
|
||||
def respond_text(scripture):
|
||||
book = str(key[scripture[idx_book]]['n'])
|
||||
chapter = str(scripture[idx_chapter])
|
||||
verse = str(scripture[idx_verse])
|
||||
text = str(scripture[idx_text])
|
||||
return (book + " " + 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."}
|
||||
return jsonify(message)
|
||||
|
||||
@app.get("/")
|
||||
def idx():
|
||||
return send_file("index.html")
|
||||
|
||||
@app.get("/test")
|
||||
def test():
|
||||
pass
|
||||
|
||||
# json
|
||||
@app.get("/random")
|
||||
def random_verse_json():
|
||||
scripture = random.choice(kjv)['field']
|
||||
|
||||
return(respond_json(scripture))
|
||||
|
||||
# plain
|
||||
@app.get("/t/random")
|
||||
def random_verse_plain():
|
||||
scripture = random.choice(kjv)['field']
|
||||
|
||||
return(respond_text(scripture))
|
||||
|
||||
@app.get("/dbg/<ref>")
|
||||
def dbg(ref):
|
||||
return (ref)
|
||||
|
||||
# json
|
||||
@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
|
||||
todays_verse_idx = random.seed(today)
|
||||
scripture = random.choice(kjv)['field']
|
||||
|
||||
return(respond_json(scripture))
|
||||
|
||||
# plaintext
|
||||
@app.get("/t/votd")
|
||||
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']
|
||||
|
||||
return(respond_text(scripture))
|
||||
|
||||
|
||||
# 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.")
|
||||
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.")
|
||||
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.")
|
||||
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.")
|
||||
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.")
|
||||
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)
|
||||
Reference in New Issue
Block a user