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 <cedric@osg.samsung.com>
This commit is contained in:
Mariusz Bialonczyk 2016-07-06 14:05:09 -07:00 committed by Cedric BAIL
parent 68902013a4
commit 949cc0f279
1 changed files with 71 additions and 52 deletions

View File

@ -56,7 +56,7 @@ struct _Instance
struct struct
{ {
char temp, distance[3], pressure[3], speed[4]; char temp, distance[3], pressure[3], speed[5];
} units; } units;
struct struct
@ -370,8 +370,8 @@ _forecasts_config_item_get(const char *id)
ci->id = eina_stringshare_add(id); ci->id = eina_stringshare_add(id);
ci->poll_time = 60.0; ci->poll_time = 60.0;
ci->degrees = DEGREES_C; ci->degrees = DEGREES_C;
ci->host = eina_stringshare_add("xml.weather.yahoo.com"); ci->host = eina_stringshare_add("query.yahooapis.com");
ci->code = eina_stringshare_add("BUXX0005"); ci->code = eina_stringshare_add("839722");
ci->show_text = 1; ci->show_text = 1;
ci->popup_on_hover = 1; ci->popup_on_hover = 1;
@ -424,8 +424,8 @@ e_modapi_init(E_Module *m)
ci = E_NEW(Config_Item, 1); ci = E_NEW(Config_Item, 1);
ci->poll_time = 60.0; ci->poll_time = 60.0;
ci->degrees = DEGREES_C; ci->degrees = DEGREES_C;
ci->host = eina_stringshare_add("xml.weather.yahoo.com"); ci->host = eina_stringshare_add("query.yahooapis.com");
ci->code = eina_stringshare_add("BUXX0005"); ci->code = eina_stringshare_add("839722");
ci->id = eina_stringshare_add("0"); ci->id = eina_stringshare_add("0");
ci->show_text = 1; ci->show_text = 1;
ci->popup_on_hover = 1; ci->popup_on_hover = 1;
@ -608,7 +608,7 @@ _forecasts_server_add(void *data, int type, void *event)
else else
degrees = 'c'; 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" snprintf(buf, sizeof(buf), "GET http://%s%s HTTP/1.1\r\n"
"Host: %s\r\n" "Host: %s\r\n"
"Connection: close\r\n\r\n", "Connection: close\r\n\r\n",
@ -665,6 +665,7 @@ _forecasts_parse(void *data)
char *needle; char *needle;
char city[256]; char city[256];
char region[256]; char region[256];
char *region_ptr;
char location[512]; char location[512];
float visibility; float visibility;
int i; int i;
@ -676,29 +677,33 @@ _forecasts_parse(void *data)
return 0; return 0;
/* Location */ /* Location */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:location city="); needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:location ");
if (!needle) goto error;
needle = strstr(needle, "city=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%255[^\"]\"", city); sscanf(needle, "\"%255[^\"]\"", city);
region[0] = '\0'; region[0] = '\0';
needle = strstr(needle, "region=\""); needle = strstr(needle, "region=\"");
if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%255[^\"]\"", region); sscanf(needle, "\"%255[^\"]\"", region);
region_ptr = region;
//get rid of leading white space
if (region[0] = ' ')
region_ptr++;
if (strlen(region)) if (strlen(region_ptr))
snprintf(location, 512, "%s, %s", city, region); snprintf(location, 512, "%s, %s", city, region_ptr);
else else
snprintf(location, 512, "%s", city); snprintf(location, 512, "%s", city);
eina_stringshare_replace(&inst->location, location); eina_stringshare_replace(&inst->location, location);
/* Units */ /* Units */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:units temperature="); needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:units ");
if (!needle) if (!needle) goto error;
goto error;
needle = strstr(needle, "\"");
sscanf(needle, "\"%c\"", &inst->units.temp);
needle = strstr(needle, "distance=\""); needle = strstr(needle, "distance=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
@ -710,29 +715,37 @@ _forecasts_parse(void *data)
needle = strstr(needle, "speed=\""); needle = strstr(needle, "speed=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%3[^\"]\"", inst->units.speed); sscanf(needle, "\"%4[^\"]\"", inst->units.speed);
needle = strstr(needle, "temperature=\"");
/* Current conditions */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:condition text=");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); 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), "<yweather:condition ");
if (!needle) goto error;
needle = strstr(needle, "code=\""); needle = strstr(needle, "code=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->condition.code); sscanf(needle, "\"%d\"", &inst->condition.code);
needle = strstr(needle, "temp=\"");
if (!needle) goto error;
needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->condition.temp);
needle = strstr(needle, "date=\""); needle = strstr(needle, "date=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%51[^\"]\"", inst->condition.update); 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 */ /* Details */
/* Wind */ /* Wind */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:wind chill="); needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:wind ");
if (!needle) goto error;
needle = strstr(needle, "chill=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->details.wind.chill); sscanf(needle, "\"%d\"", &inst->details.wind.chill);
@ -746,15 +759,12 @@ _forecasts_parse(void *data)
sscanf(needle, "\"%d\"", &inst->details.wind.speed); sscanf(needle, "\"%d\"", &inst->details.wind.speed);
/* Atmosphere */ /* Atmosphere */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:atmosphere humidity="); needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:atmosphere ");
if (!needle) goto error;
needle = strstr(needle, "humidity=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->details.atmosphere.humidity); sscanf(needle, "\"%d\"", &inst->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=\""); needle = strstr(needle, "pressure=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
@ -763,9 +773,16 @@ _forecasts_parse(void *data)
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->details.atmosphere.rising); 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 */ /* Astronomy */
needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:astronomy sunrise="); needle = strstr(eina_strbuf_string_get(inst->buffer), "<yweather:astronomy ");
if (!needle) goto error;
needle = strstr(needle, "sunrise=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%8[^\"]\"", inst->details.astronomy.sunrise); sscanf(needle, "\"%8[^\"]\"", inst->details.astronomy.sunrise);
@ -777,35 +794,37 @@ _forecasts_parse(void *data)
/* Forecasts */ /* Forecasts */
for (i = 0; i < FORECASTS; i++) for (i = 0; i < FORECASTS; i++)
{ {
needle = strstr(needle, "<yweather:forecast day="); needle = strstr(needle, "<yweather:forecast ");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\"");
sscanf(needle, "\"%4[^\"]\"", inst->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=\""); needle = strstr(needle, "code=\"");
if (!needle) goto error; if (!needle) goto error;
needle = strstr(needle, "\""); needle = strstr(needle, "\"");
sscanf(needle, "\"%d\"", &inst->forecast[i].code); 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; return 1;
error: 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; return 0;
} }
@ -820,7 +839,7 @@ _forecasts_converter(Instance *inst)
inst->units.temp = 'C'; inst->units.temp = 'C';
snprintf(inst->units.distance, 3, "km"); snprintf(inst->units.distance, 3, "km");
snprintf(inst->units.pressure, 3, "mb"); 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)) else if ((inst->units.temp == 'C') && (inst->ci->degrees == DEGREES_F))
{ {