Bot-BasicBot-Pluggable-Module-Weather

 view release on metacpan or  search on metacpan

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

    $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;

    # extract TAF from equally verbose webpage
    $content  =~ m/($site( AMD)* \d+Z .*?)</s;
    my $taf = $1;
    $taf =~ s/\n//gm;
    $taf =~ s/\s+/ /g;
    
    my $taf_highlight_bold = $self->get("taf_highlight_bold");

    
    # Optionally highlight beginnings of parts of the forecast. Some
    # find it useful, some find it obnoxious, so it's configurable. :-)
    $taf =~ s/(FM\d+Z?|TEMPO \d+|BECMG \d+|PROB\d+)/\cB$1\cB/g if $taf_highlight_bold;


    # Sane?
    return "I can't find any forecast for $site." if length($taf) < 10;

    return $taf;
}

#
# greatcircle -- calculate great circle distance and heading between
#
sub greatcircle {
    my ($self, $line) = @_;

    return "That doesn't look right. The 'great-circle' command takes two airport identifiers and returns the great circle distance and heading between them."
            unless $line =~ /^great-?circle\s+((from|between|for)\s+)?(\w+)\s+((and|to)\s)?(\w+)/i;

    # See metar part for explanation of this bit.
    my $orig_apt = _fix_icao($3);
    my $dest_apt = _fix_icao($6);

    return "$3 doesn't look like an ICAO code" unless $orig_apt;
    return "$6 doesn't look like an ICAO code" unless $dest_apt;

    return "To get the great circle distance two airports ask me 'great-circle between <ICAO code> and <ICAO code>'"
        if ($orig_apt eq 'HELP' or $dest_apt eq 'HELP');

    my $content = LWP::Simple::get("http://www8.landings.com/cgi-bin/nph-dist_apt?airport1=$orig_apt&airport2=$dest_apt");

    return "I can't seem to retrieve data from www.landings.com right now." unless $content;

    my $gcd;
    if ($content =~ m/circle: ([.\d]+).*?, ([.\d]+).*?, ([.\d]+).*?heading: ([.\d]+)/s) {
        $gcd = "Great-circle distance: $1 mi, $2 nm, $3 km, heading $4 degrees true";
    } else {
        $content =~ m/(No airport.*?database)/;
        $gcd = $1;
    }

    return $gcd;


}

#
# tsd -- calculate time, speed, distance, given any two
#
sub tsd {
    my ($self, $line) = @_;

    return "To solve time/speed/distance problems, substitute 'x' for " .
        "the unknown value in 'tsd TIME SPEED DISTANCE'. For example, " .
        "'tsd 3 x 200' will solve for the speed in at which you can travel " .
        "200 mi in 3h." if $line =~ /help/i;

    my ($time, $speed, $distance) = ($line =~ /tsd\s+(\S+)\s+(\S+)\s+(\S+)/);

    my $error;
    $error++ unless $time && $speed && $distance;



( run in 0.375 second using v1.01-cache-2.11-cpan-62beec7d96d )