esvapi: fetch all pages of search results
Fixes: https://todo.sr.ht/~jeremy/esv/1
This commit is contained in:
parent
cdabb25f74
commit
77c2c4825a
2 changed files with 40 additions and 18 deletions
17
esv.1
17
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
|
||||
|
|
41
esvapi.d
41
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[]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue