From 949cc0f279eaad427c7732a2264308ec0d25d3e7 Mon Sep 17 00:00:00 2001 From: Mariusz Bialonczyk Date: Wed, 6 Jul 2016 14:05:09 -0700 Subject: [PATCH] forecast: adapt to new YQL Weather API Reviewers: pawatzaza Reviewed By: pawatzaza Subscribers: pawatzaza Differential Revision: https://phab.enlightenment.org/D4132 Signed-off-by: Cedric BAIL --- src/e_mod_main.c | 123 +++++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/src/e_mod_main.c b/src/e_mod_main.c index f2ddadb..4f4522d 100644 --- a/src/e_mod_main.c +++ b/src/e_mod_main.c @@ -56,7 +56,7 @@ struct _Instance struct { - char temp, distance[3], pressure[3], speed[4]; + char temp, distance[3], pressure[3], speed[5]; } units; struct @@ -370,8 +370,8 @@ _forecasts_config_item_get(const char *id) ci->id = eina_stringshare_add(id); ci->poll_time = 60.0; ci->degrees = DEGREES_C; - ci->host = eina_stringshare_add("xml.weather.yahoo.com"); - ci->code = eina_stringshare_add("BUXX0005"); + ci->host = eina_stringshare_add("query.yahooapis.com"); + ci->code = eina_stringshare_add("839722"); ci->show_text = 1; ci->popup_on_hover = 1; @@ -424,8 +424,8 @@ e_modapi_init(E_Module *m) ci = E_NEW(Config_Item, 1); ci->poll_time = 60.0; ci->degrees = DEGREES_C; - ci->host = eina_stringshare_add("xml.weather.yahoo.com"); - ci->code = eina_stringshare_add("BUXX0005"); + ci->host = eina_stringshare_add("query.yahooapis.com"); + ci->code = eina_stringshare_add("839722"); ci->id = eina_stringshare_add("0"); ci->show_text = 1; ci->popup_on_hover = 1; @@ -608,7 +608,7 @@ _forecasts_server_add(void *data, int type, void *event) else degrees = 'c'; - snprintf(forecast, sizeof(forecast), "/forecastrss?p=%s&u=%c", inst->ci->code, degrees); + snprintf(forecast, sizeof(forecast), "/v1/public/yql?q=select+%%2A+from+weather.forecast+where+woeid%%3D%s+and+u%%3D%%27%c%%27", inst->ci->code, degrees); snprintf(buf, sizeof(buf), "GET http://%s%s HTTP/1.1\r\n" "Host: %s\r\n" "Connection: close\r\n\r\n", @@ -665,6 +665,7 @@ _forecasts_parse(void *data) char *needle; char city[256]; char region[256]; + char *region_ptr; char location[512]; float visibility; int i; @@ -676,29 +677,33 @@ _forecasts_parse(void *data) return 0; /* Location */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "location, location); /* Units */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "units.temp); + needle = strstr(eina_strbuf_string_get(inst->buffer), "units.speed); - - /* Current conditions */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "units.speed); + needle = strstr(needle, "temperature=\""); if (!needle) goto error; needle = strstr(needle, "\""); - sscanf(needle, "\"%255[^\"]\"", inst->condition.desc); + sscanf(needle, "\"%c\"", &inst->units.temp); + + /* Current conditions */ + needle = strstr(eina_strbuf_string_get(inst->buffer), "condition.code); - needle = strstr(needle, "temp=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%d\"", &inst->condition.temp); needle = strstr(needle, "date=\""); if (!needle) goto error; needle = strstr(needle, "\""); sscanf(needle, "\"%51[^\"]\"", inst->condition.update); + needle = strstr(needle, "temp=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%d\"", &inst->condition.temp); + needle = strstr(needle, "text=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%255[^\"]\"", inst->condition.desc); /* Details */ /* Wind */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "details.wind.chill); @@ -746,15 +759,12 @@ _forecasts_parse(void *data) sscanf(needle, "\"%d\"", &inst->details.wind.speed); /* Atmosphere */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "details.atmosphere.humidity); - needle = strstr(needle, "visibility=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%f\"", &visibility); - inst->details.atmosphere.visibility = visibility; needle = strstr(needle, "pressure=\""); if (!needle) goto error; needle = strstr(needle, "\""); @@ -763,9 +773,16 @@ _forecasts_parse(void *data) if (!needle) goto error; needle = strstr(needle, "\""); sscanf(needle, "\"%d\"", &inst->details.atmosphere.rising); + needle = strstr(needle, "visibility=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%f\"", &visibility); + inst->details.atmosphere.visibility = visibility; /* Astronomy */ - needle = strstr(eina_strbuf_string_get(inst->buffer), "details.astronomy.sunrise); @@ -777,35 +794,37 @@ _forecasts_parse(void *data) /* Forecasts */ for (i = 0; i < FORECASTS; i++) { - needle = strstr(needle, "forecast[i].day); - needle = strstr(needle, "date=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%12[^\"]\"", inst->forecast[i].date); - needle = strstr(needle, "low=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%d\"", &inst->forecast[i].low); - needle = strstr(needle, "high=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%d\"", &inst->forecast[i].high); - needle = strstr(needle, "text=\""); - if (!needle) goto error; - needle = strstr(needle, "\""); - sscanf(needle, "\"%255[^\"]\"", inst->forecast[i].desc); needle = strstr(needle, "code=\""); if (!needle) goto error; needle = strstr(needle, "\""); sscanf(needle, "\"%d\"", &inst->forecast[i].code); + needle = strstr(needle, "date=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%12[^\"]\"", inst->forecast[i].date); + needle = strstr(needle, "day=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%4[^\"]\"", inst->forecast[i].day); + needle = strstr(needle, "high=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%d\"", &inst->forecast[i].high); + needle = strstr(needle, "low=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%d\"", &inst->forecast[i].low); + needle = strstr(needle, "text=\""); + if (!needle) goto error; + needle = strstr(needle, "\""); + sscanf(needle, "\"%255[^\"]\"", inst->forecast[i].desc); } return 1; error: - fprintf(stderr, "ERROR: Couldn't parse info from xml.weather.yahoo.com\n"); + fprintf(stderr, "ERROR: Couldn't parse info from %s\n", inst->ci->host); return 0; } @@ -820,7 +839,7 @@ _forecasts_converter(Instance *inst) inst->units.temp = 'C'; snprintf(inst->units.distance, 3, "km"); snprintf(inst->units.pressure, 3, "mb"); - snprintf(inst->units.speed, 4, "kph"); + snprintf(inst->units.speed, 5, "km/h"); } else if ((inst->units.temp == 'C') && (inst->ci->degrees == DEGREES_F)) {