diff --git a/app.py b/app.py index ce78687..8779163 100644 --- a/app.py +++ b/app.py @@ -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= +- json - json-encoded response +- plain - plaintext response +- human - pretty response with css + +?kw= +- comma-delimited keyword search + +?fz= +- 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/") @@ -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)) diff --git a/human_readable.css b/human_readable.css new file mode 100644 index 0000000..208cc7e --- /dev/null +++ b/human_readable.css @@ -0,0 +1,10 @@ + diff --git a/kjv-api-test.sh b/kjv-api-test.sh index 97e8c11..620a5eb 100755 --- a/kjv-api-test.sh +++ b/kjv-api-test.sh @@ -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