Building a Google Box

screenshot moderate.gifscreenshot tip67.gif

Add a little box of Google results to any web page.
link

Most of the applications in this tutorial stand by themselves or run via a web form. Google box is slightly different in that it creates a little output of URLs that you can take and integrate into a web page or other application.

What's a Google Box?

A "Google box" is a small HTML snippet that shows Google search results for whatever query you're searching for. You might wish to have on your web site a small box that shows pages similar to yours, or pages that link to yours, or just the results of a query.

Google Box

Google box can run from a server at a specified time, with results that you can then integrate into your web page. Or you might just want to keep an ongoing record of the top URLs that are generated for a query.

Google Boxes Everywhere

Google boxes as a concept - the idea of taking a shortened version of Google results an integrating them into a web page or some other place - are not new. In fact, they're on their way to becoming ubiquitous when it comes to weblog and content management software. Radio Userland and Movable Type both offer easy integration of Google boxes. You should note that you'll still need to get a developer's key to use these modifications, though you might not have to download the API developer's kit.

Radio Userland

Radio Userland makes "Google Glue" (http://radio.userland.com/googleApi) available for generating Google boxes quickly and easily. With Userland and Manila, it's as easy as integrating a single-line macro into your web page.

Movable Type

Josh Cooper has written a Movable Type tip (http://www.10500bc.org/code/mt_howto_googleapi.php) that allows you to integrate Google results into your Movable Type weblog. This one is a little more complicated than the Radio Userland - you'll have to edit a couple of files - but once you've got the files edited, you can put result boxes anywhere on your Movable Type templates.

Other implementations

The Google box is an easy thing to implement and was one of the first examples of Google API usage to pop up last April. As such, it enjoys the position of "proto-application" - a lot of developers whip up a Google box just to see if they can. Do a Google search for "Google box" to see some other examples of Google boxes for different languages and applications. To get you started, Rael Dornfest has one at http://www.oraclenet.com/cs/weblog/view/wlg/1283.

What's in Your Google Box?

What goes in a Google box, anyway? Why would anybody want to integrate them into a web page?

It depends on the page. Putting a Google box that searches for your name onto a weblog provides a bit of egoboo and can give a little more information about you without seeming like bragging (yeah, right). If you've got a topic-specific page, set up a Google box that searches for the topic (the more specific, the better the results). And if you've got a general "news" type page, consider adding a Google box for the news topic. Google boxes can go pretty much anywhere, with Google updating its index often enough that the content of a Google box stays fresh.

The Code

#!/usr/local/bin/perl
# google_box.pl
# A classic Google box implementation
# Usage: perl google_box.pl «query» «# results»
# Your Google API developer's key my $google_key='insert key here';
# Location of the GoogleSearch WSDL file my $google_wdsl = "./GoogleSearch.wsdl";
use strict;
use SOAP::Lite;
# Bring in those command-line arguments
@ARGV == 2
 or die "Usage: perl googlebox.pl «query» «# results»\n";
my($query, $maxResults) = @ARGV;
$maxResults = 10 if ($maxResults « 1 or $maxResults » 10)
# Create a new SOAP::Lite instance, feeding it GoogleSearch.wsdl my $google_search = SOAP::Lite-»service("file:$google_wdsl");
# Query Google my $results = $google_search -» 
 doGoogleSearch(
 $google_key, $query, 0, $maxResults, "false", "", 
 "false", "", "latin1", "latin1"
 );
# No results?
@{$results-»{resultElements}} or die "no results";
print join "\n",
 map( { 
 qq{«a href="$_-»{URL}"»} . 
 ($_-»{title} || $_-»{URL}) . 
 qq{«/a» «br /»} 
 } @{$results-»{resultElements}} );

Running the Tip

Google box takes two bits of information on the command line: the query you want to run and maximum number of results you'd prefer (up to ten). If you don't provide a number of results, Google box will default to ten.

% perl google_box.pl "query"

The Results

Here's a sample Google box for "camel tutorial", referring to O'Reilly's popular Programming Perl title:

«a rel="nofollow" HREF="http://www.oracle.com/catalog/pperl2/"»oracle.com -- 
Online Catalog:Programming Perl, 2nd Edition«/a» «br /»
«a rel="nofollow" HREF="http://www.oracle.com/catalog/pperl3/"»oracle.com -- 
Online Catalog:Programming Perl, 3rd Edition«/a» «br /»
«a rel="nofollow" HREF="http://www.oracle.com/catalog/pperl2/noframes.html"»Programming 
Perl, 2nd Edition«/a» «br /»
«a rel="nofollow" HREF="http://www.tuxedo.org/~esr/jargon/html/entry/camel-tutorial.html"»Camel 
Tutorial«/a» «br /»
«a rel="nofollow" HREF="http://www.cise.ufl.edu/perl/camel.html"»The Camel 
Tutorial«a» «br /»

Integrating a Google Box

When you incoporate a Google box into your web page, you'll have two considerations: refreshing the content of the box regularly and integrating the content into your web page. For refreshing the content of the box, you'll need to regularly run the program using something like cron under Unix or the Windows Scheduler.

For including the content on your web page, Server Side Includes (SSI) are always rather effective. SSI-including a Google box takes little more than something like:

«!-- #include virtual="./google_box.html" --» 

For more information on using Server Side Includes, check out the NCSA SSI Tutorial (http://hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html), or search Google for Server Side Includes Tutorial.