PageRenderTime 89ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DDG/Spice/Snow.pm

https://github.com/ehsan/zeroclickinfo-spice
Perl | 76 lines | 60 code | 12 blank | 4 comment | 8 complexity | 2d5a3ba71476c596f526893387a8328a 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. 'make it snow',
  31. 'let it snow'
  32. );
  33. handle query_lc => sub {
  34. my $query = $_;
  35. # It's possible that $loc or some of its attributes will be null. If we
  36. # don't know where the user is, we can't determine the weather there, so we
  37. # return. This prevents a needless request to isitsnowingyet; we already
  38. # know that we would get an empty response.
  39. return unless $loc && ($loc->city || $loc->region_name) && $loc->country_name;
  40. my @location = ();
  41. if ($loc->city) {
  42. push(@location, $loc->city);
  43. }
  44. if ($loc->region_name) {
  45. push(@location, $loc->region_name);
  46. }
  47. if ($loc->country_name) {
  48. push(@location, $loc->country_name);
  49. }
  50. my $location_str = join(',', @location);
  51. if (exists $snow{$query}) {
  52. return $location_str, {is_cached => 0};
  53. } elsif ($query =~ /^(?:is[ ]it[ ])?
  54. (?:going[ ]to[ ])?
  55. snow(?:ing)?[ ]?
  56. (?:(?:here|now|yet)[ ]?)?
  57. (?:in[ ](.*?))?
  58. (?:[ ]today)?\??$/ix) {
  59. return $1 if $1;
  60. return $location_str, {is_cached => 0};
  61. }
  62. return;
  63. };
  64. 1;