PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Snow.pm

https://github.com/dakshil/zeroclickinfo-spice
Perl | 74 lines | 58 code | 12 blank | 4 comment | 8 complexity | 08e8662aa7828cce86a27329ee12f1bb MD5 | raw file
  1. package DDG::Spice::Snow;
  2. use DDG::Spice;
  3. primary_example_queries "is it snowing?";
  4. secondary_example_queries "is it snowing in New York City?";
  5. description "Check weather conditions at your location";
  6. name "Snow";
  7. icon_url "/icon16.png";
  8. source "Is it snowing yet?";
  9. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Snow.pm";
  10. topics "everyday";
  11. category "facts";
  12. attribution web => [ 'https://www.duckduckgo.com', 'DuckDuckGo' ],
  13. github => [ 'https://github.com/duckduckgo', 'duckduckgo'],
  14. twitter => ['http://twitter.com/duckduckgo', 'duckduckgo'];
  15. spice to => 'http://isitsnowingyet.org/api/check/$1/key/{{ENV{DDG_SPICE_SNOW_APIKEY}}}';
  16. triggers any => "snow", "snowing";
  17. spice proxy_cache_valid => '418 1d';
  18. my %snow = map { $_ => undef } (
  19. 'is it going to snow',
  20. 'going to snow',
  21. 'going to snow today',
  22. 'is it snowing',
  23. 'is it snowing here',
  24. 'is it snowing now',
  25. 'is it going to snow here',
  26. 'is it snowing today',
  27. 'is it going to snow today',
  28. 'going to snow today',
  29. 'is it snowing yet'
  30. );
  31. handle query_lc => sub {
  32. my $query = $_;
  33. # It's possible that $loc or some of its attributes will be null. If we
  34. # don't know where the user is, we can't determine the weather there, so we
  35. # return. This prevents a needless request to isitsnowingyet; we already
  36. # know that we would get an empty response.
  37. return unless $loc && ($loc->city || $loc->region_name) && $loc->country_name;
  38. my @location = ();
  39. if ($loc->city) {
  40. push(@location, $loc->city);
  41. }
  42. if ($loc->region_name) {
  43. push(@location, $loc->region_name);
  44. }
  45. if ($loc->country_name) {
  46. push(@location, $loc->country_name);
  47. }
  48. my $location_str = join(',', @location);
  49. if (exists $snow{$query}) {
  50. return $location_str, {is_cached => 0};
  51. } elsif ($query =~ /^(?:is[ ]it[ ])?
  52. (?:going[ ]to[ ])?
  53. snow(?:ing)?[ ]?
  54. (?:(?:here|now|yet)[ ]?)?
  55. (?:in[ ](.*?))?
  56. (?:[ ]today)?\??$/ix) {
  57. return $1 if $1;
  58. return $location_str, {is_cached => 0};
  59. }
  60. return;
  61. };
  62. 1;