From f9238ab9ad28351e33940d6494b8c251bada9ee4 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Wed, 3 May 2023 11:33:20 -0500 Subject: [PATCH] Initial simple implementation. --- .gitignore | 2 ++ .nim_esv_api.nimble.swp | Bin 12288 -> 0 bytes nim_esv_api.nimble => esv_api.nimble | 7 +++++-- src/.nim_esv_api.nim.swp | Bin 12288 -> 0 bytes src/config.nims | 1 + src/{nim_esv_api.nim => esv_api.nim} | 30 ++++++++++++++++++++------- 6 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 .gitignore delete mode 100644 .nim_esv_api.nimble.swp rename nim_esv_api.nimble => esv_api.nimble (59%) delete mode 100644 src/.nim_esv_api.nim.swp create mode 100644 src/config.nims rename src/{nim_esv_api.nim => esv_api.nim} (56%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2dea328 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +esv_api +*.sw? diff --git a/.nim_esv_api.nimble.swp b/.nim_esv_api.nimble.swp deleted file mode 100644 index 6c279bf99cd70257ca4e7c7189c0485cd8313e2c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI&&rcLF6bJAJ2X6AqgmWnuJnXQGK#0kz;Br_ahKO>|7}A+}vW4AFZD;T%e*y`A zj{l1%{|1dm-(+`{aM*<7ns1Yrb~>Hc_H%2dY4fCesCJW0lCntjB)-XSR_5s07Ll)l zjf48dpBlRl=<#N&S%0;rSJlJiclStup%8!o1Rwwb2tWV=5P-n63zU9_u4EIlqm9gu z>X|$B=n)kJAOHafKmY;|fB*y_009U<00L7eU~-~sIrRUi5B~E1e|-M`wLtV!;=9B* z2`}+pq9d^;p(R!&mL--XevflZp#hc*0SG_<0uX=z1Rwwb2tWV=|Aj!{&y@`v)$*A?qzGq^G?}XG6}M-7SvXVnj8pp@oC&$6OAc{3QN0S8teLcu;U14n Jc45w#{r~{feun@6 diff --git a/nim_esv_api.nimble b/esv_api.nimble similarity index 59% rename from nim_esv_api.nimble rename to esv_api.nimble index ffbbf06..1c1792f 100644 --- a/nim_esv_api.nimble +++ b/esv_api.nimble @@ -5,10 +5,13 @@ author = "Jonathan Bernard" description = "Simple Nim CLI wrapper around the ESV API (api.esv.org)" license = "MIT" srcDir = "src" -bin = @["nim_esv_api"] +bin = @["esv_api"] # Dependencies requires "nim >= 1.6.10" -requires @["docopt"] +requires @["docopt", "zero_functional"] + +# dependencies from git.jdb-software.com/jdb/nim-packages.git +requires @["cliutils"] diff --git a/src/.nim_esv_api.nim.swp b/src/.nim_esv_api.nim.swp deleted file mode 100644 index 17dd6ebdffbd747ad36fbc8937a4f646e77476cc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&u<$=6vrnVkZ5UxK~LVj_GqG-H3wkO@)S!Tv| zbK%E707(1|9JqqS5u_qH7;0%U_u%~NFzH6A>}rv{eeS@ID}#+ft3wf6)5v8O z2{Vd#Asa@=u6=;^0orwZKG&Fk=m~fNFHT@+d(o{c?B?Q|3vSX}aGlk%%Pz?&9->xwBC3GK}g)T#vpg&$^>}Tj}=rib3=m?6T22_WxL4RFl z>`&-B=v!zEaVUglp{vlZ@c$R+d*~}DhRT>OU{1Wto`5Id33vjYfG6MyTo?j`>!YEx z;RpEhfXj5KMUA=MM0AIJ>I=I()LP`W^*9!Vl`0Qwb@Xmo9zWXFJQiU!zQ-+3&l=w` z{c?yX4bU>T@ql+z(b8J!CMy?9g{8RHrzKieSy$#FSyp*Z_QOQQs<2Jsx^L#`(5SrW z20oCfSV5y!O(znm5lza)cxAoo-c+49MI?LF!{F9qX{-q+Q#ZIB)S6UqZGa*bfWNya8Z z>-tzrD^?MCc*8w84lGu=QK?u}{l3ukRE$KrJWzqr=S{eI+`WK($0Ho9t)NAa58duh~%Ou zp27$j3xc2woX)vKK0<^p0vd5GUFFrEJLx}3EQ^SgCOZ(++S)z1yU{+_UVp!}_FP+u z=<#7{F;f~E%QU5~AT4-8Lc*G+o>rN&3rmrmC)7vvf^}roBazeX@^%LyZfG@lubxsAcNn zEGV5gE*7-7rLoWgyUZAoQ01ocDvGMJP(#%jG*6Qqu#eYr=V%Ky(&G%^#rj;(So5Mla=BJRWMxt| tgtQ@+IePL9EiNqHq!pFpAmKUP6FTQQ!J{>q(ei4WKAc!^nK?0?{R?1Y`BDG? diff --git a/src/config.nims b/src/config.nims new file mode 100644 index 0000000..de09037 --- /dev/null +++ b/src/config.nims @@ -0,0 +1 @@ +switch("define", "ssl") diff --git a/src/nim_esv_api.nim b/src/esv_api.nim similarity index 56% rename from src/nim_esv_api.nim rename to src/esv_api.nim index b90db2c..00f57e8 100644 --- a/src/nim_esv_api.nim +++ b/src/esv_api.nim @@ -3,8 +3,8 @@ ## Simple command-line wrapper around the ESV API. -import std/[httpclient, json, logging] -import cliutils, docopt +import std/[httpclient, json, logging, os, re, strutils, uri] +import cliutils, docopt, zero_functional when isMainModule: const USAGE = """Usage: @@ -25,12 +25,12 @@ Options: let consoleLogger = newConsoleLogger( levelThreshold=lvlInfo, - fmtStr="pit - $levelname: ") + fmtStr="esv_api - $levelname: ") logging.addHandler(consoleLogger) try: # Parse arguments - let args = docopt(USAGE, version = PIT_VERSION) + let args = docopt(USAGE, version = "0.1.0") if args["--debug"]: consoleLogger.levelThreshold = lvlDebug @@ -38,11 +38,27 @@ Options: if args["--echo-args"]: stderr.writeLine($args) let cfgFilePath = getEnv("HOME") / ".esv_api.cfg.json" - let cfgFileJson = - if fileExists(cfgFilePath): parseFile(cfgFilePath) - else: newJObject() + var cfgFileJson = newJObject() + if fileExists(cfgFilePath): + debug "Loading config from " & cfgFilePath + cfgFileJson = parseFile(cfgFilePath) let cfg = CombinedConfig(docopt: args, json: cfgFileJson) + let apiToken = cfg.getVal("esv-api-token") + let apiRoot = cfg.getVal("esv-api-root", "https://api.esv.org") + let reference = $args[""] + + let http = newHttpClient() + http.headers = newHttpHeaders({"Authorization": "Token " & apiToken}) + + let urlPath = apiRoot & "/v3/passage/text/?q=" & encodeUrl(reference) + debug "requesting " & urlPath + let respJson = parseJson(http.getContent(urlPath)) + + let formattedPassages = respJson["passages"].getElems --> + map(it.getStr.multiReplace([(re"\[(\d+)\]", "$1")])) + + echo formattedPassages.join("\p") except CatchableError: fatal getCurrentExceptionMsg()