PageRenderTime 64ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Snow.pm

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