Precised last two weather_forecast xpaths
[monky] / src / weather.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Please see COPYING for details
6  *
7  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
8  *      (see AUTHORS)
9  * All rights reserved.
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * vim: ts=4 sw=4 noet ai cindent syntax=c
24  *
25  */
26
27 #include "conky.h"
28 #include "logging.h"
29 #include "weather.h"
30 #include "temphelper.h"
31 #include "ccurl_thread.h"
32 #include <time.h>
33 #include <ctype.h>
34 #ifdef MATH
35 #include <math.h>
36 #endif /* MATH */
37 #ifdef XOAP
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40
41 /* Xpath expressions for XOAP xml parsing */
42 #define NUM_XPATH_EXPRESSIONS_CC 8
43 const char *xpath_expression_cc[NUM_XPATH_EXPRESSIONS_CC] = {
44         "/weather/cc/lsup", "/weather/cc/tmp", "/weather/cc/t",
45         "/weather/cc/bar/r", "/weather/cc/wind/s", "/weather/cc/wind/d",
46         "/weather/cc/hmid", "/weather/cc/icon"
47 };
48
49 #define NUM_XPATH_EXPRESSIONS_DF 10
50 const char *xpath_expression_df[NUM_XPATH_EXPRESSIONS_DF] = {
51         "/weather/dayf/day[*]/hi", "/weather/dayf/day[*]/low",
52         "/weather/dayf/day[*]/part[1]/icon", "/weather/dayf/day[*]/part[1]/t",
53         "/weather/dayf/day[*]/part[1]/wind/s","/weather/dayf/day[*]/part[1]/wind/d",
54         "/weather/dayf/day[*]/part[1]/ppcp", "/weather/dayf/day[*]/part[1]/hmid",
55         "/weather/dayf/day[*]/@t", "/weather/dayf/day[*]/@dt"
56 };
57 #endif /* XOAP */
58
59 /* Possible sky conditions */
60 #define NUM_CC_CODES 6
61 const char *CC_CODES[NUM_CC_CODES] = {
62         "SKC", "CLR", "FEW", "SCT", "BKN", "OVC"
63 };
64
65 /* Possible weather modifiers */
66 #define NUM_WM_CODES 9
67 const char *WM_CODES[NUM_WM_CODES] = {
68         "VC", "MI", "BC", "PR", "TS", "BL",
69         "SH", "DR", "FZ"
70 };
71
72 /* Possible weather conditions */
73 #define NUM_WC_CODES 17
74 const char *WC_CODES[NUM_WC_CODES] = {
75         "DZ", "RA", "GR", "GS", "SN", "SG",
76         "FG", "HZ", "FU", "BR", "DU", "SA",
77         "FC", "PO", "SQ", "SS", "DS"
78 };
79
80 static ccurl_location_t *locations_head_cc = 0;
81 #ifdef XOAP
82 static ccurl_location_t *locations_head_df = 0;
83 #endif
84
85 void weather_free_info(void)
86 {
87         ccurl_free_locations(&locations_head_cc);
88 #ifdef XOAP
89         ccurl_free_locations(&locations_head_df);
90 #endif
91 }
92
93 int rel_humidity(int dew_point, int air) {
94         const float a = 17.27f;
95         const float b = 237.7f;
96
97         float diff = a*(dew_point/(b+dew_point)-air/(b+air));
98 #ifdef MATH
99         return (int)(100.f*expf(diff));
100 #else
101         return (int)(16.666667163372f*(6.f+diff*(6.f+diff*(3.f+diff))));
102 #endif /* MATH */
103 }
104
105 #ifdef XOAP
106 static void parse_df(PWEATHER_FORECAST *res, xmlXPathContextPtr xpathCtx)
107 {
108         int i, j, k;
109         char *content;
110         xmlXPathObjectPtr xpathObj;
111
112         xpathObj = xmlXPathEvalExpression((const xmlChar *)"/error/err", xpathCtx);
113         if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0 &&
114                         xpathObj->nodesetval->nodeTab[0]->type == XML_ELEMENT_NODE) {
115                 content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
116                 NORM_ERR("XOAP error: %s", content);
117                 xmlFree(content);
118                 xmlXPathFreeObject(xpathObj);
119                 return;
120         }
121         xmlXPathFreeObject(xpathObj);
122
123         for (i = 0; i < NUM_XPATH_EXPRESSIONS_DF; i++) {
124                 xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath_expression_df[i], xpathCtx);
125                 if (xpathObj != NULL) {
126                         xmlNodeSetPtr nodes = xpathObj->nodesetval;
127                         k = 0;
128                         for (j = 0; j < nodes->nodeNr; ++j) {
129                                 if (nodes->nodeTab[j]->type == XML_ELEMENT_NODE) {
130                                         content = (char *)xmlNodeGetContent(nodes->nodeTab[k]);
131                                         switch(i) {
132                                         case 0:
133                                                 res->hi[k] = atoi(content);
134                                                 break;
135                                         case 1:
136                                                 res->low[k] = atoi(content);
137                                                 break;
138                                         case 2:
139                                                 strncpy(res->icon[k], content, 2);
140                                         case 3:
141                                                 strncpy(res->xoap_t[k], content, 31);
142                                                 break;
143                                         case 4:
144                                                 res->wind_s[k] = atoi(content);
145                                                 break;
146                                         case 5:
147                                                 res->wind_d[k] = atoi(content);
148                                                 break;
149                                         case 6:
150                                                 res->ppcp[k] = atoi(content);
151                                                 break;
152                                         case 7:
153                                                 res->hmid[k] = atoi(content);
154                                         }
155                                 } else if (nodes->nodeTab[j]->type == XML_ATTRIBUTE_NODE) {
156                                         content = (char *)xmlNodeGetContent(nodes->nodeTab[k]);
157                                         switch(i) {
158                                         case 8:
159                                                 strncpy(res->day[k], content, 8);
160                                                 break;
161                                         case 9:
162                                                 strncpy(res->date[k], content, 6);
163                                         }
164                                 }
165                                 xmlFree(content);
166                                 if (++k == FORECAST_DAYS) break;
167                         }
168                 }
169                 xmlXPathFreeObject(xpathObj);
170         }
171         return;
172 }
173
174 static void parse_weather_forecast_xml(PWEATHER_FORECAST *res, const char *data)
175 {
176         xmlDocPtr doc;
177         xmlXPathContextPtr xpathCtx;
178
179         if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
180                 NORM_ERR("weather_forecast: can't read xml data");
181                 return;
182         }
183
184         xpathCtx = xmlXPathNewContext(doc);
185         if(xpathCtx == NULL) {
186                 NORM_ERR("weather_forecast: unable to create new XPath context");
187                 xmlFreeDoc(doc);
188                 return;
189         }
190
191         parse_df(res, xpathCtx);
192         xmlXPathFreeContext(xpathCtx);
193         xmlFreeDoc(doc);
194         return;
195 }
196
197 static void parse_cc(PWEATHER *res, xmlXPathContextPtr xpathCtx)
198 {
199         int i;
200         char *content;
201         xmlXPathObjectPtr xpathObj;
202
203         xpathObj = xmlXPathEvalExpression((const xmlChar *)"/error/err", xpathCtx);
204         if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr > 0 &&
205                         xpathObj->nodesetval->nodeTab[0]->type == XML_ELEMENT_NODE) {
206                 content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
207                 NORM_ERR("XOAP error: %s", content);
208                 xmlFree(content);
209                 xmlXPathFreeObject(xpathObj);
210                 return;
211         }
212         xmlXPathFreeObject(xpathObj);
213
214         for (i = 0; i < NUM_XPATH_EXPRESSIONS_CC; i++) {
215                 xpathObj = xmlXPathEvalExpression((const xmlChar *)xpath_expression_cc[i], xpathCtx);
216                 if (xpathObj && xpathObj->nodesetval && xpathObj->nodesetval->nodeNr >0 &&
217                                 xpathObj->nodesetval->nodeTab[0]->type ==
218                                 XML_ELEMENT_NODE) {
219                         content = (char *)xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]);
220                         switch(i) {
221                                 case 0:
222                                         strncpy(res->lastupd, content, 31);
223                                         break;
224                                 case 1:
225                                         res->temp = atoi(content);
226                                         break;
227                                 case 2:
228                                         strncpy(res->xoap_t, content, 31);
229                                         break;
230                                 case 3:
231                                         res->bar = atoi(content);
232                                         break;
233                                 case 4:
234                                         res->wind_s = atoi(content);
235                                         break;
236                                 case 5:
237                                         res->wind_d = atoi(content);
238                                         break;
239                                 case 6:
240                                         res->hmid = atoi(content);
241                                         break;
242                                 case 7:
243                                         strncpy(res->icon, content, 2);
244                         }
245                         xmlFree(content);
246                 }
247                 xmlXPathFreeObject(xpathObj);
248         }
249         return;
250 }
251
252 static void parse_weather_xml(PWEATHER *res, const char *data)
253 {
254         xmlDocPtr doc;
255         xmlXPathContextPtr xpathCtx;
256
257         if (!(doc = xmlReadMemory(data, strlen(data), "", NULL, 0))) {
258                 NORM_ERR("weather: can't read xml data");
259                 return;
260         }
261
262         xpathCtx = xmlXPathNewContext(doc);
263         if(xpathCtx == NULL) {
264                 NORM_ERR("weather: unable to create new XPath context");
265                 xmlFreeDoc(doc);
266                 return;
267         }
268
269         parse_cc(res, xpathCtx);
270         xmlXPathFreeContext(xpathCtx);
271         xmlFreeDoc(doc);
272         return;
273 }
274 #endif /* XOAP */
275
276 /*
277  * Horrible hack to avoid using regexes
278  *
279  */
280
281 static inline void parse_token(PWEATHER *res, char *token) {
282
283         int i;
284         char s_tmp[64];
285
286         switch (strlen(token)) {
287
288                 //Check all tokens 2 chars long
289                 case 2:
290
291                         //Check if token is a weather condition
292                         for (i=0; i<2; i++) {
293                                 if (!isalpha(token[i])) break;
294                         }
295                         if (i==2) {
296                                 for(i=0; i<NUM_WC_CODES; i++) {
297                                         if (!strncmp(token, WC_CODES[i], 2)) {
298                                                 res->wc=i+1;
299                                                 break;
300                                         }
301                                 }
302                                 return;
303                         }
304
305                         //Check for CB
306                         if (!strcmp(token, "CB")) {
307                                 res->cc = 8;
308                                 return;
309                         }
310
311                         break;
312
313                         //Check all tokens 3 chars long
314                 case 3:
315
316                         //Check if token is a modified weather condition
317                         if ((token[0] == '+') || (token[0] == '-')) {
318                                 for (i=1; i<3; i++) {
319                                         if (!isalpha(token[i])) break;
320                                 }
321                                 if (i==3) {
322                                         for(i=0; i<NUM_WC_CODES; i++) {
323                                                 if (!strncmp(&token[1], WC_CODES[i], 2)) {
324                                                         res->wc=i+1;
325                                                         break;
326                                                 }
327                                         }
328                                         return;
329                                 }
330                         }
331
332                         //Check for NCD or NSC
333                         if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
334                                 res->cc = 1;
335                                 return;
336                         }
337
338                         //Check for TCU
339                         if (!strcmp(token, "TCU")) {
340                                 res->cc = 7;
341                                 return;
342                         }
343
344                         break;
345
346                         //Check all tokens 4 chars long
347                 case 4:
348
349                         //Check if token is a modified weather condition
350                         for(i=0; i<NUM_WM_CODES; i++) {
351                                 if (!strncmp(token, WM_CODES[i], 2)) {
352                                         for(i=0; i<NUM_WC_CODES; i++) {
353                                                 if (!strncmp(&token[2], WC_CODES[i], 2)) {
354                                                         res->wc=i+1;
355                                                         return;
356                                                 }
357                                         }
358                                         break;
359                                 }
360                         }
361
362                         break;
363
364                         //Check all tokens 5 chars long
365                 case 5:
366
367                         //Check for CAVOK
368                         if (!strcmp(token, "CAVOK")) {
369                                 res->cc = 1;
370                                 return;
371                         }
372
373                         //Check if token is the temperature
374                         for (i=0; i<2; i++) {
375                                 if (!isdigit(token[i])) break;
376                         }
377                         if ((i==2) && (token[2] == '/')) {
378                                 for (i=3; i<5; i++) {
379                                         if (!isdigit(token[i])) break;
380                                 }
381                                 if (i==5) {
382                                         //First 2 digits gives the air temperature
383                                         res->temp=atoi(token);
384
385                                         //4th and 5th digits gives the dew point temperature
386                                         res->dew=atoi(&token[3]);
387
388                                         //Compute humidity
389                                         res->hmid = rel_humidity(res->dew, res->temp);
390
391                                         return;
392                                 }
393                         }
394
395                         //Check if token is the pressure
396                         if ((token[0] == 'Q') || (token[0] == 'A')) {
397                                 for (i=1; i<5; i++) {
398                                         if (!isdigit(token[i])) break;
399                                 }
400                                 if (i==5) {
401                                         if (token[0] == 'A') {
402                                                 //Convert inches of mercury to mbar
403                                                 res->bar = (int)(atoi(&token[1])*0.338637526f);
404                                                 return;
405                                         }
406
407                                         //Last 4 digits is pressure im mbar
408                                         res->bar = atoi(&token[1]);
409                                         return;
410                                 }
411                         }
412
413                         //Check if token is a modified weather condition
414                         if ((token[0] == '+') || (token[0] == '-')) {
415                                 for(i=0; i<NUM_WM_CODES; i++) {
416                                         if (!strncmp(&token[1], WM_CODES[i], 2)) {
417                                                 for(i=0; i<NUM_WC_CODES; i++) {
418                                                         if (!strncmp(&token[3], WC_CODES[i], 2)) {
419                                                                 res->wc=i+1;
420                                                                 return;
421                                                         }
422                                                 }
423                                                 break;
424                                         }
425                                 }
426                         }
427                         break;
428
429                         //Check all tokens 6 chars long
430                 case 6:
431
432                         //Check if token is the cloud cover
433                         for (i=0; i<3; i++) {
434                                 if (!isalpha(token[i])) break;
435                         }
436                         if (i==3) {
437                                 for (i=3; i<6; i++) {
438                                         if (!isdigit(token[i])) break;
439                                 }
440                                 if (i==6) {
441                                         //Check if first 3 digits gives the cloud cover condition
442                                         for(i=0; i<NUM_CC_CODES; i++) {
443                                                 if (!strncmp(token, CC_CODES[i], 3)) {
444                                                         res->cc=i+1;
445                                                         break;
446                                                 }
447                                         }
448                                         return;
449                                 }
450                         }
451
452                         //Check if token is positive temp and negative dew
453                         for (i=0; i<2; i++) {
454                                 if (!isdigit(token[i])) break;
455                         }
456                         if ((i==2) && (token[2] == '/')  && (token[3] == 'M')) {
457                                 for (i=4; i<6; i++) {
458                                         if (!isdigit(token[i])) break;
459                                 }
460                                 if (i==6) {
461                                         //1st and 2nd digits gives the temperature
462                                         res->temp = atoi(token);
463
464                                         //5th and 6th digits gives the dew point temperature
465                                         res->dew = -atoi(&token[4]);
466
467                                         //Compute humidity
468                                         res->hmid = rel_humidity(res->dew, res->temp);
469
470                                         return;
471                                 }
472                         }
473
474                         break;
475
476                         //Check all tokens 7 chars long
477                 case 7:
478
479                         //Check if token is the observation time
480                         for (i=0; i<6; i++) {
481                                 if (!isdigit(token[i])) break;
482                         }
483                         if ((i==6) && (token[6] == 'Z')) return;
484
485                         //Check if token is the wind speed/direction in knots
486                         for (i=0; i<5; i++) {
487                                 if (!isdigit(token[i])) break;
488                         }
489                         if ((i==5) && (token[5] == 'K') &&  (token[6] == 'T')) {
490
491                                 //First 3 digits are wind direction
492                                 strncpy(s_tmp, token, 3);
493                                 s_tmp[3]='\0';
494                                 res->wind_d=atoi(s_tmp);
495
496                                 //4th and 5th digit are wind speed in knots (convert to km/hr)
497                                 res->wind_s = (int)(atoi(&token[3])*1.852);
498
499                                 return;
500                         }
501
502                         //Check if token is negative temperature
503                         if ((token[0] == 'M') && (token[4] == 'M')) {
504                                 for (i=1; i<3; i++) {
505                                         if (!isdigit(token[i])) break;
506                                 }
507                                 if ((i==3) && (token[3] == '/')) {
508                                         for (i=5; i<7; i++) {
509                                                 if (!isdigit(token[i])) break;
510                                         }
511                                         if (i==7) {
512                                                 //2nd and 3rd digits gives the temperature
513                                                 res->temp = -atoi(&token[1]);
514
515                                                 //6th and 7th digits gives the dew point temperature
516                                                 res->dew = -atoi(&token[5]);
517
518                                                 //Compute humidity
519                                                 res->hmid = rel_humidity(res->dew, res->temp);
520
521                                                 return;
522                                         }
523                                 }
524                         }
525
526                         //Check if token is wind variability
527                         for (i=0; i<3; i++) {
528                                 if (!isdigit(token[i])) break;
529                         }
530                         if ((i==3) && (token[3] == 'V')) {
531                                 for (i=4; i<7; i++) {
532                                         if (!isdigit(token[i])) break;
533                                 }
534                                 if (i==7) return;
535                         }
536
537                         break;
538
539                         //Check all tokens 8 chars long
540                 case 8:
541
542                         //Check if token is the wind speed/direction in m/s
543                         for (i=0; i<5; i++) {
544                                 if (!isdigit(token[i])) break;
545                         }
546                         if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
547
548                                 //First 3 digits are wind direction
549                                 strncpy(s_tmp, token, 3);
550                                 s_tmp[3]='\0';
551                                 res->wind_d=atoi(s_tmp);
552
553                                 //4th and 5th digit are wind speed in m/s (convert to km/hr)
554                                 res->wind_s = (int)(atoi(&token[3])*3.6);
555
556                                 return;
557                         }
558
559                 default:
560
561                         //printf("token : %s\n", token);
562                         break;
563         }
564 }
565
566 #ifdef XOAP
567 void parse_weather_forecast(void *result, const char *data)
568 {
569         PWEATHER_FORECAST *res = (PWEATHER_FORECAST*)result;
570         /* Reset results */
571         memset(res, 0, sizeof(PWEATHER_FORECAST));
572
573         //Check if it is an xml file
574         if ( strncmp(data, "<?xml ", 6) == 0 ) {
575                 parse_weather_forecast_xml(res, data);
576         }
577 }
578 #endif /* XOAP */
579
580 void parse_weather(void *result, const char *data)
581 {
582         PWEATHER *res = (PWEATHER*)result;
583         /* Reset results */
584         memset(res, 0, sizeof(PWEATHER));
585
586 #ifdef XOAP
587         //Check if it is an xml file
588         if ( strncmp(data, "<?xml ", 6) == 0 ) {
589                 parse_weather_xml(res, data);
590         } else
591 #endif /* XOAP */
592         {
593                 //We assume its a text file
594                 char s_tmp[256];
595                 const char delim[] = " ";
596
597                 //Divide time stamp and metar data
598                 if (sscanf(data, "%[^'\n']\n%[^'\n']", res->lastupd, s_tmp) == 2) {
599
600                         //Process all tokens
601                         char *p_tok = NULL;
602                         char *p_save = NULL;
603
604                         if ((strtok_r(s_tmp, delim, &p_save)) != NULL) {
605
606                                 //Jump first token, must be icao
607                                 p_tok = strtok_r(NULL, delim, &p_save);
608
609                                 do {
610
611                                         parse_token(res, p_tok);
612                                         p_tok = strtok_r(NULL, delim, &p_save);
613
614                                 } while (p_tok != NULL);
615                         }
616                         return;
617                 }
618                 else {
619                         return;
620                 }
621         }
622 }
623
624 void wind_deg_to_dir(char *p, int p_max_size, int wind_deg) {
625         if ((wind_deg >= 349) || (wind_deg < 12)) {
626                 strncpy(p, "N", p_max_size);
627         } else if (wind_deg < 33) {
628                 strncpy(p, "NNE", p_max_size);
629         } else if (wind_deg < 57) {
630                 strncpy(p, "NE", p_max_size);
631         } else if (wind_deg < 79) {
632                 strncpy(p, "ENE", p_max_size);
633         } else if (wind_deg < 102) {
634                 strncpy(p, "E", p_max_size);
635         } else if (wind_deg < 124) {
636                 strncpy(p, "ESE", p_max_size);
637         } else if (wind_deg < 147) {
638                 strncpy(p, "SE", p_max_size);
639         } else if (wind_deg < 169) {
640                 strncpy(p, "SSE", p_max_size);
641         } else if (wind_deg < 192) {
642                 strncpy(p, "S", p_max_size);
643         } else if (wind_deg < 214) {
644                 strncpy(p, "SSW", p_max_size);
645         } else if (wind_deg < 237) {
646                 strncpy(p, "SW", p_max_size);
647         } else if (wind_deg < 259) {
648                 strncpy(p, "WSW", p_max_size);
649         } else if (wind_deg < 282) {
650                         strncpy(p, "W", p_max_size);
651         } else if (wind_deg < 304) {
652                 strncpy(p, "WNW", p_max_size);
653         } else if (wind_deg < 327) {
654                 strncpy(p, "NW", p_max_size);
655         } else if (wind_deg < 349) {
656                 strncpy(p, "NNW", p_max_size);
657         };
658 }
659
660 #ifdef XOAP
661 void weather_forecast_process_info(char *p, int p_max_size, char *uri, unsigned int day, char *data_type, int interval)
662 {
663         PWEATHER_FORECAST *data;
664
665         ccurl_location_t *curloc = ccurl_find_location(&locations_head_df, uri);
666         if (!curloc->p_timed_thread) {
667                 curloc->result = malloc(sizeof(PWEATHER_FORECAST));
668                 memset(curloc->result, 0, sizeof(PWEATHER_FORECAST));
669                 curloc->process_function = &parse_weather_forecast;
670                 ccurl_init_thread(curloc, interval);
671                 if (!curloc->p_timed_thread) {
672                         NORM_ERR("error setting up weather_forecast thread");
673                 }
674         }
675
676         timed_thread_lock(curloc->p_timed_thread);
677         data = (PWEATHER_FORECAST*)curloc->result;
678         if (strcmp(data_type, "hi") == EQUAL) {
679                 temp_print(p, p_max_size, data->hi[day], TEMP_CELSIUS);
680         } else if (strcmp(data_type, "low") == EQUAL) {
681                 temp_print(p, p_max_size, data->low[day], TEMP_CELSIUS);
682         } else if (strcmp(data_type, "icon") == EQUAL) {
683                 strncpy(p, data->icon[day], p_max_size);
684         } else if (strcmp(data_type, "forecast") == EQUAL) {
685                 strncpy(p, data->xoap_t[day], p_max_size);
686         } else if (strcmp(data_type, "wind_speed") == EQUAL) {
687                 snprintf(p, p_max_size, "%d", data->wind_s[day]);
688         } else if (strcmp(data_type, "wind_dir") == EQUAL) {
689                 wind_deg_to_dir(p, p_max_size, data->wind_d[day]);
690         } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
691                 snprintf(p, p_max_size, "%d", data->wind_d[day]);
692         } else if (strcmp(data_type, "humidity") == EQUAL) {
693                 snprintf(p, p_max_size, "%d", data->hmid[day]);
694         } else if (strcmp(data_type, "precipitation") == EQUAL) {
695                 snprintf(p, p_max_size, "%d", data->ppcp[day]);
696         } else if (strcmp(data_type, "day") == EQUAL) {
697                 strncpy(p, data->day[day], p_max_size);
698         } else if (strcmp(data_type, "date") == EQUAL) {
699                 strncpy(p, data->date[day], p_max_size);
700         }
701
702         timed_thread_unlock(curloc->p_timed_thread);
703 }
704 #endif /* XOAP */
705
706 void weather_process_info(char *p, int p_max_size, char *uri, char *data_type, int interval)
707 {
708         static const char *wc[] = {
709                 "", "drizzle", "rain", "hail", "soft hail",
710                 "snow", "snow grains", "fog", "haze", "smoke",
711                 "mist", "dust", "sand", "funnel cloud tornado",
712                 "dust/sand", "squall", "sand storm", "dust storm"
713         };
714         PWEATHER *data;
715
716         ccurl_location_t *curloc = ccurl_find_location(&locations_head_cc, uri);
717         if (!curloc->p_timed_thread) {
718                 curloc->result = malloc(sizeof(PWEATHER));
719                 memset(curloc->result, 0, sizeof(PWEATHER));
720                 curloc->process_function = &parse_weather;
721                 ccurl_init_thread(curloc, interval);
722                 if (!curloc->p_timed_thread) {
723                         NORM_ERR("error setting up weather thread");
724                 }
725         }
726
727         timed_thread_lock(curloc->p_timed_thread);
728         data = (PWEATHER*)curloc->result;
729         if (strcmp(data_type, "last_update") == EQUAL) {
730                 strncpy(p, data->lastupd, p_max_size);
731         } else if (strcmp(data_type, "temperature") == EQUAL) {
732                 temp_print(p, p_max_size, data->temp, TEMP_CELSIUS);
733         } else if (strcmp(data_type, "cloud_cover") == EQUAL) {
734 #ifdef XOAP
735                 if (data->xoap_t[0] != '\0') {
736                         char *s = p;
737                         strncpy(p, data->xoap_t, p_max_size);
738                         while (*s) {
739                                 *s = tolower(*s);
740                                 s++;
741                         }
742                 } else
743 #endif /* XOAP */
744                         if (data->cc == 0) {
745                                 strncpy(p, "", p_max_size);
746                         } else if (data->cc < 3) {
747                                 strncpy(p, "clear", p_max_size);
748                         } else if (data->cc < 5) {
749                                 strncpy(p, "partly cloudy", p_max_size);
750                         } else if (data->cc == 5) {
751                                 strncpy(p, "cloudy", p_max_size);
752                         } else if (data->cc == 6) {
753                                 strncpy(p, "overcast", p_max_size);
754                         } else if (data->cc == 7) {
755                                 strncpy(p, "towering cumulus", p_max_size);
756                         } else  {
757                                 strncpy(p, "cumulonimbus", p_max_size);
758                         }
759 #ifdef XOAP
760         } else if (strcmp(data_type, "icon") == EQUAL) {
761                 strncpy(p, data->icon, p_max_size);
762 #endif /* XOAP */
763         } else if (strcmp(data_type, "pressure") == EQUAL) {
764                 snprintf(p, p_max_size, "%d", data->bar);
765         } else if (strcmp(data_type, "wind_speed") == EQUAL) {
766                 snprintf(p, p_max_size, "%d", data->wind_s);
767         } else if (strcmp(data_type, "wind_dir") == EQUAL) {
768                 wind_deg_to_dir(p, p_max_size, data->wind_d);
769         } else if (strcmp(data_type, "wind_dir_DEG") == EQUAL) {
770                 snprintf(p, p_max_size, "%d", data->wind_d);
771
772         } else if (strcmp(data_type, "humidity") == EQUAL) {
773                 snprintf(p, p_max_size, "%d", data->hmid);
774         } else if (strcmp(data_type, "weather") == EQUAL) {
775                 strncpy(p, wc[data->wc], p_max_size);
776         }
777
778         timed_thread_unlock(curloc->p_timed_thread);
779 }
780
781 #ifdef XOAP
782
783 /* xoap suffix for weather from weather.com */
784 static char *xoap_cc = NULL;
785 static char *xoap_df = NULL;
786
787 /*
788  * TODO: make the xoap keys file readable from the config file
789  *       make the keys directly readable from the config file
790  *       make the xoap keys file giveable as a command line option
791  */
792 void load_xoap_keys(void)
793 {
794         FILE *fp;
795         char *par  = (char *) malloc(11 * sizeof(char));
796         char *key  = (char *) malloc(17 * sizeof(char));
797         char *xoap = (char *) malloc(64 * sizeof(char));
798
799         to_real_path(xoap, XOAP_FILE);
800         fp = fopen(xoap, "r");
801         if (fp != NULL) {
802                 if (fscanf(fp, "%10s %16s", par, key) == 2) {
803                         xoap_cc = (char *) malloc(128 * sizeof(char));
804                         xoap_df = (char *) malloc(128 * sizeof(char));
805
806                         strcpy(xoap_cc, "?cc=*&link=xoap&prod=xoap&par=");
807                         strcat(xoap_cc, par);
808                         strcat(xoap_cc, "&key=");
809                         strcat(xoap_cc, key);
810                         strcat(xoap_cc, "&unit=m");
811
812                         /* TODO: Use FORECAST_DAYS instead of 5 */
813                         strcpy(xoap_df, "?dayf=5&link=xoap&prod=xoap&par=");
814                         strcat(xoap_df, par);
815                         strcat(xoap_df, "&key=");
816                         strcat(xoap_df, key);
817                         strcat(xoap_df, "&unit=m");
818                 }
819                 fclose(fp);
820         }
821         free(par);
822         free(key);
823         free(xoap);
824 }
825 #endif /* XOAP */
826
827 int process_weather_uri(char *uri, char *locID, int dayf UNUSED_ATTR)
828 {
829         /* locID MUST BE upper-case */
830         char *tmp_p = locID;
831
832         while (*tmp_p) {
833                 *tmp_p = toupper(*tmp_p);
834                 tmp_p++;
835         }
836
837         /* Construct complete uri */
838 #ifdef XOAP
839         if (strstr(uri, "xoap.weather.com")) {
840                 if ((dayf == 0) && (xoap_cc != NULL)) {
841                         strcat(uri, locID);
842                         strcat(uri, xoap_cc);
843                 } else if ((dayf == 1) && (xoap_df != NULL)) {
844                         strcat(uri, locID);
845                         strcat(uri, xoap_df);
846                 } else {
847                         free(uri);
848                         uri = NULL;
849                 }
850         } else
851 #endif /* XOAP */
852         if (strstr(uri, "weather.noaa.gov")) {
853                 strcat(uri, locID);
854                 strcat(uri, ".TXT");
855         } else  if (!strstr(uri, "localhost") && !strstr(uri, "127.0.0.1")) {
856                 return -1;
857         }
858         return 0;
859 }