< Day Day Up > |
Hack 24. Find RecipesLet the Google API transform those random ingredients in your fridge into a wonderful dinner. Google can help you find news, catalogs, discussions, web pages, and so much more—and it can also help you figure out what to have for dinner tonight! This hack uses the Google API to help you transform those random ingredients in your fridge into a wonderful dinner. Well, you do have to do some of the work. But it all starts with this hack. 2.6.1. The CodeThis 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($_->{title}||'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. 2.6.2. Running the HackThis hack runs as a CGI script, producing a dynamic web page alongside the rest of the pages in your web site. Since just where you place and how you run CGI scripts varies from server to server and ISP to ISP, you're best left to ask your administrator or provider for help. Once the script is in place, call it by pointing your Web browser at goocook.cgi, fill in the ingredients you have on hand, select a recipe type, and hit the "Get Cookin'!" button. 2.6.3. Hacking the HackOf course, the most obvious way to hack this hack is to add new recipe options to it. That involves first finding new recipe sites, and then adding them to the hack. Adding new recipe sites entails finding the domains that you want to search. Use the cooking section of the Google Directory to find recipes, starting here: http://directory.google.com/Top/Home/Cooking/Recipe_Collections/. Next, find what you want and build it into a query supplement like the one in the form, surrounded by parentheses with each item separated by a |. Remember, using the site: syntax means that you'll be searching for an entire domain, so if you find a great recipe site at http://www.geocities.com/reallygreat/food/recipes/, don't use the site: syntax to search it; use the inurl: search instead (inurl:geocities.com/reallygreat/food/recipes). Just remember that an addition like this counts heavily against your 10-word query limit. Let's look at an example. The cookbook section of the Google Directory has a seafood section with several sites. Let's pull out five sites and turn them into a constraint on our query: (site:simplyseafood.com | site:baycooking.com | site:coastangler.com | site: welovefish.com | site:sea-ex.com) Next, test the query constraints live in Google by adding a query (in this case, salmon) and running it as a search: salmon (site:simplyseafood.com | site:baycooking.com | site:coastangler.com | site:welovefish.com | site:sea-ex.com) Run a few different queries with a few different query words (salmon, scallops, whatever) and make sure that you're getting a decent number of results. Once you're confident that you have a good selection of recipes, you'll need to add this new option to the hack: 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" ); Simply add the name you want to call the option (a =>) and the search string. Make sure you add it before the closing parenthesis and semicolon. Your code should look something like the code shown next. 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" "Seafood" => "site:simplyseafood.com | site:baycooking.com | site: coastangler.com | site:welovefish.com | site:sea-ex.com" ); You can add as many search sets to the hack as you want. You may want to add Chinese Cooking, Desserts, Soups, Salads, or any number of other options. —Tara Calishain and Judy Hourihan |
< Day Day Up > |