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

/lib/DDG/Spice/Recipes.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 38 lines | 26 code | 10 blank | 2 comment | 5 complexity | e00b70353dd9f6b84ee781285043acac MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Recipes;
  2. # ABSTRACT: Search for recipes on yummly.com
  3. use strict;
  4. use DDG::Spice;
  5. use Text::Trim;
  6. # removing line breaks from ingredients.txt file:
  7. my %ingredients = map { trim($_) => 0 } share('ingredients.txt')->slurp;
  8. my @stopwords = ('duck duck hack');
  9. triggers any => ('recipe', 'recipes', keys(%ingredients));
  10. spice to => 'http://api.yummly.com/v1/api/recipes?q=$1&requirePictures=true&maxResult=35&_app_id={{ENV{DDG_SPICE_YUMMLY_APPID}}}&_app_key={{ENV{DDG_SPICE_YUMMLY_APIKEY}}}&callback={{callback}}';
  11. handle query_lc => sub {
  12. return if grep {$req->query_lc eq $_} @stopwords;
  13. my $ingredient_count = 0;
  14. if ($_ =~ s/ ?recipes? ?//g) {
  15. $ingredient_count++;
  16. if ($_ eq '') {
  17. return " ";
  18. }
  19. }
  20. my $non_ingredient_count = 0;
  21. my @words = split(/\s/, $_);
  22. foreach my $word (@words) {
  23. exists $ingredients{$word} ? $ingredient_count++ : $non_ingredient_count++;
  24. };
  25. return $_ if $ingredient_count > 1 && $non_ingredient_count < $ingredient_count;
  26. return;
  27. };
  28. 1;