weather: Add weather-tool and weather-configurator
This commit is contained in:
parent
bd92f00a40
commit
fbf7bff3dd
|
@ -0,0 +1,57 @@
|
|||
#include <stdio.h>
|
||||
#include <toaru/json.h>
|
||||
#include <toaru/hashmap.h>
|
||||
#include <toaru/list.h>
|
||||
|
||||
typedef struct JSON_Value Value;
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
|
||||
Value * config = json_parse_file("/etc/weather.json");
|
||||
if (config) {
|
||||
char * city = JSON_KEY(config, "city")->string;
|
||||
char * key = JSON_KEY(config, "key")->string;
|
||||
char * __comment = JSON_KEY(config, "--comment")->string;
|
||||
char * units = JSON_KEY(config, "units")->string;
|
||||
|
||||
fprintf(stdout, "City? [%s] ", city);
|
||||
fflush(stdout);
|
||||
|
||||
char ncity[100];
|
||||
fgets(ncity, 100, stdin);
|
||||
|
||||
if (ncity[0] != '\n') {
|
||||
char * n = strstr(ncity, "\n");
|
||||
if (n) *n = '\0';
|
||||
city = ncity;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Units? [%s] ", units);
|
||||
fflush(stdout);
|
||||
|
||||
char nunits[100];
|
||||
fgets(nunits, 100, stdin);
|
||||
|
||||
if (nunits[0] != '\n') {
|
||||
char * n = strstr(nunits, "\n");
|
||||
if (n) *n = '\0';
|
||||
units = nunits;
|
||||
}
|
||||
|
||||
FILE * f = fopen("/etc/weather.json", "w");
|
||||
fprintf(f, "{\n");
|
||||
fprintf(f, " \"city\": \"%s\",\n", city);
|
||||
fprintf(f, " \"units\": \"%s\",\n", units);
|
||||
fprintf(f, "\n");
|
||||
fprintf(f, " \"--comment\": \"%s\",\n", __comment);
|
||||
fprintf(f, " \"key\": \"%s\"\n", key);
|
||||
fprintf(f, "}\n");
|
||||
fclose(f);
|
||||
|
||||
} else {
|
||||
fprintf(stderr, "Configuration is not set. A key is required. Please create the file manually.\n");
|
||||
fprintf(stderr, "(Press ENTER to exit.)\n");
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <toaru/json.h>
|
||||
#include <toaru/hashmap.h>
|
||||
#include <toaru/list.h>
|
||||
|
||||
typedef struct JSON_Value Value;
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
|
||||
Value * config = json_parse_file("/etc/weather.json");
|
||||
if (!config) {
|
||||
fprintf(stderr, "No weather config data\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char * city = JSON_KEY(config, "city")->string;
|
||||
char * key = JSON_KEY(config, "key")->string;
|
||||
char * units = JSON_KEY(config, "units")->string;
|
||||
|
||||
char cmdline[1024];
|
||||
sprintf(cmdline, "fetch -o /tmp/weather-data.json http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s", city, key, units);
|
||||
system(cmdline);
|
||||
|
||||
Value * result = json_parse_file("/tmp/weather-data.json");
|
||||
assert(result && result->type == JSON_TYPE_OBJECT);
|
||||
|
||||
Value * _main = JSON_KEY(result,"main");
|
||||
Value * conditions = (JSON_KEY(result,"weather") && JSON_KEY(result,"weather")->array->length > 0) ?
|
||||
JSON_IND(JSON_KEY(result,"weather"),0) : NULL;
|
||||
|
||||
FILE * out = fopen("/tmp/weather-parsed.conf", "w");
|
||||
fprintf(out, "%.2lf\n", JSON_KEY(_main,"temp")->number);
|
||||
fprintf(out, "%d\n", (int)JSON_KEY(_main,"temp")->number);
|
||||
fprintf(out, "%s\n", conditions ? JSON_KEY(conditions,"main")->string : "");
|
||||
fprintf(out, "%s\n", conditions ? JSON_KEY(conditions,"icon")->string : "");
|
||||
fprintf(out, "%d\n", (int)JSON_KEY(_main,"humidity")->number);
|
||||
fprintf(out, "%d\n", JSON_KEY(JSON_KEY(result,"clouds"),"all") ? (int)JSON_KEY(JSON_KEY(result,"clouds"),"all")->number : 0);
|
||||
fprintf(out, "%s\n", city);
|
||||
char * format = "%a, %d %b %Y %H:%M:%S";
|
||||
struct tm * timeinfo;
|
||||
struct timeval now;
|
||||
char buf[BUFSIZ] = {0};
|
||||
gettimeofday(&now, NULL);
|
||||
timeinfo = localtime((time_t *)&now.tv_sec);
|
||||
strftime(buf,BUFSIZ,format,timeinfo);
|
||||
fprintf(out, buf);
|
||||
fclose(out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue