PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Time.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 44 lines | 37 code | 6 blank | 1 comment | 0 complexity | 0cb5449ad70d58b975ca04bdd1da8c95 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Time;
  2. # ABSTRACT: Time zone converter
  3. use strict;
  4. use DDG::Spice;
  5. use Text::Trim;
  6. use YAML::XS 'LoadFile';
  7. spice proxy_cache_valid => "418 1d";
  8. spice to => 'http://api.xmltime.com/timeservice?accesskey={{ENV{DDG_SPICE_TIME_AND_DATE_ACCESSKEY}}}&secretkey={{ENV{DDG_SPICE_TIME_AND_DATE_SECRETKEY}}}&out=js&callback={{callback}}&query=$1&time=1&tz=1&verbosetime=1&version=1';
  9. triggers any => "time", "date", "day", "year", "month";
  10. my $capitals = LoadFile(share('capitals.yml'));
  11. handle query_lc => sub {
  12. my $q = shift;
  13. return unless $q =~ m/^(?<rest>what'?s?|is|the|current|local|\s)*(?:time|date|day|month|year|\s)*(?:is|it|in|of|for|at|\s)*(?<loc>[^\?]*)[\?]*$/;
  14. my $rest = trim $+{rest};
  15. my $q_loc = trim $+{loc};
  16. # if no location is given, current user location is returned
  17. my $tz_string = $loc->time_zone;
  18. $tz_string =~ s/[\/_]/ /g;
  19. my $location = join(', ', ($loc->city, $loc->region_name));
  20. return ($tz_string, 'generic', $location) unless $q_loc;
  21. $q_loc =~ s/,//g;
  22. return unless (my $caps = $capitals->{$q_loc});
  23. # These are internally sorted by population, so assume they want the big one for now.
  24. $q_loc = string_for_search($caps->[0]);
  25. return $q_loc;
  26. };
  27. sub string_for_search {
  28. my $city_hash = shift;
  29. return join(' ', map { lc $city_hash->{$_} } (qw(city country_name)));
  30. }
  31. 1;