# HG changeset patch # User Matti Hamalainen # Date 1433346702 -10800 # Node ID b3fded816ad82162577c70782966305a10ca72c6 # Parent a5282cdc56e693958bfdde2009fea5fb9dafa701# Parent fe12434d6cbd5a675786e265ee49edcbd862d66b Merged. diff -r a5282cdc56e6 -r b3fded816ad8 fetch_weather.pl --- a/fetch_weather.pl Wed Jun 03 18:51:30 2015 +0300 +++ b/fetch_weather.pl Wed Jun 03 18:51:42 2015 +0300 @@ -344,6 +344,19 @@ } +sub opt_get_int($) +{ + if (defined($settings{$_[0]})) + { + return int($settings{$_[0]}); + } + else + { + return -1; + } +} + + sub opt_get($) { if (defined($settings{$_[0]})) @@ -404,7 +417,7 @@ my $weatherdata = {}; die( -"Weather Fetch v0.1 by ccr/TNSP \n". +"Weather Fetch v0.3 by ccr/TNSP \n". "Usage: $0 \n" ) unless scalar(@ARGV) >= 1; @@ -419,8 +432,9 @@ { for (my $i = 1; $i <= 22; $i++) { - my $qurl = "http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_".$i.".html"; - my $res = fetch_http($qurl); + my $uri = "http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_".$i.".html"; + print STDERR "Fetching ".$uri." ...\n" if (opt_get_int("debug") > 1); + my $res = fetch_http($uri); if ($res->code >= 200 && $res->code <= 201) { my $data = $res->decoded_content; @@ -446,10 +460,12 @@ my $otree = parse_html($data); if (!defined($otree)) { - print STDERR "ERROR: Failed to parse file '".$qurl."'.\n"; + print STDERR "ERROR: Failed to parse file '".$uri."'.\n"; next; } + print STDERR "Parsed : ".$uri." as:\n".Dumper($otree)."\n--\n" if (opt_get_int("debug") > 2); + # Find our desired element nodes my $odata = fnodea(fnodep($otree, "body"), "div", "class=elementc"); my $oupdate = fnodep($odata, "p"); @@ -481,7 +497,13 @@ } } } + else + { + print STDERR "Failed to fetch ".$uri." (\n" if (opt_get_int("debug") > 0); + } } + + print STDERR "Tiehallinto data blob:\n".Dumper($weatherdata)."\n--\n" if (opt_get_int("debug") > 1); } @@ -500,7 +522,7 @@ "&starttime=".format_time_gmt(time() - 10*60)."&endtime=".format_time_gmt(time()). "¶meters=".join(",", @fmitems)."&maxlocations=100&bbox=19,59,32,75"; - print STDERR "FMI URI: ".$uri."\n" if opt_chk_bool("debug"); + print STDERR "FMI URI: ".$uri."\n" if (opt_get_int("debug") > 0); my $res = fetch_http($uri); if ($res->code >= 200 && $res->code <= 201)