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):
+35 -15
View File
@@ -5,10 +5,7 @@
<style>
html{
font-family: sans-serif;
height: 100%;
width: 768px;
overflow: hidden;
display: table;
}
</style>
</head>
@@ -20,9 +17,7 @@ html{
<p>URL Root: <a href="https://api.1611.social/">https://api.1611.social/<a> (you are here)</p>
<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/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/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/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>
@@ -48,18 +43,43 @@ example:</p>
<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>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>
<b>
<?php echo curl_exec(curl_init('https://api.1611.social/t/votd')); ?>
</b>
<pre>
<code>
FUTURE ENDPOINT SPECIFICATION
======================
<h3>Examples</h3>
<p><a href="https://api.1611.social/p/John3:16">https://api.1611.social/p/John3:16</a></p>
<p><a href="https://api.1611.social/s/faith">https://api.1611.social/s/faith</a></p>
<p><a href="https://api.1611.social/sp/hell">https://api.1611.social/sp/hell</a></p>
<p><a href="https://api.1611.social/spl/reprobate">https://api.1611.social/spl/reprobate</a></p>
/random - outputs a random verse
/votd - returns verse of the day
/search - returns a keyword search result set
/show - returns a verse or set of verses by name/location
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>
+3
View File
@@ -0,0 +1,3 @@
<!DOCTYPE html>
<h1>{passage}</h1>
<p>{text}</p>