aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2023-08-19 21:24:14 +0000
committerrodri <rgl@antares-labs.eu>2023-08-19 21:24:14 +0000
commitfac705ddaf2ae19afd8d1791c0c94cfc696c03e2 (patch)
treec9ab1e66433e284c1a109e3d7efb20ed2a728f37
downloadwpq-front.tar.gz
wpq-front.tar.bz2
wpq-front.zip
now version controlled.HEADfront
-rw-r--r--mkfile18
-rw-r--r--readme.md45
-rwxr-xr-xwpq33
-rw-r--r--wpqprompt.c58
4 files changed, 154 insertions, 0 deletions
diff --git a/mkfile b/mkfile
new file mode 100644
index 0000000..f528551
--- /dev/null
+++ b/mkfile
@@ -0,0 +1,18 @@
+</$objtype/mkfile
+
+BIN=$home/bin/$objtype
+TARG=wpqprompt
+OFILES=\
+ wpqprompt.$O
+
+RCBIN=$home/bin/rc
+RCSCR=\
+ wpq
+
+</sys/src/cmd/mkone
+
+install:V: $BIN/$TARG
+ cp $RCSCR $RCBIN
+
+uninstall:V:
+ rm -f $BIN/$TARG $RCBIN/$RCSCR
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..3b0eb2f
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,45 @@
+# Wpq
+
+_Wpq_ is the WikiPedia Query tool. It allows you to look up a term
+and disambiguate if necessary, using Wikipedia's API to open pages
+directly in your `$browser` or show you the PDF file in **page(1)**.
+
+# Usage
+
+Let's say we've been reading PC stuff and just learned about the A20
+line. We go ahead and ask wpq for more info about it:
+
+ % wpq a20
+
+However, _a20_ by itself is too ambiguous, so we'll get a list of
+possible subjects for us to choose manually:
+
+```
+% wpq a20
+1) A20
+2) A20 line
+3) A20 road (England)
+4) A202 road
+5) A20 autoroute
+6) A201 road
+7) A206 road
+8) A20 heavy tank
+9) A200 road
+10) A20 cells
+[1-10]:
+```
+
+We are only interested in the A20 line, so we go ahead and type the
+index number corresponding to the entry:
+
+```
+% wpq a20
+1) A20
+2) A20 line
+…
+[1-10]: 2
+```
+
+And by default this will open up the article in **page(1)** after
+fetching it with **hget(1)**. If instead you want to see it in your
+`$browser`, pass the `-h` flag to _wpq_, and it will plumb the URL.
diff --git a/wpq b/wpq
new file mode 100755
index 0000000..370a62e
--- /dev/null
+++ b/wpq
@@ -0,0 +1,33 @@
+#!/bin/rc
+rfork e
+ts=`{date -n}
+tmpfile=/tmp/wpsearch.$ts
+wanthtml=n
+
+fn sigint {
+ rm -f $tmpfile
+}
+
+fn usage {
+ echo usage: $0 [-h] query >[1=2]
+ exit usage
+}
+
+if(~ $#* 0)
+ usage
+switch($1){
+case -*h*
+ wanthtml=y
+ shift
+}
+{
+ echo -n '{"result":'
+ hget 'https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search='^$"*
+ echo -n '}'
+} >$tmpfile
+targ=`{wpqprompt $tmpfile | tr ' ' '_'}
+rm -f $tmpfile
+if(~ $wanthtml y)
+ plumb 'https://en.wikipedia.org/wiki/'^$targ
+if not
+ hget 'https://en.wikipedia.org/api/rest_v1/page/pdf/'^$targ | page -w -p145
diff --git a/wpqprompt.c b/wpqprompt.c
new file mode 100644
index 0000000..b904dc8
--- /dev/null
+++ b/wpqprompt.c
@@ -0,0 +1,58 @@
+#include <u.h>
+#include <libc.h>
+#include <json.h>
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s wpqsearchfile\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ JSON *obj, *res;
+ JSONEl *e;
+ char buf[8192], *targets[32];
+ int fd, n, i;
+
+ JSONfmtinstall();
+ ARGBEGIN{
+ default: usage();
+ }ARGEND;
+ if(argc != 1)
+ usage();
+ fd = open(argv[0], OREAD);
+ if((n = readn(fd, buf, sizeof(buf)-1)) <= 0)
+ sysfatal("read: %r");
+ buf[n] = 0;
+ close(fd);
+ obj = jsonparse(buf);
+ if(obj == nil || obj->t != JSONObject)
+ sysfatal("jsonparse: %r");
+ res = jsonbyname(obj, "result");
+ if(res == nil || res->t != JSONArray)
+ sysfatal("jsonbyname: %r");
+ if(res->first->next->val->first == nil)
+ sysfatal("no results");
+ for(e = res->first->next->val->first, i = 0; e != nil && i < nelem(targets); e = e->next, i++){
+ targets[i] = e->val->s;
+ fprint(2, "%d) %s\n", i+1, targets[i]);
+ }
+ if(i < 2)
+ n = i;
+ else{
+prompt:
+ fprint(2, "[1-%d]: ", i);
+ if((n = read(0, buf, sizeof(buf)-1)) <= 0)
+ sysfatal("read: %r");
+ buf[n] = 0;
+ n = strtol(buf, nil, 0);
+ if(n < 1 || n > i)
+ goto prompt;
+ }
+ print("%s\n", targets[n-1]);
+ jsonfree(obj);
+ exits(0);
+}