/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
- package DDG::Spice::Recipes;
- # ABSTRACT: Search for recipes on yummly.com
- use DDG::Spice;
- use Text::Trim;
- primary_example_queries 'tofu ginger recipe';
- description 'Search for recipes';
- name 'Recipes';
- code_url 'https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Recipes.pm';
- # removing line breaks from ingredients.txt file:
- my %ingredients = map { trim($_) => 0 } share('ingredients.txt')->slurp;
- triggers any => ('recipe', 'recipes', keys(%ingredients));
- 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}}';
- handle query_lc => sub {
- if ($_ =~ s/recipes?//g) {
- return trim($_);
- }
- my $ingredient_count = 0;
- my $non_ingredient_count = 0;
- my @words = split(/\s/, $_);
- foreach my $word (@words) {
- exists $ingredients{$word} ? $ingredient_count++ : $non_ingredient_count++;
- };
- return $_ if $ingredient_count > 1 && $non_ingredient_count < $ingredient_count;
- return;
- };
- 1;