Reorganized votd and random and integrated rich responses for those

This commit is contained in:
2022-12-30 23:36:43 -08:00
parent 92ff0a8257
commit e2c61db862
3 changed files with 77 additions and 90 deletions
+39 -75
View File
@@ -18,121 +18,85 @@ 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()[0]
def respond_json(scripture):
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)
return (jsonify(verse))
def respond_text(scripture):
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."
}
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("/qwerty")
def test():
style = css.read()
return (style + "Hello! Testing!")
# json
@app.get("/random")
def random_verse_json():
#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(respond_json(scripture))
return(response(scripture))
# plain
@app.get("/t/random")
def random_verse_plain():
rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id)
return(respond_text(scripture))
@app.get("/dbg/<ref>")
def dbg(ref):
return (ref)
# json
@app.get("/votd")
#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(respond_json(scripture))
return(response(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
random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids))
return(respond_text(scripture))
@app.get("/qwerty")
def test():
style = css.read()
return (style + "Hello! Testing!")
# plain
@app.get("/<passage>")
def passage_json(passage):