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';
|
| 77 | 77 | die unless defined $response; |
| 78 | 78 | |
| 79 | 79 | my $xml = XMLin($response); |
| | 80 | |
| | 81 | sub has_value |
| | 82 | { |
| | 83 | my $key = pop @_; |
| | 84 | |
| | 85 | return defined $xml->{$key} && |
| | 86 | $xml->{$key} ne "NA" && $xml->{$key} ne ""; |
| | 87 | } |
| | 88 | |
| 80 | 89 | foreach (@types) { |
| 81 | 90 | my $label; |
| 82 | 91 | my $key; |
| … |
… |
foreach (@types) {
|
| 84 | 93 | $label = $_; |
| 85 | 94 | |
| 86 | 95 | if (/temp$/ || /dewpoint$/ || /heat_index$/ || /windchill$/) { |
| 87 | | if (defined( $xml->{$_ . '_c'})){ |
| | 96 | if (has_value( $_ . '_c') && has_value( $_ . '_f')) { |
| 88 | 97 | printf( "%s::%s;%s\n", |
| 89 | 98 | $label, $xml->{$_ . '_c'}, $xml->{$_ . '_f'}); |
| | 99 | } else { |
| | 100 | print "${label}::NA\n"; |
| 90 | 101 | } |
| 91 | 102 | } |
| 92 | 103 | elsif (/pressure$/) { |
| 93 | 104 | printf( "%s::;%s\n", $label, $xml->{$_ . '_in'}); |
| 94 | 105 | } |
| 95 | 106 | 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 | } |
| 97 | 112 | } 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'}); |
| 102 | 116 | } else { |
| 103 | 117 | print "wind_gust::NA\n"; |
| 104 | 118 | print "wind_spdgst::NA\n" |
| 105 | 119 | } |
| 106 | 120 | } 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 | } |
| 108 | 126 | } 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 | } |
| 119 | 138 | } |
| 120 | 139 | } |
| | 140 | print "weather_icon::${icon}\n"; |
| 121 | 141 | } elsif (/cclocation/) { |
| 122 | 142 | print "${label}::" . $xml->{'location'}; |
| 123 | 143 | } 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"; |
| 131 | 154 | } |
| 132 | 155 | } elsif (/copyright/) { |
| 133 | | print "copyright::$xml->{credit}\n"; |
| | 156 | my $value = has_value('credit') ? $xml->{credit} : "NA"); |
| | 157 | print "copyright::${value}\n"; |
| 134 | 158 | } else { |
| 135 | 159 | my $value = (defined( $xml->{$_}) ? $xml->{$_} : "NA"); |
| 136 | 160 | print( "${label}::${value}\n"); |