This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.c
95 lines (77 loc) · 3.18 KB
/
weather.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
W8UPD Simplex Repeater - A simplex repeater
Copyright (C) 2012 W8UPD
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include "speech_synthesis.h"
CURL *curl;
CURLcode curl_res;
typedef struct {
xmlChar *city;
xmlChar *temp_f;
xmlChar *conditions;
xmlChar *humidity;
xmlChar *wind;
} gweather_t;
gweather_t parseit() {
gweather_t weather;
xmlDocPtr doc = xmlReadFile("/tmp/gweather.xml", NULL, 0);
if (!doc) {
printf("Failed to parse XML file.");
exit(1);
}
xmlXPathContext *context = xmlXPathNewContext(doc);
xmlXPathObjectPtr city = xmlXPathEvalExpression((xmlChar*)"/xml_api_reply/weather/forecast_information/city", context);
weather.city = xmlGetProp(city->nodesetval->nodeTab[0], (xmlChar*)"data");
xmlXPathObjectPtr temp_f = xmlXPathEvalExpression((xmlChar*)"/xml_api_reply/weather/current_conditions/temp_f", context);
weather.temp_f = xmlGetProp(temp_f->nodesetval->nodeTab[0], (xmlChar*)"data");
xmlXPathObjectPtr conditions = xmlXPathEvalExpression((xmlChar*)"/xml_api_reply/weather/current_conditions/condition", context);
weather.conditions = xmlGetProp(conditions->nodesetval->nodeTab[0], (xmlChar*)"data");
xmlXPathObjectPtr wind = xmlXPathEvalExpression((xmlChar*)"/xml_api_reply/weather/current_conditions/wind_condition", context);
weather.wind = xmlGetProp(wind->nodesetval->nodeTab[0], (xmlChar*)"data");
xmlXPathObjectPtr humidity = xmlXPathEvalExpression((xmlChar*)"/xml_api_reply/weather/current_conditions/humidity", context);
weather.humidity = xmlGetProp(humidity->nodesetval->nodeTab[0], (xmlChar*)"data");
xmlFreeDoc(doc);
xmlCleanupParser();
return weather;
}
void fetch_weather(const char *location) {
FILE *tmp = fopen("/tmp/gweather.xml", "w");
if (!tmp) {
printf("Could not open /tmp/gweather.json for some odd reason.\n");
}
char url[100] = "https://www.google.com/ig/api?weather=";
strcat(url, location);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, tmp);
curl_res = curl_easy_perform(curl);
if (curl_res == 0) {
printf("Fetched google weather JSON\n");
} else {
printf("Error.\n");
}
fclose(tmp);
curl_easy_cleanup(curl);
gweather_t weather = parseit();
char result[1000];
snprintf(result, sizeof(result), "Weather for %s. Currently: %s degrees fahrenheit. %s. %s. %s.",
weather.city, weather.temp_f, weather.conditions, weather.wind, weather.humidity);
vocalize(result);
}