summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2020-02-22 22:59:40 +0000
committerrodri <rgl@antares-labs.eu>2020-02-22 22:59:40 +0000
commit1e256e1f0adf0d230597138a3f02a92291924c5f (patch)
tree56d9fb67cd73a53844bb6dfe0ee19b9dca4b7b24
downloadstalker-1e256e1f0adf0d230597138a3f02a92291924c5f.tar.gz
stalker-1e256e1f0adf0d230597138a3f02a92291924c5f.tar.bz2
stalker-1e256e1f0adf0d230597138a3f02a92291924c5f.zip
now version controlled.
-rwxr-xr-xgeolocate2
-rw-r--r--geoparse.c85
-rwxr-xr-xinstall11
-rw-r--r--mkfile9
4 files changed, 107 insertions, 0 deletions
diff --git a/geolocate b/geolocate
new file mode 100755
index 0000000..769aa99
--- /dev/null
+++ b/geolocate
@@ -0,0 +1,2 @@
+#!/bin/rc
+hget http://ip-api.com/json/$1 | stalker/geoparse
diff --git a/geoparse.c b/geoparse.c
new file mode 100644
index 0000000..b58a0f2
--- /dev/null
+++ b/geoparse.c
@@ -0,0 +1,85 @@
+#include <u.h>
+#include <libc.h>
+#include <json.h>
+
+typedef struct TargetInfo TargetInfo;
+
+struct TargetInfo {
+ char *name;
+ int type;
+};
+
+void
+usage(void)
+{
+ fprint(2, "usage: geoparse [jsonfile]\n");
+ exits("usage");
+}
+
+char *
+jsontypestr(int type)
+{
+ char *tab[] = {
+ [JSONNull] "null",
+ [JSONBool] "bool",
+ [JSONNumber] "number",
+ [JSONString] "string",
+ [JSONArray] "array",
+ [JSONObject] "object"
+ };
+ return tab[type];
+}
+
+TargetInfo ti[] = {
+ { "as", JSONString },
+ { "city", JSONString },
+ { "country", JSONString },
+ { "countryCode", JSONString },
+ { "isp", JSONString },
+ { "lat", JSONNumber },
+ { "lon", JSONNumber },
+ { "org", JSONString },
+ { "query", JSONString },
+ { "region", JSONString },
+ { "regionName", JSONString },
+ { "status", JSONString },
+ { "timezone", JSONString },
+ { "zip", JSONString }
+};
+
+void
+main(int argc, char *argv[])
+{
+ char buf[1024];
+ JSON *obj, *record;
+ int fd, n;
+ int i;
+
+ if(argc < 2)
+ fd = 0;
+ else{
+ fd = open(argv[1], OREAD);
+ if(fd < 0)
+ sysfatal("open: %r");
+ }
+ if((n = read(fd, buf, sizeof(buf)-1)) <= 0)
+ sysfatal("read: %r");
+ buf[n] = 0;
+ obj = jsonparse(buf);
+ if(obj == nil || obj->t != JSONObject)
+ sysfatal("wrong input");
+ for(i = 0; i < nelem(ti); i++){
+ record = jsonbyname(obj, ti[i].name);
+ if(record == nil)
+ continue;
+ if(record->t != ti[i].type)
+ sysfatal("%s field not a %s",
+ ti[i].name, jsontypestr(ti[i].type));
+ if(ti[i].type == JSONString)
+ print("%s=%s\n", ti[i].name, record->s);
+ else
+ print("%s=%g\n", ti[i].name, record->n);
+ }
+ jsonfree(obj);
+ exits(0);
+}
diff --git a/install b/install
new file mode 100755
index 0000000..06a5f25
--- /dev/null
+++ b/install
@@ -0,0 +1,11 @@
+#!/bin/rc
+. <{cat /$objtype/mkfile | grep -v '^<'}
+
+RCBIN=$home/bin/rc/stalker
+
+if(! test -d $RCBIN)
+ mkdir $RCBIN
+mk
+cp $O.out $RCBIN/geoparse
+cp geolocate $RCBIN
+mk clean
diff --git a/mkfile b/mkfile
new file mode 100644
index 0000000..a7f1ef8
--- /dev/null
+++ b/mkfile
@@ -0,0 +1,9 @@
+</$objtype/mkfile
+BIN=/$objtype/bin
+
+OFILES=\
+ geoparse.$O
+
+TARG=geoparse
+
+</sys/src/cmd/mkone