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_verse = 3
idx_text = 4 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): def lookup_one_verse(id):
return(kjv_cur.execute("SELECT * FROM t_kjv WHERE id = {};".format(id)).fetchone()) return(kjv_cur.execute("SELECT * FROM t_kjv WHERE id = {};".format(id)).fetchone())
def lookup_bookname(id): def lookup_bookname(id):
return kjv_cur.execute("SELECT n FROM key_english WHERE b = {}".format(id)).fetchone()[0] return kjv_cur.execute("SELECT n FROM key_english WHERE b = {}".format(id)).fetchone()[0]
def respond_json(scripture): def response_json(scripture):
verse = { verse = {
"bookname": lookup_bookname(scripture[idx_book]), "bookname": lookup_bookname(scripture[idx_book]),
"chapter": scripture[idx_chapter], "chapter": scripture[idx_chapter],
"verse": scripture[idx_verse], "verse": scripture[idx_verse],
"text": scripture[idx_text], "text": scripture[idx_text],
} }
return jsonify(verse) return (jsonify(verse))
def respond_text(scripture): def response_text(scripture):
bookname = lookup_bookname(scripture[idx_book]) bookname = lookup_bookname(scripture[idx_book])
chapter = str(scripture[idx_chapter]) chapter = str(scripture[idx_chapter])
verse = str(scripture[idx_verse]) verse = str(scripture[idx_verse])
text = str(scripture[idx_text]) text = str(scripture[idx_text])
return (bookname + " " + chapter + ":" + verse + " " + 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) @app.errorhandler(404)
def page_not_found(e): def page_not_found(e):
message = { 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."}
"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) return jsonify(message)
@app.get("/") @app.get("/")
def idx(): def idx():
return send_file("index.html") return send_file("index.html")
@app.get("/qwerty") #backwards-compatibility
def test(): @app.get('/t/random', defaults={'view':'plain'})
style = css.read() @app.get('/random')
return (style + "Hello! Testing!") def random_verse():
# json
@app.get("/random")
def random_verse_json():
rand_id = random.SystemRandom().choice(verse_ids) rand_id = random.SystemRandom().choice(verse_ids)
scripture = lookup_one_verse(rand_id) scripture = lookup_one_verse(rand_id)
return(respond_json(scripture)) return(response(scripture))
# plain #backwards-compatibility
@app.get("/t/random") @app.get('/t/votd', defaults={'view':'plain'})
def random_verse_plain(): @app.get('/votd')
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")
def verse_of_the_day(): def verse_of_the_day():
today = int(str(date.today()).replace('-','')) today = int(str(date.today()).replace('-',''))
#this is deterministic because of today's date being used as the seed #this is deterministic because of today's date being used as the seed
random.seed(today) random.seed(today)
scripture = lookup_one_verse(random.choice(verse_ids)) 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 # plain
@app.get("/<passage>") @app.get("/<passage>")
def passage_json(passage): def passage_json(passage):
+35 -15
View File
@@ -5,10 +5,7 @@
<style> <style>
html{ html{
font-family: sans-serif; font-family: sans-serif;
height: 100%;
width: 768px; width: 768px;
overflow: hidden;
display: table;
} }
</style> </style>
</head> </head>
@@ -20,9 +17,7 @@ html{
<p>URL Root: <a href="https://api.1611.social/">https://api.1611.social/<a> (you are here)</p> <p>URL Root: <a href="https://api.1611.social/">https://api.1611.social/<a> (you are here)</p>
<ul> <ul>
<li><a href="https://api.1611.social/random">/random</a> - a random JSON-encoded verse. Different every time.</li> <li><a href="https://api.1611.social/random">/random</a> - a random JSON-encoded verse. Different every time.</li>
<li><a href="https://api.1611.social/t/random">/t/random</a> - a random plaintext verse. Different every time.</li>
<li><a href="https://api.1611.social/votd">/votd</a> - json-encoded verse changes from day-to-day.</li> <li><a href="https://api.1611.social/votd">/votd</a> - json-encoded verse changes from day-to-day.</li>
<li><a href="https://api.1611.social/t/votd">/t/votd</a> - plaintext verse changes from day-to-day.</li>
<li><a href="https://api.1611.social/p/john3:16">/p/someverse</a> - will attempt to parse and return the Bible requested in plaintext (pretty, with escape characters)</li> <li><a href="https://api.1611.social/p/john3:16">/p/someverse</a> - will attempt to parse and return the Bible requested in plaintext (pretty, with escape characters)</li>
<li><a href="https://api.1611.social/John3:16">/someverse</a> - will attempt to parse and return the Bible requested in plaintext, with no escape characters. Newlines are encoded as "/n" - for you to handle.</li> <li><a href="https://api.1611.social/John3:16">/someverse</a> - will attempt to parse and return the Bible requested in plaintext, with no escape characters. Newlines are encoded as "/n" - for you to handle.</li>
<li><a href="https://api.1611.social/s/faith">/s/keyword</a> - will return any verses containing keyword in a JSON list</li> <li><a href="https://api.1611.social/s/faith">/s/keyword</a> - will return any verses containing keyword in a JSON list</li>
@@ -48,18 +43,43 @@ example:</p>
<h3>Integration</h3> <h3>Integration</h3>
<p>If you want to put the verse of the day into your website, you can use the following example php:</p> <p>If you want to put the verse of the day into your website, you can use the following example php:</p>
<p>echo curl_exec(curl_init('https://api.1611.social/t/votd'));</p> <p>echo curl_exec(curl_init('https://api.1611.social/votd?view=plain'));</p>
<p>You can see this work here:</p> <pre>
<b> <code>
<?php echo curl_exec(curl_init('https://api.1611.social/t/votd')); ?> FUTURE ENDPOINT SPECIFICATION
</b> ======================
<h3>Examples</h3> /random - outputs a random verse
<p><a href="https://api.1611.social/p/John3:16">https://api.1611.social/p/John3:16</a></p> /votd - returns verse of the day
<p><a href="https://api.1611.social/s/faith">https://api.1611.social/s/faith</a></p> /search - returns a keyword search result set
<p><a href="https://api.1611.social/sp/hell">https://api.1611.social/sp/hell</a></p> /show - returns a verse or set of verses by name/location
<p><a href="https://api.1611.social/spl/reprobate">https://api.1611.social/spl/reprobate</a></p>
URL PARAMETERS
==============
?view=
- json - json-encoded response
- plain - plaintext response
- rich - pretty response with css
default: json
?kw=
- comma-delimited keyword search for /search
search default: faith
show default: john3:16
?fz=
- on - fuzzy search on
default: off
EXAMPLES
========
api.tld/random?view=json
api.tld/votd?view=human
api.tld/show?kw=john3:16,1john5:7&view=plain
api.tld/search?kw=faith
</code>
</pre>
<h3>Bugs</h3> <h3>Bugs</h3>
+3
View File
@@ -0,0 +1,3 @@
<!DOCTYPE html>
<h1>{passage}</h1>
<p>{text}</p>