This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 942
/
Airlines.pm
71 lines (56 loc) · 2.07 KB
/
Airlines.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package DDG::Spice::Airlines;
# ABSTRACT: Information about airplane flights
use strict;
use DDG::Spice;
spice to => 'https://api.flightstats.com/flex/flightstatus/rest/v2/jsonp/flight/status/$1/$2/arr/$3/$4/$5?hourOfDay=$6&utc=true&appId={{ENV{DDG_SPICE_FLIGHTS_API_ID}}}&appKey={{ENV{DDG_SPICE_FLIGHTS_APIKEY}}}&callback={{callback}}';
spice from => '(.*)/(.*)/(.*)/(.*)/(.*)/(.*)';
spice proxy_cache_valid => '418 1d';
triggers any => '///***never trigger***///';
# triggers query_lc => qr/^(\d+)\s+(.*?)(?:[ ]air.*?)?$|^(.*?)(?:[ ]air.*?)?\s+(\d+)$/;
# Get the list of airlines and strip out the words.
my %airlines = ();
my @airlines_lines = share('airlines.txt')->slurp;
foreach my $line (@airlines_lines) {
chomp($line);
my @line = split(/,/, $line);
$line[1] =~ s/\s+air.*$//i;
#American (Airlines <- regex removed) => AA
$airlines{lc $line[1]} = $line[0];
#AA => AA
$airlines{lc $line[0]} = $line[0];
}
# Get the list of elements because an airline name can be an element.
my %elements = ();
my @elements_lines = share('symbols.txt')->slurp;
foreach my $line (@elements_lines) {
chomp $line;
my @line = split(/,/, $line);
$elements{$line[0]} = 1;
$elements{$line[1]} = 1;
}
sub checkAirlines {
my ($airline, $flightno, $year, $month, $dayOfMonth, $hour) = @_;
# Check if we found something and if it's not an element.
if($airline && !exists $elements{$airline}) {
return $airline, $flightno, $year, $month, $dayOfMonth, $hour;
} else {
return;
}
}
handle query_lc => sub {
my $query = $_;
# get the current time
my ($second, $minute, $hour, $dayOfMonth,
$month, $year, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime(time);
$month += 1;
$year += 1900;
# 102 AA
if($query =~ /^(\d+)\s*(.*?)(?:[ ]air.*?)?$/) {
return checkAirlines($airlines{$2}, $1, $year, $month, $dayOfMonth, $hour);
# AA 102
} elsif($query =~ /^(.*?)(?:[ ]air.*?)?\s*(\d+)$/) {
return checkAirlines($airlines{$1}, $2, $year, $month, $dayOfMonth, $hour);
}
return;
};
1;