# HG changeset patch # User Matti Hamalainen # Date 1401797447 -10800 # Node ID 9b8ec700ede4459148c8e43b62ec3191e1121595 # Parent eda776bcb7eda06bf2f65540b48fc7cd3d494dd8 Clean up the weather data parser backend a bit. diff -r eda776bcb7ed -r 9b8ec700ede4 fetch_weather.pl --- a/fetch_weather.pl Tue Jun 03 13:06:13 2014 +0300 +++ b/fetch_weather.pl Tue Jun 03 15:10:47 2014 +0300 @@ -19,6 +19,7 @@ "outfile" => "", ); + ### ### Helper functions ### @@ -28,6 +29,24 @@ } +sub fetch_http($) +{ + my $tmpAgent = LWP::UserAgent->new; + $tmpAgent->agent("Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 6.0) Opera 10.63 [en]"); + $tmpAgent->timeout(10); + + my $tmpRequest = HTTP::Request->new(GET => $_[0]); + + return $tmpAgent->request($tmpRequest); +} + + +sub parse_timestamp($$) +{ + return str2time($_[0]) - str2time("00:00") + $_[1]; +} + + sub str_trim($) { my $str = $_[0]; @@ -39,6 +58,9 @@ } +### +### Loose HTML parser +### sub pop_token_a($) { my $tokens = $_[0]; @@ -159,6 +181,10 @@ } +### +### Helper functions for locating/traversing nodes +### in the parsed data tree structure. +### sub fnode($$) { return html_find_node($_[0], $_[1], ""); @@ -171,32 +197,14 @@ } -sub fetch_http($) -{ - my $tmpAgent = LWP::UserAgent->new; - $tmpAgent->agent("Mozilla/4.0 (compatible; MSIE 6.0; MSIE 5.5; Windows NT 6.0) Opera 10.63 [en]"); - $tmpAgent->timeout(10); - - my $tmpRequest = HTTP::Request->new(GET => $_[0]); - - return $tmpAgent->request($tmpRequest); -} - - sub get_node($$$) { return defined($_[0]->[$_[2]]{"nodes"}[0]{$_[1]}) ? $_[0]->[$_[2]]{"nodes"}[0]{$_[1]} : ""; } -sub parse_timestamp($$) -{ - return str2time($_[0]) - str2time("00:00") + $_[1]; -} - - ### -### Configuration +### Configuration handling ### sub opt_chk_bool($) { @@ -290,10 +298,13 @@ "Usage: $0 \n" ) unless scalar(@ARGV) >= 1; -opt_read_config(shift) == 0 or exit; +my $cfgfile = shift; +opt_read_config($cfgfile) == 0 or die("Errors while parsing configuration file '".$cfgfile."'.\n"); -### Fetch tiehallinto data +### +### Fetch tiehallinto road weather measurement data +### if (opt_chk_bool("opt_tiehallinto")) { for (my $i = 1; $i <= 22; $i++) @@ -350,7 +361,10 @@ } } + +### ### Fetch FMI data +### if (opt_chk_bool("opt_fmi")) { die("FMI data scrape enabled, but no API key set.\n") unless opt_chk_valid("fmi_api_key", 10); @@ -392,7 +406,9 @@ } +### ### Output +### if (opt_chk_valid("outfile", 1)) { open(STDOUT, '>', opt_get("outfile")) or die("Could not open output file '".opt_get("outfile")."'.\n"); }