From 77c2c4825a29961b03655b2b86f1c133e39553df Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Tue, 25 Jun 2024 09:40:53 +1200 Subject: [PATCH] esvapi: fetch all pages of search results Fixes: https://todo.sr.ht/~jeremy/esv/1 --- esv.1 | 17 +++++------------ esvapi.d | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/esv.1 b/esv.1 index 1a53e62..226aefd 100644 --- a/esv.1 +++ b/esv.1 @@ -1,4 +1,4 @@ -.Dd $Mdocdate: June 18 2024 $ +.Dd $Mdocdate: June 25 2024 $ .Dt ESV 1 .Os .Sh NAME @@ -150,17 +150,10 @@ Search the Bible for the phrase .Sh AUTHORS .An Jeremy Baxter Aq Mt jeremy@baxters.nz .Sh BUGS -Currently searching the Bible using -.Fl s -only shows a portion of the results. -If you have many results for your query, -and there are not many in the New Testament compared to those -in the Old Testament, -your search may have been affected by this bug. -.Pp -If you have found a potential bug in -.Nm , +Currently there are no known bugs in +.Nm ; +but if you think you've found a potential bug, please report it to me using my email address above. .Pp -Existing bugs can be found on the bug tracker: +Existing bugs and planned features can be found on the bug tracker: .Lk https://todo.sr.ht/~jeremy/esv diff --git a/esvapi.d b/esvapi.d index f63a97e..0356120 100644 --- a/esvapi.d +++ b/esvapi.d @@ -326,16 +326,45 @@ class ESVApi + + Example: search("It is finished") +/ - char[] - search(in string query) + string + search(in string query) @trusted { - return makeRequest("search/?q=" ~ query.tr(" ", "+")); + JSONValue[] pages, results; + JSONValue result; + + JSONValue + makeQuery(long page) + => parseJSON(makeRequest("search/?page-size=100" + ~ "&page=" ~ page.to!string() + ~ "&q=" ~ query.tr(" ", "+"))); + + pages ~= makeQuery(1); + if (pages[0]["total_pages"].integer == 1) { + result = JSONValue([ + "results": pages[0]["results"], + "total": JSONValue(pages[0]["results"].array.length) + ]); + return result.toString(); + } + + foreach (long i; 2 .. pages[0]["total_pages"].integer + 1) { + pages ~= makeQuery(i); + } + foreach (JSONValue page; pages) { + results ~= page["results"].array; + } + + result = JSONValue([ + "results": JSONValue(results), + "total": JSONValue(results.length) + ]); + return result.toString(); } /++ + Calls search() and formats the results nicely as plain text. +/ - char[] + string searchFormat(alias fmt = "\033[1m%s\033[0m\n %s\n") (in string query, int lineLength = 0) /* 0 means default */ { @@ -345,7 +374,7 @@ class ESVApi resp = parseJSON(search(query)); layout = []; - enforce!ESVException(resp["total_results"].integer != 0, + enforce!ESVException(resp["total"].integer != 0, "No results for search"); lineLength = lineLength == 0 ? 80 : lineLength; @@ -360,7 +389,7 @@ class ESVApi } }(); - return layout; + return layout.idup(); } protected char[]