Ticket #4416: 4416.patch

File 4416.patch, 6.5 KB (added by Joe Ripley <vitaminjoe@…>, 16 years ago)

Checks if certain variables are defined before attempting to access them

  • mythplugins/mythweather/mythweather/scripts/ENVCANParser.pm

     
    1 #!/usr/bin/perl
    2 #
    31# This script parses the HTML of an Environment Canada weather forecast
    42# page as returned from http://www.weatheroffice.gc.ca.
    53#
     
    4644
    4745sub text {
    4846
    49         if (defined $inside{title}) {
     47        if (defined($inside{'title'}) && $inside{'title'}) {
    5048                $_[0] =~ s/\n//sg;
    5149                $_[0] =~ /(.*),\s*(.*)- 5 Day Weather/;
    5250                $results{'cclocation'} = "$1, $2";
     
    5452                $results{'6dlocation'} = "$1, $2";
    5553        }
    5654
    57         if ($inside{h3}) {
     55        if (defined($inside{'h3'}) && $inside{'h3'}) {
    5856                if ($inside{'div-id'} eq "f1") { $results{'date-0'} = $_[0]; }
    5957                if ($inside{'div-id'} eq "f2") { $results{'date-1'} = $_[0]; }
    6058                if ($inside{'div-id'} eq "f3") { $results{'date-2'} = $_[0]; }
     
    6462        }
    6563
    6664        if ($inside{li}) {
    67                 if ($inside{'li-class'} eq "low") {
    68                         if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'low-0'} = $1; }
    69                         if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'low-1'} = $1; }
    70                         if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'low-2'} = $1; }
    71                         if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'low-3'} = $1; }
    72                         if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'low-4'} = $1; }
    73                         if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'low-5'} = $1; }
    74                 }
     65                if (defined($inside{'li-class'})) {
     66            if ($inside{'li-class'} eq "low") {
     67                        if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'low-0'} = $1; }
     68                        if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'low-1'} = $1; }
     69                        if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'low-2'} = $1; }
     70                            if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'low-3'} = $1; }
     71                        if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'low-4'} = $1; }
     72                        if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'low-5'} = $1; }
     73            }
    7574
    76                 if ($inside{'li-class'} eq "high") {
    77                         if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'high-0'} = $1; }
    78                         if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'high-1'} = $1; }
    79                         if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'high-2'} = $1; }
    80                         if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'high-3'} = $1; }
    81                         if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'high-4'} = $1; }
    82                         if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'high-5'} = $1; }
     75                if ($inside{'li-class'} eq "high") {
     76                if ($inside{'div-id'} eq "f1") { $_[0] =~ /\w* (-?\d*)/; $results{'high-0'} = $1; }
     77                if ($inside{'div-id'} eq "f2") { $_[0] =~ /\w* (-?\d*)/; $results{'high-1'} = $1; }
     78                if ($inside{'div-id'} eq "f3") { $_[0] =~ /\w* (-?\d*)/; $results{'high-2'} = $1; }
     79                if ($inside{'div-id'} eq "f4") { $_[0] =~ /\w* (-?\d*)/; $results{'high-3'} = $1; }
     80                if ($inside{'div-id'} eq "f5") { $_[0] =~ /\w* (-?\d*)/; $results{'high-4'} = $1; }
     81                if ($inside{'div-id'} eq "f6") { $_[0] =~ /\w* (-?\d*)/; $results{'high-5'} = $1; }
     82            }
    8383                }
    8484        }
    85                        
    86         if ($inside{div}) {
    87                 if ($inside{'div-class'} eq "citycondition") { $results{'weather'} = $_[0]; }
    8885
    89                 if ($inside{'div-id'} eq "cityobserved") {
    90                         $_[0] =~ /.* (\d*\:\d*.*)/;
    91                         $results{'observation_time'} = "Last updated at $1";
    92                         $results{'updatetime'} = "Last updated at $1";
    93                         $results{'observation_time_rfc822'} = rfc822($1);
    94                 }
    95         }
     86    if (defined($inside{'div'}) && $inside{'div'}) {
     87        if (defined($inside{'div-class'}) && ($inside{'div-class'} eq "citycondition")) {
     88            $results{'weather'} = $_[0];
     89        }
    9690
    97         if ($inside{dt}) {
     91        if (defined($inside{'div-id'}) && ($inside{'div-id'} eq "cityobserved")) {
     92                $_[0] =~ /.* (\d*\:\d*.*)/;
     93                        $results{'observation_time'} = "Last updated at $1";
     94                    $results{'updatetime'} = "Last updated at $1";
     95                $results{'observation_time_rfc822'} = rfc822($1);
     96        }
     97    }
     98
     99        if (defined($inside{'dt'}) && $inside{'dt'}) {
    98100                if ($_[0] =~ /(Temperature)/) { $scratch = 1; return; }
    99101                if ($_[0] =~ /(Pressure)\/ Tendency/) { $scratch = 2; return; }
    100102                if ($_[0] =~ /(Visibility)/) { $scratch = 3; return; }
     
    104106                if ($_[0] =~ /(Wind)/) { $scratch = 7; return; }
    105107        }
    106108
    107         if ($inside{dd}) {
     109        if (defined($inside{'dd'}) && $inside{'dd'}) {
    108110                if ($scratch == 1) { $_[0] =~ /(-?\d*).*/; $results{'temp'} = $1; }
    109111                if ($scratch == 2) { $_[0] =~ /(\d*\.\d+) kPa.*/; $results{'pressure'} = $1 * 10; }
    110112                if ($scratch == 3) { $_[0] =~ /(\d*) km/; $results{'visibility'} = $1; }
     
    132134        my $icon;
    133135
    134136        # Get Icon
    135         if ($inside{'img-src'} =~ /\/weathericons\/(\d*\.gif)/) {
     137        if (defined($inside{'img-src'}) && ($inside{'img-src'} =~ /\/weathericons\/(\d*\.gif)/)) {
    136138                $icon = $1;
    137139                open(FH, "ENVCAN_icons") or die "Cannot open icons";
    138140                while (my $line = <FH>) {
     
    146148        }
    147149                       
    148150        # Current conditions
    149         $results{"weather_icon"} = $icon if ($inside{'img-class'} eq "currentimg");
    150         $results{"icon-0"} = $icon if ($inside{'img-longdesc'} eq "#f1");
    151         $results{"icon-1"} = $icon if ($inside{'img-longdesc'} eq "#f2");
    152         $results{"icon-2"} = $icon if ($inside{'img-longdesc'} eq "#f3");
    153         $results{"icon-3"} = $icon if ($inside{'img-longdesc'} eq "#f4");
    154         $results{"icon-4"} = $icon if ($inside{'img-longdesc'} eq "#f5");
    155         $results{"icon-5"} = $icon if ($inside{'img-longdesc'} eq "#f6");
     151        $results{"weather_icon"} = $icon if (defined($inside{'img-class'}) && ($inside{'img-class'} eq "currentimg"));
     152        $results{"icon-0"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f1"));
     153        $results{"icon-1"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f2"));
     154        $results{"icon-2"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f3"));
     155        $results{"icon-3"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f4"));
     156        $results{"icon-4"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f5"));
     157        $results{"icon-5"} = $icon if (defined($inside{'img-longdesc'}) && ($inside{'img-longdesc'} eq "#f6"));
    156158
    157159        undef ($inside{'img-class'});
    158160        undef ($inside{'img-src'});