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 6c279bf..0000000 Binary files a/.nim_esv_api.nimble.swp and /dev/null differ 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 17dd6eb..0000000 Binary files a/src/.nim_esv_api.nim.swp and /dev/null differ 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()