actually fixed the SQL and the testing script. Added future support for rich responses

This commit is contained in:
2022-12-30 22:40:58 -08:00
parent ede1c816f1
commit 92ff0a8257
3 changed files with 62 additions and 14 deletions
+50 -13
View File
@@ -7,8 +7,8 @@ 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()
kjv_cur = kjv_db.cursor()
verse_ids = [i[0] for i in kjv_cur.execute("SELECT id FROM t_kjv;").fetchall()]
app = Flask(__name__)
@@ -18,11 +18,49 @@ idx_chapter = 2
idx_verse = 3
idx_text = 4
"""
FUTURE
ENDPOINT SPECIFICATION
======================
/random - outputs a random verse
/votd - returns verse of the day
/search - returns a keyword search result set
/view - returns a verse or set of verses by name/location
Search/view with no kw argument returns John 3:16
random/votd with no view argument returns JSON
URL PARAMETERS
==============
?view=<string>
- json - json-encoded response
- plain - plaintext response
- human - pretty response with css
?kw=<string>
- comma-delimited keyword search
?fz=<bool>
- fuzzy search on/off (uses thesaurus for similar words when searching)
EXAMPLES
========
api.tld/random?view=json
api.tld/votd?
api.tld/
api.tld/
api.tld/
api.tld/
"""
css = open('human_readable.css', 'r')
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()
return kjv_cur.execute("SELECT n FROM key_english WHERE b = {}".format(id)).fetchone()[0]
def respond_json(scripture):
verse = {
@@ -40,6 +78,8 @@ def respond_text(scripture):
text = str(scripture[idx_text])
return (bookname + " " + chapter + ":" + verse + " " + text)
@app.errorhandler(404)
def page_not_found(e):
message = {
@@ -51,24 +91,23 @@ def page_not_found(e):
def idx():
return send_file("index.html")
@app.get("/test")
@app.get("/qwerty")
def test():
pass
style = css.read()
return (style + "Hello! Testing!")
# json
@app.get("/random")
def random_verse_json():
rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id))
scripture = lookup_one_verse(rand_id)
return(respond_json(scripture))
# plain
@app.get("/t/random")
def random_verse_plain():
rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id))
scripture = lookup_one_verse(rand_id)
return(respond_text(scripture))
@app.get("/dbg/<ref>")
@@ -81,8 +120,7 @@ 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)))
scripture = lookup_one_verse(random.choice(verse_ids))
return(respond_json(scripture))
# plaintext
@@ -91,8 +129,7 @@ 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
random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids)))
scripture = lookup_one_verse(random.choice(verse_ids))
return(respond_text(scripture))
+10
View File
@@ -0,0 +1,10 @@
<style>
html{
font-family: sans-serif;
font-size: 50pt;
height: 100%;
width: 768px;
overflow: hidden;
display: table;
}
</style>
+2 -1
View File
@@ -1,2 +1,3 @@
#!/bin/sh
uwsgi_python3 --http-socket 0.0.0.0:1612 --wsgi-file /opt/kjv-api/app.py --callable app
# run from current directory only
uwsgi_python3 --http-socket 0.0.0.0:1612 --wsgi-file app.py --callable app