O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Find Recipes
Let the Google API transform those random ingredients in your fridge into a wonderful dinner
The Code
[Discuss (2) | Link to this hack]

The Code

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:

# 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"
  header( ),
  '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 -> 
      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( );

Save the code as a CGI script ["How to Run the Scripts" in the Preface] named goocook.cgi in your web site's cgi-bin directory.

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.