PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Airlines.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 71 lines | 46 code | 15 blank | 10 comment | 4 complexity | 7e49276dd9170b73989fd47fd1e2ec01 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Airlines;
  2. # ABSTRACT: Information about airplane flights
  3. use strict;
  4. use DDG::Spice;
  5. 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}}';
  6. spice from => '(.*)/(.*)/(.*)/(.*)/(.*)/(.*)';
  7. spice proxy_cache_valid => '418 1d';
  8. triggers any => '///***never trigger***///';
  9. # triggers query_lc => qr/^(\d+)\s+(.*?)(?:[ ]air.*?)?$|^(.*?)(?:[ ]air.*?)?\s+(\d+)$/;
  10. # Get the list of airlines and strip out the words.
  11. my %airlines = ();
  12. my @airlines_lines = share('airlines.txt')->slurp;
  13. foreach my $line (@airlines_lines) {
  14. chomp($line);
  15. my @line = split(/,/, $line);
  16. $line[1] =~ s/\s+air.*$//i;
  17. #American (Airlines <- regex removed) => AA
  18. $airlines{lc $line[1]} = $line[0];
  19. #AA => AA
  20. $airlines{lc $line[0]} = $line[0];
  21. }
  22. # Get the list of elements because an airline name can be an element.
  23. my %elements = ();
  24. my @elements_lines = share('symbols.txt')->slurp;
  25. foreach my $line (@elements_lines) {
  26. chomp $line;
  27. my @line = split(/,/, $line);
  28. $elements{$line[0]} = 1;
  29. $elements{$line[1]} = 1;
  30. }
  31. sub checkAirlines {
  32. my ($airline, $flightno, $year, $month, $dayOfMonth, $hour) = @_;
  33. # Check if we found something and if it's not an element.
  34. if($airline && !exists $elements{$airline}) {
  35. return $airline, $flightno, $year, $month, $dayOfMonth, $hour;
  36. } else {
  37. return;
  38. }
  39. }
  40. handle query_lc => sub {
  41. my $query = $_;
  42. # get the current time
  43. my ($second, $minute, $hour, $dayOfMonth,
  44. $month, $year, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime(time);
  45. $month += 1;
  46. $year += 1900;
  47. # 102 AA
  48. if($query =~ /^(\d+)\s*(.*?)(?:[ ]air.*?)?$/) {
  49. return checkAirlines($airlines{$2}, $1, $year, $month, $dayOfMonth, $hour);
  50. # AA 102
  51. } elsif($query =~ /^(.*?)(?:[ ]air.*?)?\s*(\d+)$/) {
  52. return checkAirlines($airlines{$1}, $2, $year, $month, $dayOfMonth, $hour);
  53. }
  54. return;
  55. };
  56. 1;