Created
May 18, 2019 00:10
-
-
Save jmcblane/6c4635f60e4d849745a6a446cdf9b22d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import auth # Contains our client definition, with login to API | |
import httpClient, oauth2, json | |
import htmlparser, xmltree, strformat | |
import strutils, httpcore, strtabs | |
type | |
Index* = tuple[ | |
plain: string, | |
num: string ] | |
Ref* = tuple[ | |
title: string, | |
refcode: string, | |
book: int, | |
para: int, | |
text: string ] | |
Search* = enum | |
egw = "book", apl = "apl", ltms = "ltms" | |
per = "periodicals", reference = "reference" | |
# Maybe implement topical search? Commentary search | |
# is broken on the API side, but who cares? | |
# There is also a Bible search available... | |
proc get_json*(url: string): JsonNode = | |
let r = client.bearerRequest(api_url & url, accessToken) | |
return parseJson(r.body) | |
proc webster*(query: string): string = | |
let | |
query = query.split(" ").join("%20") | |
json = get_json(fmt"/search/advanced/dictionary/content/?chapter={query}") | |
var strings: seq[string] | |
for i in json: | |
case i["element_subtype"].str: | |
of "list": strings.add("\n" & i["content"].str.parseHtml.innerText) | |
of "indent2": strings.add(" " & i["content"].str.parseHtml.innerText) | |
else: strings.add(i["content"].str.parseHtml.innerText) | |
result = strings.join("\n") | |
# Might want to better sanitize the input. So far, just | |
# replaces spaces with %20 | |
proc lookup*(query: string): Ref = | |
let | |
query = query.split(" ").join("%20") | |
json = get_json(fmt"/search/suggestions/?query={query}&lang=en") | |
try: | |
let s = json[0]["para_id"].str.split(".") | |
result.book = s[0].parseInt | |
result.para = s[1].parseInt | |
except: | |
result.book = 0 | |
result.para = 0 | |
# book is currently unused. Perhaps implement that later | |
# for searching withing a certain book | |
proc search*(kind: Search, query: string, book: string = ""): seq[Ref] = | |
let query = query.split(" ").join("%20") | |
let json = get_json(fmt"/search/advanced/{kind}/?query={query}&lang=en&limit=100&snippet=full") | |
for i in json["results"]: | |
var reference: Ref | |
let s = i["para_id"].str.split(".") | |
reference.book = s[0].parseInt | |
reference.para = s[1].parseInt | |
reference.title = i["refcode_long"].str | |
reference.refcode = i["refcode_short"].str | |
reference.text = i["snippet"].str.parseHtml.innerText | |
result.add(reference) | |
proc script_index*(book: string, chapter: int, verse: int): seq[Index] = | |
let | |
entry = get_json( | |
fmt"/search/advanced/scripture/navigate/?book={book}&chapter={chapter}&verse={verse}") | |
index = get_json("/content/books/2778/content/" & $entry["para"]) | |
for i in index[0]["content"].str.parsehtml.findAll("span"): | |
result.add((i.innerText, i.attrs["data-link"])) | |
proc get_si_refs*(refs: seq[Index]): seq[Ref] = | |
for i in refs: | |
var reference: Ref | |
let | |
s = i.num.split(".") | |
json = get_json(fmt"/content/books/{s[0]}/content/{s[1]}/?limit=2") | |
reference.title = json[0]["refcode_long"].str | |
reference.refcode = json[0]["refcode_short"].str | |
reference.book = s[0].parseInt | |
reference.para = s[1].parseInt | |
reference.text = json[0]["content"].str.parseHtml.innerText & | |
"\n\n" & json[1]["content"].str.parseHtml.innerText | |
result.add(reference) | |
proc get_paragraph*(q: Ref): Ref = | |
result.book = q.book | |
result.para = q.para | |
if result.book == 0: return result | |
let json = get_json(fmt"/content/books/{q.book}/content/{q.para}") | |
result.title = json[0]["refcode_long"].str | |
result.refcode = json[0]["refcode_short"].str | |
result.text = json[0]["content"].str.parseHtml.innerText | |
proc get_chapter*(q: Ref): Ref = | |
result.book = q.book | |
result.para = q.para | |
if result.book == 0: return result | |
let json = get_json(fmt"/content/books/{q.book}/chapter/{q.para}") | |
var ch_txt: seq[string] | |
result.title = json[0]["refcode_long"].str | |
result.refcode = json[0]["refcode_short"].str | |
for par in json: | |
if par["element_type"].str == "p": | |
ch_txt.add(par["content"].str.parseHtml.innerText & " <" & par["refcode_short"].str & ">") | |
else: ch_txt.add(par["content"].str.parseHtml.innerText) | |
result.text = ch_txt.join("\n\n") | |
proc get_book*(q: Ref): seq[Ref] = | |
let json = get_json(fmt"/content/books/{q.book}/toc") | |
for i in json: | |
var reference: Ref | |
let s = i["para_id"].str.split(".") | |
reference.book = s[0].parseInt | |
reference.para = s[1].parseInt | |
reference.title = i["title"].str | |
reference.refcode = get_paragraph(reference).refcode | |
result.add(reference) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment