From 65529edfb4e23dd9e3e38f8124d413c1ee12b9d0 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 25 Jul 2021 21:03:10 +0900 Subject: [PATCH] weather-tool: Use lat/lon when querying by guessed location --- apps/weather-tool.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/weather-tool.c b/apps/weather-tool.c index e111e122..450d1cc5 100644 --- a/apps/weather-tool.c +++ b/apps/weather-tool.c @@ -32,7 +32,7 @@ int main(int argc, char * argv[]) { if (!strcmp(city, "guess")) { /* See if the location data already exists... */ if (access(LOCATION_DATA_PATH, R_OK)) { - sprintf(cmdline, "fetch -o \"" LOCATION_DATA_PATH "\" \"http://ip-api.com/json/?fields=countryCode,regionName,city\""); + sprintf(cmdline, "fetch -o \"" LOCATION_DATA_PATH "\" \"http://ip-api.com/json/?fields=lat,lon,city\""); system(cmdline); } Value * locationData = json_parse_file(LOCATION_DATA_PATH); @@ -41,17 +41,17 @@ int main(int argc, char * argv[]) { return 1; } - char * cityName = JSON_KEY(locationData, "city")->string; - char * regionName = JSON_KEY(locationData, "regionName")->string; - char * countryCode = JSON_KEY(locationData, "countryCode")->string; + city = JSON_KEY(locationData, "city")->string; + double lat = JSON_KEY(locationData, "lat")->number; + double lon = JSON_KEY(locationData, "lon")->number; - city = malloc(strlen(cityName) + strlen(regionName) + strlen(countryCode) + 10); - sprintf(city, "%s, %s, %s", cityName, regionName, countryCode); + sprintf(cmdline, "fetch -o \"" WEATHER_DATA_PATH "\" \"http://api.openweathermap.org/data/2.5/weather?lat=%.5lf&lon=%.5lf&appid=%s&units=%s\"", lat, lon, key, units); + system(cmdline); + } else { + sprintf(cmdline, "fetch -o \"" WEATHER_DATA_PATH "\" \"http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s\"", city, key, units); + system(cmdline); } - sprintf(cmdline, "fetch -o \"" WEATHER_DATA_PATH "\" \"http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=%s\"", city, key, units); - system(cmdline); - Value * result = json_parse_file(WEATHER_DATA_PATH); assert(result && result->type == JSON_TYPE_OBJECT);