Ticket #8356: script-fixes-2.patch

File script-fixes-2.patch, 4.5 KB (added by Alec leamas <gmail: leamas.alec>, 16 years ago)
  • mythweather/scripts/us_nws/nwsxml.pl

    Adds more checks to avoid silent dead due to missing data.
    
    From: Alec Leamas <leamas.alec@gmail.com>
    
    
    ---
    
     mythweather/scripts/us_nws/nwsxml.pl |   74 +++++++++++++++++++++++-----------
     1 files changed, 49 insertions(+), 25 deletions(-)
     mode change 100755 => 100644 mythweather/scripts/us_nws/nwsxml.pl
    
    
    diff --git a/mythweather/scripts/us_nws/nwsxml.pl b/mythweather/scripts/us_nws/nwsxml.pl
    old mode 100755
    new mode 100644
    index 2e9cf82..729debc
    a b my $response = get $base_url . $loc . '.xml';  
    7777die unless defined $response;
    7878
    7979my $xml = XMLin($response);
     80
     81sub has_value
     82{
     83    my $key = pop @_;
     84
     85    return defined $xml->{$key} &&
     86        $xml->{$key} ne "NA" && $xml->{$key} ne "";
     87}
     88
    8089foreach (@types) {
    8190    my $label;
    8291    my $key;
    foreach (@types) {  
    8493    $label = $_;
    8594
    8695    if (/temp$/ || /dewpoint$/ || /heat_index$/ || /windchill$/) {
    87         if (defined(  $xml->{$_ . '_c'})){
     96        if (has_value( $_ . '_c') && has_value( $_ . '_f')) {
    8897            printf( "%s::%s;%s\n",
    8998                    $label, $xml->{$_ . '_c'}, $xml->{$_ . '_f'});
     99        } else {
     100            print "${label}::NA\n";
    90101        }
    91102    }
    92103    elsif (/pressure$/) {
    93104        printf( "%s::;%s\n", $label, $xml->{$_ . '_in'});
    94105    }
    95106    elsif (/wind_speed/) {
    96           printf( "%s::;%s\n", $label, $xml->{'wind_mph'});
     107        if( has_value('wind_mph')) {
     108            printf( "%s::;%s\n", $label, $xml->{'wind_mph'});
     109        } else {
     110            print "wind_speed::NA\n";
     111        }
    97112    } elsif (/wind_gust/) {
    98         if (defined($xml->{'wind_gust_mph'})) {
    99             printf( "%s::;%s\n", $label, $xml->{'wind_gust_mph'});
    100             printf( "wind_spdgst::;%s\n", $xml->{'wind_gust_mph'});
    101 
     113        if (has_value( 'wind_gust_mph')) {
     114             printf( "wind_gust::;%s\n",  $xml->{'wind_gust_mph'});
     115             printf( "wind_spdgst::;%s\n",$xml->{'wind_gust_mph'});
    102116        } else {
    103117            print "wind_gust::NA\n";
    104118            print "wind_spdgst::NA\n"
    105119        }
    106120    } elsif (/visibility/) {
    107         printf( "%s::;%s\n", $label,  $xml->{'visibility_mi'});
     121        if (has_value( 'visibility_mi')) {
     122            printf( "%s::;%s\n", $label,  $xml->{'visibility_mi'});
     123        } else {
     124            print "visibility::NA\n";
     125        }
    108126    } elsif (/weather_icon/) {
    109         $key = 'weather_icon';
    110         $xml->{$key} = 'unknown.png';
    111         local *FH;
    112         open(FH, "icons") or die "Cannot open icons";
    113         while(my $line = <FH>) {
    114             chomp $line;
    115             if ($line =~ /$xml->{'icon_url_name'}::/) {
    116                 $line =~ s/.*:://;
    117                 $xml->{$key} = $line;
    118                 last;
     127        my $icon = 'unknown.png';
     128        if (has_value('icon_url_name')) {
     129            local *FH;
     130            open(FH, "icons") or die "Cannot open icons";
     131            while(my $line = <FH>) {
     132                chomp $line;
     133                if ($line =~ /$xml->{'icon_url_name'}::/) {
     134                    $line =~ s/.*:://;
     135                    $icon = $line;
     136                    last;
     137                }
    119138            }
    120139        }
     140        print "weather_icon::${icon}\n";
    121141    } elsif (/cclocation/) {
    122142         print "${label}::" . $xml->{'location'};
    123143    } elsif (/appt$/) {
    124         if (defined($xml->{windchill_f})) {
    125             printf( "%s::%s;%s\n",
    126                     $label, $xml->{windchill_c}, $xml->{windchill_f});
    127        
    128         } elsif (defined($xml->{appt_f})) {
    129              printf( "%s::%s;%s\n",
    130                      $label, $xml->{appt_c}, $xml->{appt_f});
     144        if (has_value( 'appt_f') && has_value( 'appt_c')) {
     145            printf( "appt::%s;%s\n", $xml->{'appt_c'}, $xml->{'appt_f'});
     146        } elsif (has_value( 'windchill_f') && has_value( 'windchill_c') ) {
     147            printf( "appt::%s;%s\n",
     148                    $xml->{'windchill_c'}, $xml->{'windchill_f'});
     149        } elsif (has_value( 'heat_index_f') && has_value( 'heat_index_c')) {
     150            printf( "appt::%s;%s\n",
     151                    $xml->{'heat_index_c'}, $xml->{'heat_index_f'});
     152        } else {
     153            print "appt::NA\n";
    131154        }
    132155    } elsif (/copyright/) {
    133         print "copyright::$xml->{credit}\n";
     156        my $value = has_value('credit') ? $xml->{credit} : "NA");
     157        print "copyright::${value}\n";
    134158    } else {
    135159        my $value = (defined( $xml->{$_}) ? $xml->{$_} : "NA");
    136160        print( "${label}::${value}\n");