Bot-BasicBot-Pluggable-Module-Weather

 view release on metacpan or  search on metacpan

lib/Bot/BasicBot/Pluggable/Module/Aviation.pm  view on Meta::CPAN

package Bot::BasicBot::Pluggable::Module::Aviation;

use strict;
use Bot::BasicBot::Pluggable::Module; 
use base qw(Bot::BasicBot::Pluggable::Module);

use Geo::WeatherNWS;
use LWP::Simple ();
use HTML::Entities;

sub said { 
    my ($self, $mess, $pri) = @_;

    my $line = $mess->{body}; 
    my $who  = $mess->{who};

    return unless ($pri == 2);

    if    ($line =~ /^metar/i)             { return $self->metar($line)       }
    elsif ($line =~ /^taf/i)               { return $self->taf($line)         }
    elsif ($line =~ /^great[-\s]?circle/i) { return $self->greatcircle($line) }
    elsif ($line =~ /^tsd/i)               { return $self->tsd($line)         }
    elsif ($line =~ /^zulutime/i)          { return $self->zulutime($line)    }
    elsif ($line =~ /^airport/i)           { return $self->airport($line)     }
    elsif ($line =~ /^aviation/i)          { return $self->aviation($line)    }

    return;

}

sub help {
    return "My aviation-related functions are metar, taf, great-circle, tsd, zulutime, and airport. For help with any, ask me about '<function name> help";
}

sub _fix_icao {
    my $site = uc(shift);

    # ICAO airport codes *can* contain numbers, despite earlier claims.
    # Americans tend to use old FAA three-letter codes; luckily we can
    # *usually* guess what they mean by prepending a 'K'. The original
    # author, being Canadian apparently, displays similarl impeccable 
    # laziness.    

    $site =~ s/[.?!]$//;
    $site =~ s/\s+$//g;
    return undef
            unless $site =~ /^[\w\d]{3,4}$/;
    $site  = "C" . $site if length($site) == 3 && $site =~ /^Y/;
    $site  = "K" . $site if length($site) == 3;

    return $site;
}


#
# METAR - current weather observation
#
sub metar {
    my ($self, $line) = @_;

    return unless $line  =~ /\s*metar\s+(for\s+)?(.*)/i;
    
    my $site = _fix_icao($2);
    return "'$site' doesn't look like a valid ICAO airport identifier." 
        unless defined $site;

    return "For observations, ask me 'metar <code>'. For information on decoding Aerodrome Weather Observations (METAR ), see http://www.avweb.com/toc/metartaf.html"
        if ($site eq 'HELP');

    my $r = Geo::WeatherNWS->new();

    $r->getreport($site);

    return "Hrmm, I hit a problem - ".$r->{errortext} if $r->{error};

    return $r->{obs};

}

#
# TAF - terminal area (aerodrome) forecast
#
sub taf {
    my ($self, $line) = @_;


    return unless $line  =~ /\s*taf\s+(for\s+)?(.*)/i;

    my $site = _fix_icao($2);
    return "'$site' doesn't look like a valid ICAO airport identifier."
        unless defined $site;    


    return "For a forecast, ask me 'taf <ICAO code>'. For information on decoding Terminal Area Forecasts, see http://www.avweb.com/toc/metartaf.html"
        if ($site eq 'HELP'); 

    # god I hate CPAN some times, this code should be in GEO::Taf
    my $content = LWP::Simple::get("http://weather.noaa.gov/cgi-bin/mgettaf.pl?cccc=$site");
    return "I can't seem to retrieve data from weather.noaa.com right now." 
            unless $content;



( run in 1.358 second using v1.01-cache-2.11-cpan-6b5c3043376 )