diff fetch_weather.pl @ 228:fe420576f284

The kludges intensify.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 07 Dec 2014 01:05:30 +0200
parents 330d7e39c8f4
children 0dc95d66ba8d
line wrap: on
line diff
--- a/fetch_weather.pl	Mon Dec 01 11:42:35 2014 +0200
+++ b/fetch_weather.pl	Sun Dec 07 01:05:30 2014 +0200
@@ -219,9 +219,18 @@
 ### Helper functions for locating/traversing nodes
 ### in the parsed data tree structure.
 ###
-sub fnode($$)
+
+sub fnodep(@)
 {
-  return html_find_node($_[0], $_[1], "");
+  my $res = html_find_node(shift, shift, "");
+  if (defined(my $tmp = shift))
+  {
+    return defined($res) ? $res->{$tmp} : undef;
+  }
+  else
+  {
+    return $res;
+  }
 }
 
 
@@ -255,6 +264,12 @@
 }
 
 
+sub plonk_data($)
+{
+  return defined($_[0]) ? $_[0] : "";
+}
+
+
 ###
 ### Configuration handling
 ###
@@ -387,8 +402,8 @@
       my $otree = parse_html($data);
 
       # Find our desired element nodes
-      my $odata = fnodea(fnode($otree, "body"), "div", "class=elementc");
-      my $oupdate = fnode($odata, "p");
+      my $odata = fnodea(fnodep($otree, "body"), "div", "class=elementc");
+      my $oupdate = fnodep($odata, "p");
       my $time_base = str2time("00:00");
       if ($oupdate) {
         my $tmp = $oupdate->{"nodes"}[0]{"text"};
@@ -397,20 +412,23 @@
         }
       }
       
-      my $oelems = fnode($odata, "table")->{"nodes"};
-      for (my $n = 1; $n < scalar(@$oelems); $n++)
+      my $oelems = fnodep($odata, "table", "nodes");
+      if (defined($oelems))
       {
-        my $fdata = @$oelems[$n]->{"nodes"};
-        $weatherdata->{get_node($fdata, "text", 0)} =
-        [
-          0,
-          parse_timestamp(get_node($fdata, "text", 1), $time_base),
-          get_node($fdata, "text", 2),
-          get_node($fdata, "text", 3),
-          translate_th_rain(get_node($fdata, "text", 4)),
-          "",
-          get_node($fdata, "text", 5),
-        ];
+        for (my $n = 1; $n < scalar(@$oelems); $n++)
+        {
+          my $fdata = @$oelems[$n]->{"nodes"};
+          $weatherdata->{get_node($fdata, "text", 0)} =
+          [
+            0,
+            parse_timestamp(get_node($fdata, "text", 1), $time_base),
+            get_node($fdata, "text", 2),
+            get_node($fdata, "text", 3),
+            translate_th_rain(get_node($fdata, "text", 4)),
+            "",
+            get_node($fdata, "text", 5),
+          ];
+        }
       }
     }
   }
@@ -467,9 +485,9 @@
       
       # This is horrible :S
       my $fcrap = $fdata->{"om:featureOfInterest"}{"sams:SF_SpatialSamplingFeature"}{"sams:shape"}{"gml:MultiPoint"}{"gml:pointMember"};
-      foreach my $fnode (@{$fcrap})
+      foreach my $xnode (@{$fcrap})
       {
-        my $floc = $fnode->{"gml:Point"};
+        my $floc = $xnode->{"gml:Point"};
         if ($floc->{"gml:pos"} =~ /^\s*([\+\-]?\d+\.\d*)\s+([\+\-]?\d+\.\d*)\s*$/)
         {
           my ($flat, $flong) = ($1, $2);
@@ -481,11 +499,11 @@
               $weatherdata->{$floc->{"gml:name"}} =
               [
                 1,
-                $flol->{"time"},
-                $flol->{"temp"},
+                plonk_data($flol->{"time"}),
+                plonk_data($flol->{"temp"}),
                 "",
                 "",
-                defined($flol->{"humidity"}) ? $flol->{"humidity"} : "",
+                plonk_data($flol->{"humidity"}),
               ];
             }
           }