PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DDG/Spice/Forecast.pm

https://github.com/robertbrook/zeroclickinfo-spice
Perl | 77 lines | 59 code | 12 blank | 6 comment | 4 complexity | 09f27e501cb1ee490710b72bd126d52a MD5 | raw file
  1. package DDG::Spice::Forecast;
  2. use Data::Dumper;
  3. use DDG::Spice;
  4. use Text::Trim;
  5. name "Forecast";
  6. description "Weather forecast";
  7. source "Forecast";
  8. primary_example_queries "weather";
  9. secondary_example_queries "weather 12180";
  10. topics "everyday", "travel";
  11. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Forecast.pm";
  12. my @triggers = ('forecast', 'forcast', 'weather', 'temp', 'temperature', 'meteo');
  13. triggers startend => @triggers;
  14. spice from => '([^/]*)/?([^/]*)';
  15. spice to => 'http://forecast.io/ddg?apikey={{ENV{DDG_SPICE_FORECAST_APIKEY}}}&q=$1&callback={{callback}}';
  16. # cache DDG Rewrite for 24 hours and
  17. # API responses with return code 200 for 30 minutes
  18. spice is_cached => 1;
  19. spice proxy_cache_valid => "200 30m";
  20. my $no_location_qr = qr/fore?cast|report|meteo|weather|temp(?:erature)/;
  21. my $weather_qr = qr/(?:(?:weather|temp(?:erature|)|fore?cast|meteo)(?: fore?cast| report| today| tomm?orr?ow| this week| monday| tuesday| wednesday| thursday| friday| saturday| sunday| lundi| mardi| mercredi| jeudi| vendredi| samedi| dimanche| demain|))+/;
  22. handle query_lc => sub {
  23. my $location = '';
  24. # Capture user defined location if it exists.
  25. if (/^(?:what(?:'s| is) the |)(?:(?:current|local) )?$weather_qr(?: in | for | at |)(.*)/) {
  26. $location = $1 unless ($1 =~ $no_location_qr);
  27. } elsif (/^(.*?)(?:(?:current|local) )?$weather_qr/) {
  28. $location = $1 unless ($1 =~ $no_location_qr);
  29. }
  30. # 10/29/2013 russell double check for things we don't want
  31. $location = trim $location if $location;
  32. # bbc
  33. # shipping forecast, bbc forecast, bbc weather forecast etc.
  34. return if /(shipping\s+fore?cast)|((weather|fore?cast)\sbbc$)|(^bbc\s.*(weather|fore?cast))|(\s+bbc\s+)/;
  35. # has quotes
  36. return if /(%22)|\"/;
  37. # has financialish terms
  38. return if /financ(e|ial)|market|bond|treasury|pension|fund|t-?bill|stock|government|strateg(y|ies)|analytics|market|fore?cast(ing|or|er)/;
  39. return if /(gold|silver|oil|naturalgas|palladium|platinum|copper|lead|zinc|tin|aluminium|aluminum|nickel|cobalt|molybdenum|polypropylene|ethanol).*(fore?cast)/;
  40. # sports
  41. return if /football|golf|soccer|tennis|basketball|hockey|nba|ncaa|nfl|nhl/;
  42. # has other terms
  43. return if (/(^site\:)|http|(\.(org|com|net))|underground/);
  44. # color temperature
  45. if (/temp(era?ture)?/) {
  46. return if /\bcolou?r\b|[0-9]+\s*[kK]/;
  47. }
  48. # Don't cache generic queries due to
  49. # variations in the users location.
  50. if ($location) {
  51. return $location;
  52. } else {
  53. $location = $loc->loc_str;
  54. return $location, 'current', {is_cached => 0};
  55. }
  56. };
  57. 1;