File: | weather-met.c |
Warning: | line 149, column 18 Value stored to 'session' during its initialization is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
2 | /* weather-met.c - UK Met Office forecast source |
3 | * |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License as |
6 | * published by the Free Software Foundation; either version 2 of the |
7 | * License, or (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, see |
16 | * <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #ifdef HAVE_CONFIG_H1 |
20 | #include <config.h> |
21 | #endif |
22 | |
23 | #include <ctype.h> |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | |
27 | #define CAFEWEATHER_I_KNOW_THIS_IS_UNSTABLE |
28 | #include "weather.h" |
29 | #include "weather-priv.h" |
30 | |
31 | static char * |
32 | met_reprocess (char *x, int len) |
33 | { |
34 | char *p = x; |
35 | char *o; |
36 | int spacing = 0; |
37 | static gchar *buf; |
38 | static gint buflen = 0; |
39 | gchar *lastspace = NULL((void*)0); |
40 | int count = 0; |
41 | |
42 | if (buflen < len) |
43 | { |
44 | if (buf) |
45 | g_free (buf); |
46 | buf = g_malloc (len + 1); |
47 | buflen = len; |
48 | } |
49 | |
50 | o = buf; |
51 | x += len; /* End mark */ |
52 | |
53 | while (*p && p < x) { |
54 | if (g_ascii_isspace (*p)((g_ascii_table[(guchar) (*p)] & G_ASCII_SPACE) != 0)) { |
55 | if (!spacing) { |
56 | spacing = 1; |
57 | lastspace = o; |
58 | count++; |
59 | *o++ = ' '; |
60 | } |
61 | p++; |
62 | continue; |
63 | } |
64 | spacing = 0; |
65 | if (count > 75 && lastspace) { |
66 | count = o - lastspace - 1; |
67 | *lastspace = '\n'; |
68 | lastspace = NULL((void*)0); |
69 | } |
70 | |
71 | if (*p == '&') { |
72 | if (g_ascii_strncasecmp (p, "&", 5) == 0) { |
73 | *o++ = '&'; |
74 | count++; |
75 | p += 5; |
76 | continue; |
77 | } |
78 | if (g_ascii_strncasecmp (p, "<", 4) == 0) { |
79 | *o++ = '<'; |
80 | count++; |
81 | p += 4; |
82 | continue; |
83 | } |
84 | if (g_ascii_strncasecmp (p, ">", 4) == 0) { |
85 | *o++ = '>'; |
86 | count++; |
87 | p += 4; |
88 | continue; |
89 | } |
90 | } |
91 | if (*p == '<') { |
92 | if (g_ascii_strncasecmp (p, "<BR>", 4) == 0) { |
93 | *o++ = '\n'; |
94 | count = 0; |
95 | } |
96 | if (g_ascii_strncasecmp (p, "<B>", 3) == 0) { |
97 | *o++ = '\n'; |
98 | *o++ = '\n'; |
99 | count = 0; |
100 | } |
101 | p++; |
102 | while (*p && *p != '>') |
103 | p++; |
104 | if (*p) |
105 | p++; |
106 | continue; |
107 | } |
108 | *o++ = *p++; |
109 | count++; |
110 | } |
111 | *o = 0; |
112 | return buf; |
113 | } |
114 | |
115 | |
116 | /* |
117 | * Parse the metoffice forecast info. |
118 | * For cafe 3.0 we want to just embed an HTML cafecomponent component and |
119 | * be done with this ;) |
120 | */ |
121 | |
122 | static gchar * |
123 | met_parse (const gchar *meto) |
124 | { |
125 | gchar *p; |
126 | gchar *rp; |
127 | gchar *r = g_strdup ("Met Office Forecast\n")g_strdup_inline ("Met Office Forecast\n"); |
128 | gchar *t; |
129 | |
130 | g_return_val_if_fail (meto != NULL, r)do { if ((meto != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "meto != NULL"); return (r); } } while (0); |
131 | |
132 | p = strstr (meto, "Summary: </b>"); |
133 | g_return_val_if_fail (p != NULL, r)do { if ((p != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "p != NULL"); return (r); } } while (0); |
134 | |
135 | rp = strstr (p, "Text issued at:"); |
136 | g_return_val_if_fail (rp != NULL, r)do { if ((rp != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "rp != NULL"); return (r); } } while (0); |
137 | |
138 | p += 13; |
139 | /* p to rp is the text block we want but in HTML malformat */ |
140 | t = g_strconcat (r, met_reprocess (p, rp - p), NULL((void*)0)); |
141 | g_free (r); |
142 | |
143 | return t; |
144 | } |
145 | |
146 | static void |
147 | met_finish (GObject *object, GAsyncResult *result, gpointer data) |
148 | { |
149 | SoupSession *session = SOUP_SESSION (object); |
Value stored to 'session' during its initialization is never read | |
150 | SoupMessage *msg = soup_session_get_async_result_message (SOUP_SESSION (object), result); |
151 | WeatherInfo *info = (WeatherInfo *)data; |
152 | GBytes *response_body = NULL((void*)0); |
153 | const gchar *msgdata; |
154 | |
155 | g_return_if_fail (info != NULL)do { if ((info != ((void*)0))) { } else { g_return_if_fail_warning ("CafeWeather", ((const char*) (__func__)), "info != NULL"); return; } } while (0); |
156 | |
157 | response_body = soup_session_send_and_read_finish(SOUP_SESSION(object), |
158 | result, NULL((void*)0)/*&error*/); |
159 | |
160 | if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))((soup_message_get_status (msg)) >= 200 && (soup_message_get_status (msg)) < 300)) { |
161 | g_warning ("Failed to get Met Office forecast data: %d %s.\n", |
162 | soup_message_get_status (msg), soup_message_get_reason_phrase (msg)); |
163 | request_done (info, FALSE(0)); |
164 | return; |
165 | } |
166 | |
167 | msgdata = g_bytes_get_data(response_body, NULL((void*)0)/*&length*/); |
168 | |
169 | info->forecast = met_parse (msgdata /*soup_server_message_get_response_body (msg)->data*/); |
170 | request_done (info, TRUE(!(0))); |
171 | } |
172 | |
173 | void |
174 | metoffice_start_open (WeatherInfo *info) |
175 | { |
176 | gchar *url; |
177 | SoupMessage *msg; |
178 | WeatherLocation *loc; |
179 | |
180 | loc = info->location; |
181 | url = g_strdup_printf ("http://www.metoffice.gov.uk/weather/europe/uk/%s.html", loc->zone + 1); |
182 | |
183 | msg = soup_message_new ("GET", url); |
184 | soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT0, NULL((void*)0), met_finish, info); |
185 | g_free (url); |
186 | |
187 | info->requests_pending++; |
188 | } |