esvapi: fetch all pages of search results

Fixes: https://todo.sr.ht/~jeremy/esv/1
This commit is contained in:
Jeremy Baxter 2024-06-25 09:40:53 +12:00
parent cdabb25f74
commit 77c2c4825a
2 changed files with 40 additions and 18 deletions

17
esv.1
View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: June 18 2024 $ .Dd $Mdocdate: June 25 2024 $
.Dt ESV 1 .Dt ESV 1
.Os .Os
.Sh NAME .Sh NAME
@ -150,17 +150,10 @@ Search the Bible for the phrase
.Sh AUTHORS .Sh AUTHORS
.An Jeremy Baxter Aq Mt jeremy@baxters.nz .An Jeremy Baxter Aq Mt jeremy@baxters.nz
.Sh BUGS .Sh BUGS
Currently searching the Bible using Currently there are no known bugs in
.Fl s .Nm ;
only shows a portion of the results. but if you think you've found a potential bug,
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 ,
please report it to me using my email address above. please report it to me using my email address above.
.Pp .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 .Lk https://todo.sr.ht/~jeremy/esv

View file

@ -326,16 +326,45 @@ class ESVApi
+ +
+ Example: search("It is finished") + Example: search("It is finished")
+/ +/
char[] string
search(in string query) 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. + Calls search() and formats the results nicely as plain text.
+/ +/
char[] string
searchFormat(alias fmt = "\033[1m%s\033[0m\n %s\n") searchFormat(alias fmt = "\033[1m%s\033[0m\n %s\n")
(in string query, int lineLength = 0) /* 0 means default */ (in string query, int lineLength = 0) /* 0 means default */
{ {
@ -345,7 +374,7 @@ class ESVApi
resp = parseJSON(search(query)); resp = parseJSON(search(query));
layout = []; layout = [];
enforce!ESVException(resp["total_results"].integer != 0, enforce!ESVException(resp["total"].integer != 0,
"No results for search"); "No results for search");
lineLength = lineLength == 0 ? 80 : lineLength; lineLength = lineLength == 0 ? 80 : lineLength;
@ -360,7 +389,7 @@ class ESVApi
} }
}(); }();
return layout; return layout.idup();
} }
protected char[] protected char[]