From e2c61db8620701d111f09361bf59af5e14f2af56 Mon Sep 17 00:00:00 2001
From: Tyler
Date: Fri, 30 Dec 2022 23:36:43 -0800
Subject: [PATCH] Reorganized votd and random and integrated rich responses for
those
---
app.py | 114 +++++++++++++++++---------------------------------
index.html | 50 +++++++++++++++-------
response.html | 3 ++
3 files changed, 77 insertions(+), 90 deletions(-)
create mode 100644 response.html
diff --git a/app.py b/app.py
index 8779163..22ec1fe 100644
--- a/app.py
+++ b/app.py
@@ -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=
-- 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()[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/[")
-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("/")
def passage_json(passage):
diff --git a/index.html b/index.html
index ef31fa9..f7befe0 100644
--- a/index.html
+++ b/index.html
@@ -5,10 +5,7 @@
@@ -20,9 +17,7 @@ html{
]URL Root: https://api.1611.social/ (you are here)
- /random - a random JSON-encoded verse. Different every time.
- - /t/random - a random plaintext verse. Different every time.
- /votd - json-encoded verse changes from day-to-day.
- - /t/votd - plaintext verse changes from day-to-day.
- /p/someverse - will attempt to parse and return the Bible requested in plaintext (pretty, with escape characters)
- /someverse - will attempt to parse and return the Bible requested in plaintext, with no escape characters. Newlines are encoded as "/n" - for you to handle.
- /s/keyword - will return any verses containing keyword in a JSON list
@@ -48,18 +43,43 @@ example:
Integration
If you want to put the verse of the day into your website, you can use the following example php:
-echo curl_exec(curl_init('https://api.1611.social/t/votd'));
+echo curl_exec(curl_init('https://api.1611.social/votd?view=plain'));
-You can see this work here:
-
-
-
+
+
+FUTURE ENDPOINT SPECIFICATION
+======================
-Examples
-https://api.1611.social/p/John3:16
-https://api.1611.social/s/faith
-https://api.1611.social/sp/hell
-https://api.1611.social/spl/reprobate
+/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
+
+
Bugs
diff --git a/response.html b/response.html
new file mode 100644
index 0000000..67e640b
--- /dev/null
+++ b/response.html
@@ -0,0 +1,3 @@
+
+{passage}
+{text}