get-nws-alerts.pl Perl script grabbing NWS XML feeds code=yes # tags = #perl - #weather - #xml - #rss showintro=no ######## #!/usr/bin/perl -wT use strict; use Data::Dumper; use HTML::Entities; $|++; BEGIN { unshift @INC, "/home/toledotk/New-TT/lib"; } use XML::TreePP; use Config::Config; my $show_fulton_county_msg = Config::get_value_for("show_fulton_county_msg"); my $fulton_county_msg = Config::get_value_for("fulton_county_msg"); my $show_hazardous_outlook = Config::get_value_for("show_hazardous_outlook"); my $show_mesoscale_discussions = Config::get_value_for("show_mesoscale_discussion"); my $show_radar_link = Config::get_value_for("show_radar_link"); my $radar_gif_url = Config::get_value_for("radar_link"); my $bgcolor = Config::get_value_for("weather_alert_bgcolor"); if ( !$bgcolor ) { $bgcolor = "white"; } # my $tt_alert_link = ">>"; my $tt_alert_link = ">>"; my $radar_link = "weather radar - "; my $fulton_link = "fulton county - "; if ( !$show_fulton_county_msg ) { $fulton_link = ""; } if ( !$show_radar_link ) { $radar_link = ""; } my $have_something = 0; my $final_link = ""; #processing spc mesoscale discussions. if CLE then display at TT my $md_link = ""; if ( $show_mesoscale_discussions ) { my $md_url = "http://www.spc.noaa.gov/products/spcmdrss.xml"; my $mdtpp = XML::TreePP->new(); my $mdtree = $mdtpp->parsehttp( GET => $md_url ); if ( $mdtree ) { # reference to an array of hasshes # print $mdtree->{'rss'}->{'channel'}->{'item'}->[0]->{'link'} . "\n"; my $mdarrref = $mdtree->{'rss'}->{'channel'}->{'item'}; if ( ref $mdarrref eq ref [] ) { foreach my $mditem ( @$mdarrref ) { my $mdlink = $mditem->{'link'}; my $mddesc = $mditem->{'description'}; # if ( $mddesc =~ m/CLE\.\./s ) { if ( $mddesc =~ m/CLE/s or $mddesc =~ m/DTX/s or $mddesc =~ m/IWX/s ) { $md_link .= " mesoscale discussion - "; $have_something = 1; $final_link .= " mesoscale discussion - "; } } } else { my $mdlink = $mdarrref->{'link'}; my $mddesc = $mdarrref->{'description'}; # if ( $mddesc =~ m/CLE\.\./s ) { if ( $mddesc =~ m/CLE/s or $mddesc =~ m/DTX/s or $mddesc =~ m/IWX/s ) { $md_link .= " mesoscale discussion - "; $have_something = 1; $final_link .= " mesoscale discussion - "; } } } } #lucas county zone #### my $xml_url = "http://forecast.weather.gov/MapClick.php?lat=41.61000&lon=-83.8&unit=0&lg=english&FcstType=dwml"; my $xml_url = "http://forecast.weather.gov/MapClick.php?lat=41.66394&lon=-83.55521199999998&unit=0&lg=english&FcstType=dwml"; #fulton county zone for testing on 28Jan2013 # my $xml_url = "http://forecast.weather.gov/MapClick.php?lat=41.56533&lon=-84.17949152542unit=0&lg=english&FcstType=dwml"; my $tpp = XML::TreePP->new(); my $tree = $tpp->parsehttp( GET => $xml_url ); exit unless $tree; # print Dumper($tree); my $alert_msg = ""; # reference to an array of hashes - possibly unless only one hazard message exists and then it's not an array. my $test = $tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'}; if ( ref $test ne ref [] ) { my $hazard_headline_one = lc($tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'}->{'hazard-conditions'}->{'hazard'}->{'-headline'}); my $hazard_url_one = $tree->{'dwml'}->{'data'}->[0]->{'parameters'}->{'hazards'}->{'hazard-conditions'}->{'hazard'}->{'hazardTextURL'}; if ( $hazard_headline_one and $hazard_url_one ) { if ( $hazard_headline_one eq "hazardous weather outlook" && $show_hazardous_outlook eq "1" ) { # print "
$hazard_headline_one - $md_link $fulton_link $radar_link $tt_alert_link
\n"; $have_something = 1; $final_link .= " $hazard_headline_one - \n"; } elsif ( $hazard_headline_one ne "hazardous weather outlook" ) { # print "
$hazard_headline_one - $md_link $fulton_link $radar_link $tt_alert_link
\n"; $final_link .= " $hazard_headline_one - \n"; $have_something = 1; } } } else { my $tmp_ctr = 0; # looping through the reference to the array of hashes foreach my $hz ( @$test ) { # each hz in the loop is a hash my $hazard = lc($hz->{'hazard-conditions'}->{'hazard'}->{'-headline'}); my $hazard_url = $hz->{'hazard-conditions'}->{'hazard'}->{'hazardTextURL'}; if ( $tmp_ctr > 0 ) { $alert_msg .= " - "; } if ( $hazard eq "hazardous weather outlook" && $show_hazardous_outlook eq "1" ) { $alert_msg .= "$hazard"; $final_link .= " $hazard - \n"; $have_something = 1; } elsif ( $hazard ne "hazardous weather outlook" ) { $alert_msg .= "$hazard"; $final_link .= " $hazard - \n"; $have_something = 1; } $tmp_ctr++; } if ( $tmp_ctr > 0 ) { # print "
$alert_msg - $md_link $fulton_link $radar_link $tt_alert_link
\n"; $have_something = 1; } } if ( !$have_something and ( $show_radar_link or $show_fulton_county_msg ) ) { # print "
$fulton_link $radar_link $tt_alert_link
\n"; } if ( $final_link ) { $final_link = "
$final_link $fulton_link $radar_link $tt_alert_link
\n"; print $final_link; }