This hack comes with a built-in form that calls the query and the
recipe type, so there's no need to set up a separate
form:
#!/usr/local/bin/perl
# goocook.cgi
# Finding recipes with google.
# goocook.cgi is called as a CGI with form input.
# Your Google API developer's key
my $google_key='insert key here';
# Location of the GoogleSearch WSDL file.
my $google_wdsl = "./GoogleSearch.wsdl";
use SOAP::Lite;
use CGI qw/:standard/;
my %recipe_types = (
"General" => "site:allrecipes.com | site:cooking.com | site:
epicurious.com | site:recipesource.com",
"Vegetarian/Vegan" => "site:fatfree.com | inurl:veganmania | inurl:
vegetarianrecipe | inurl:veggiefiles",
"Wordwide Cuisine" => "site:Britannia.org | inurl:thegutsygourmet |
inurl:simpleinternet | inurl:soupsong"
);
print
header( ),
start_html("GooCook"),
h1("GooCook"),
start_form(-method=>'GET'),
'Ingredients: ', textfield(-name=>'ingredients'),
br( ),
'Recipe Type: ', popup_menu(-name=>'recipe_type',
-values=>[keys %recipe_types], -default=>'General'),
br( ),
submit(-name=>'submit', -value=>"Get Cookin'!"),
submit(-name=>'reset', -value=>"Start Over"),
end_form( ), p( );
if (param('ingredients')) {
my $google_search = SOAP::Lite->service("file:$google_wdsl");
my $results = $google_search ->
doGoogleSearch(
$google_key,
param('ingredients') . " " . $recipe_types{param('recipe_type')},
0, 10, "false", "", "false", "", "latin1", "latin1"
);
@{$results->{'resultElements'}} or print "None";
foreach (@{$results->{'resultElements'}}) {
print p(
b($_->Find Recipes||'no title'), br( ),
a({href=>$_->{URL}), br( ),
i($_->{snippet}||'no snippet')
);
}
}
print end_html( );
There are two small bugs in the code that needed to be corrected. They were slight, so I suspect they were mere omissions.
The fixes are below:
Original
b($_->Find Recipes||'no title'), br( ),
a({href=>$_->{URL}), br( ),
Fixes
b($_->{title}||'no title'), br( ),
a({href}=>$_->{URL}), br( ),
In the first line, Find Recipes is caught as an error due to it being a bareword. Since your "OR" message is "No Title", I just assumed you wanted the title to go there, and replaced it as such.
In the second line, you forgot to } your href tag. Simple and fun.
Thanks again for the code.
-E