comparison fetch_weather.pl @ 162:4f8a163b2bc1

Work on the backend weather data fetching.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 02 Jun 2014 15:56:01 +0300
parents db2b1f74d994
children d5a0e4248f3e
comparison
equal deleted inserted replaced
161:2c9db9a29429 162:4f8a163b2bc1
5 use XML::Simple; 5 use XML::Simple;
6 use Text::Iconv; 6 use Text::Iconv;
7 use Date::Parse; 7 use Date::Parse;
8 use Data::Dumper; 8 use Data::Dumper;
9 9
10 ###
11 ### Settings
12 ###
13 my $opt_outfile = "/home/niinuska/bot/weather.data";
14
15 ###
16 ### Helper functions
17 ###
10 sub str_trim($) 18 sub str_trim($)
11 { 19 {
12 my $str = $_[0]; 20 my $str = $_[0];
13 if (defined($str)) { 21 if (defined($str)) {
14 $str =~ s/^\s*//; 22 $str =~ s/^\s*//;
168 } 176 }
169 177
170 178
171 sub parse_timestamp($$) 179 sub parse_timestamp($$)
172 { 180 {
173 return $_[1] + str2time($_[0]) - time(); 181 return str2time($_[0]) - str2time("00:00") + $_[1];
174 } 182 }
175 183
176 184
177 ### 185 ###
178 ### Main program begins 186 ### Main program begins
179 ### 187 ###
180 die(
181 "Weather Fetch v0.1 by ccr/TNSP <ccr\@tnsp.org>\n".
182 "Usage: $0 <output_data_file> <fmi_api_key>\n"
183 ) unless scalar(@ARGV) >= 2;
184
185 my $opt_outfile = shift;
186 my $opt_api_key = shift;
187 my $weatherdata = {}; 188 my $weatherdata = {};
188 189
189 ### Fetch tiehallinto data 190 ### Fetch tiehallinto data
190 for (my $i = 1; $i <= 22; $i++) 191 for (my $i = 1; $i <= 22; $i++)
191 { 192 {
215 my $otree = parse_html($data); 216 my $otree = parse_html($data);
216 217
217 # Find our desired element nodes 218 # Find our desired element nodes
218 my $odata = fnodea(fnode($otree, "body"), "div", "class=elementc"); 219 my $odata = fnodea(fnode($otree, "body"), "div", "class=elementc");
219 my $oupdate = fnode($odata, "p"); 220 my $oupdate = fnode($odata, "p");
220 my $time_base = time(); 221 my $time_base = str2time("00:00");
221 if ($oupdate) { 222 if ($oupdate) {
222 my $tmp = $oupdate->{"nodes"}[0]{"text"}; 223 my $tmp = $oupdate->{"nodes"}[0]{"text"};
223 if ($tmp =~ /:\s+(\d\d\.\d\d\.\d\d\d\d)\s+(\d\d:\d\d)/) { 224 if ($tmp =~ /:\s+(\d\d\.\d\d\.\d\d\d\d)\s+(\d\d:\d\d)/) {
224 $time_base = str2time($1); 225 $time_base = str2time($1);
225 } 226 }
236 get_node($fdata, "text", 3), 237 get_node($fdata, "text", 3),
237 get_node($fdata, "text", 4), 238 get_node($fdata, "text", 4),
238 get_node($fdata, "text", 5), 239 get_node($fdata, "text", 5),
239 ]; 240 ];
240 } 241 }
241 print STDERR ".";
242 } 242 }
243 } 243 }
244 244
245 ### Fetch FMI data 245 ### Fetch FMI data
246 my $res = fetch_http("http://data.fmi.fi/fmi-apikey/".$opt_api_key. 246 my $res = fetch_http("http://data.fmi.fi/fmi-apikey/".$opt_api_key.