diff fetch_weather.pl @ 232:0dc95d66ba8d

Improve debugging.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 10 Dec 2014 15:34:48 +0200
parents fe420576f284
children dc1afa011883
line wrap: on
line diff
--- a/fetch_weather.pl	Sun Dec 07 01:06:10 2014 +0200
+++ b/fetch_weather.pl	Wed Dec 10 15:34:48 2014 +0200
@@ -157,7 +157,8 @@
       } elsif ($token =~ /^<\/([a-z]+[1-9]*)>$/) {
         return $tree;
       } else {
-        die("HORROR TERROR ELITE: '$token'\n");
+        print STDERR "ERROR: Failed to parse '$token'\n";
+        return undef;
       }
     } else {
       $token = str_trim(decode_entities($token));
@@ -177,8 +178,14 @@
   $str =~ tr/\r/ /;
   $str =~ tr/\n/ /;
   my @tokens = grep { !($_ =~ /^\s*$/) } split(/(<\/?[a-zA-Z]+.*?>)/, $str);
-  parse_html_tree(\@tokens, $res);
-  return $res;
+  if (defined(parse_html_tree(\@tokens, $res)))
+  {
+    return $res;
+  }
+  else
+  {
+    return undef;
+  }
 }
 
 
@@ -376,7 +383,8 @@
 {
   for (my $i = 1; $i <= 22; $i++)
   {
-    my $res = fetch_http("http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_".$i.".html");
+    my $qurl = "http://alk.tiehallinto.fi/alk/tiesaa/tiesaa_maak_".$i.".html";
+    my $res = fetch_http($qurl);
     if ($res->code >= 200 && $res->code <= 201)
     {
       my $data = $res->decoded_content;
@@ -400,6 +408,11 @@
 
       # Parse the HTML mess
       my $otree = parse_html($data);
+      if (!defined($otree))
+      {
+        print STDERR "ERROR: Failed to parse file '".$qurl."'.\n";
+        next;
+      }
 
       # Find our desired element nodes
       my $odata = fnodea(fnodep($otree, "body"), "div", "class=elementc");