· 8 years ago · Mar 13, 2018, 04:50 PM
1use Geo::WeatherNWS;
2use Weather::Underground;
3use Data::Dumper;
4use Geo::IATA;
5use List::AllUtils qw/first/;
6use Text::Levenshtein qw/fastdistance/;
7
8# Stefan Petrea
9# stefan.petrea at gmail.com
10# perlhobby.googlecode.com
11
12use Text::Levenshtein qw/fastdistance/;
13
14
15sub solve_weather {
16 my $arg = shift;
17 my $g = Geo::IATA->new;
18 my $location =
19 first { defined $_->{icao} }
20 @{$g->location($arg)};
21 my $weather = Weather::Underground->new( place => $location->{icao}, debug => 0 );
22
23
24 my $data = undef;
25 eval{ my $data = $weather->get_weather; };
26 if(!defined($data)) {
27 my $min="999999999";
28 my $match = undef;
29
30 for( @{ $g->location('%') } ) {
31 $dist = fastdistance($_->{location},$arg);
32 if($dist < 10 && $dist < $min) {
33 $min = $dist;
34 $match = $_;
35 $dist == 1 && last;
36 };
37 }
38 $weather = Weather::Underground->new( place => $match->{icao}, debug => 0 );
39 $location = $match;
40 };
41
42 return ($weather,$data,$location->{location});
43}