Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Reborn IA: Drinks #2191

Merged
merged 9 commits into from
Oct 22, 2015
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/DDG/Spice/Drinks.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package DDG::Spice::Drinks;
# ABSTRACT: Drink mixing instructions

use strict;
use DDG::Spice;
use Text::Trim;

spice is_cached => 1;

name "Drinks";
description "Bartending info";
primary_example_queries "how to make a mojito";
secondary_example_queries "what ingredients are being used within gin fizz", "long island cocktail";

code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Drinks.pm";
attribution github => ["https://github.com/mutilator", "mutilator"],
github => ["https://github.com/ozdemirburak", "Burak Özdemir"],
twitter => ["https://twitter.com/ozdemirbur", "Burak Özdemir"],
web => ["http://burakozdemir.co.uk", "Burak Özdemir"];

spice to => 'http://www.thecocktaildb.com/api/json/v1/1/search.php?s=$1';
spice wrap_jsonp_callback => 1;

my %drinks = map { trim($_) => 0 } share('drinks.txt')->slurp;
triggers any => ('cocktail', 'drink', keys(%drinks));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in theory a great idea, but in practice it will result in a lot of over-triggering for dictionary words/phrases that exist in the drinks.txt file. E.G. "abc", "adam", "acid", etc. If the query is just "adam" it's pretty unlikely that they're looking for a drink recipe.

For now, let's stick with the triggers that are more suggestive of the user's intention, like "how to mix", "how to make", "drink", "cocktail", "recipe"


# Handle statement
handle query_lc => sub {
my $query = $_;
my @stop_words = ("a", "an", "are", "being", "cocktail", "drink", "for", "how", "in", "ingredient", "ingredients", "is", "make", "making", "mix", "mixing", "needed", "of", "that", "to", "used", "what", "within");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define this array outside the handle -- no need to re-define it each time the handle executes

my ($rx) = map qr/(?:$_)/, join "|", map qr/\b\Q$_\E\b/, @stop_words;
$query =~ s/$rx//g;
my $drink = trim($query);
return $drink if $drink ne "";
return;
};

1;
46 changes: 46 additions & 0 deletions share/spice/drinks/drinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function (env) {
"use strict";

function getInfoBoxData(item) {
var infoboxData = [{
heading: 'Ingredients:'
}];
for (var i = 1; i <= 15; i++) {
if(item["strIngredient" + i] !== "") {
infoboxData.push({
label: item["strMeasure" + i] + "" + item["strIngredient" + i]
});
}
}
return infoboxData;
}

env.ddg_spice_drinks = function(api_result){

if (!api_result || api_result.error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check if drinks is null:

screen shot 2015-09-28 at 4 57 03 pm

return Spice.failed('drinks');
}

var drink = api_result.drinks[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if drinks has any elements:

screen shot 2015-09-28 at 4 58 32 pm


Spice.add({
id: 'drinks',
data: drink,
name: "Drinks",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change this to Recipes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed this 👍

meta: {
sourceUrl: "http://www.thecocktaildb.com/drink.php?c=" + drink.idDrink,
sourceName: 'TheCocktailDB'
},
normalize: function(item) {
return {
description: item.strInstructions,
title: item.strDrink,
infoboxData: getInfoBoxData(item)
};
},
templates: {
group: 'info'
}
});
};
}(this));