/lib/DDG/Spice/Recipes.pm

https://github.com/pardocz/zeroclickinfo-spice · Perl · 36 lines · 24 code · 10 blank · 2 comment · 3 complexity · 96c512d60b03fa9dc5e60893dc3ab05e MD5 · raw file

  1. package DDG::Spice::Recipes;
  2. # ABSTRACT: Search for recipes on yummly.com
  3. use DDG::Spice;
  4. use Text::Trim;
  5. primary_example_queries 'tofu ginger recipe';
  6. description 'Search for recipes';
  7. name 'Recipes';
  8. code_url 'https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Recipes.pm';
  9. # removing line breaks from ingredients.txt file:
  10. my %ingredients = map { trim($_) => 0 } share('ingredients.txt')->slurp;
  11. triggers any => ('recipe', 'recipes', keys(%ingredients));
  12. 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}}';
  13. handle query_lc => sub {
  14. if ($_ =~ s/recipes?//g) {
  15. return trim($_);
  16. }
  17. my $ingredient_count = 0;
  18. my $non_ingredient_count = 0;
  19. my @words = split(/\s/, $_);
  20. foreach my $word (@words) {
  21. exists $ingredients{$word} ? $ingredient_count++ : $non_ingredient_count++;
  22. };
  23. return $_ if $ingredient_count > 1 && $non_ingredient_count < $ingredient_count;
  24. return;
  25. };
  26. 1;