From 1daec5116d3f4bb6705b27ad6294318a1636bd5a Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Thu, 1 Feb 2024 18:00:26 +0100 Subject: [PATCH] add experimental utility with which to enhance ghash/ with tzdata --- contrib/addtzname/.gitignore | 3 ++ contrib/addtzname/Makefile | 8 ++++ contrib/addtzname/README.md | 20 ++++++++++ contrib/addtzname/addtzname.c | 72 +++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 contrib/addtzname/.gitignore create mode 100644 contrib/addtzname/Makefile create mode 100644 contrib/addtzname/README.md create mode 100644 contrib/addtzname/addtzname.c diff --git a/contrib/addtzname/.gitignore b/contrib/addtzname/.gitignore new file mode 100644 index 0000000..43aa6e2 --- /dev/null +++ b/contrib/addtzname/.gitignore @@ -0,0 +1,3 @@ +*.o +.DS_Store +addtzname diff --git a/contrib/addtzname/Makefile b/contrib/addtzname/Makefile new file mode 100644 index 0000000..3a04764 --- /dev/null +++ b/contrib/addtzname/Makefile @@ -0,0 +1,8 @@ + +CFLAGS=-I../.. \ + -DTZDATADB="\"../../sundry/zonedetect/ZoneDetect/out/timezone16.bin\"" + +OBJS=../../json.o ../../zonedetect.o ../../geohash.o + +addtzname: addtzname.c $(OBJS) + $(CC) $(CFLAGS) -o addtzname addtzname.c $(OBJS) diff --git a/contrib/addtzname/README.md b/contrib/addtzname/README.md new file mode 100644 index 0000000..a2489cf --- /dev/null +++ b/contrib/addtzname/README.md @@ -0,0 +1,20 @@ +# addtzname + +Use at your own risk. + +Basically, after taking a backup, this is: + +- `o` is the path to your existing STORAGEDIR +- `n` is a new directory + +```console +$ mkdir n +$ ot-recorder -S n --initialize + +$ ocat -S o --dump | ./addtzname | ocat -S n --load +$ ocat -S n --dump | head # verify JSON contains tzname + +# when you're ready: +$ cp n/ghash/* ....../o/ghash/ +``` + diff --git a/contrib/addtzname/addtzname.c b/contrib/addtzname/addtzname.c new file mode 100644 index 0000000..ebcf1f5 --- /dev/null +++ b/contrib/addtzname/addtzname.c @@ -0,0 +1,72 @@ +/* + * OwnTracks Recorder + * Copyright (C) 2024 Jan-Piet Mens + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include "json.h" +#include "geohash.h" +#include "zonedetect.h" + +int main(int argc, char **argv) +{ + char buf[BUFSIZ]; + char *s, *geohash, *js; + JsonNode *json, *j; + + static ZoneDetect *zdb = NULL; + + zdb = ZDOpenDatabase(TZDATADB); + if (zdb == NULL) { + perror("Can't open zdb"); + exit(2); + } + + while (fgets(s = buf, sizeof(buf), stdin) != NULL) { + // find first space + if ((geohash = strtok(buf, " ")) != NULL) { + js = buf + strlen(geohash) + 1; + // puts(js); + if ((json = json_decode(js)) == NULL) { + if (strncmp(buf, "friends", 7) && + strncmp(buf, "topic2tid", 9) && + strncmp(buf, "wp", 2)) { + fprintf(stderr, "Can't decode %s\n", buf); + } + continue; + } + if ((j = json_find_member(json, "tzname")) == NULL) { + char *tz; + + GeoCoord g = geohash_decode(geohash); + + if ((tz = ZDHelperSimpleLookupString(zdb, g.latitude, g.longitude)) != NULL) { + json_append_member(json, "tzname", json_mkstring(tz)); + ZDHelperSimpleLookupStringFree(tz); + } + } + js = json_stringify(json, NULL); + printf("%s %s\n", geohash, js); + free(js); + json_delete(json); + } + } + return 0; +}