Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 67 Building a Google Box

figs/moderate.giffigs/hack67.gif

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

Most of the applications in this book 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.

67.1 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.

67.2 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.

67.3 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.

67.3.1 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.

67.3.2 Movable Type

Josh Cooper has written a Movable Type hack (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.

67.3.3 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.oreillynet.com/cs/weblog/view/wlg/1283.

67.4 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.

67.5 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}} );

67.6 Running the Hack

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"

67.7 The Results

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

<a href="http://www.oreilly.com/catalog/pperl2/">oreilly.com -- 
Online Catalog:Programming Perl, 2nd Edition</a> <br />
<a href="http://www.oreilly.com/catalog/pperl3/">oreilly.com -- 
Online Catalog:Programming Perl, 3rd Edition</a> <br />
<a href="http://www.oreilly.com/catalog/pperl2/noframes.html">Programming 
Perl, 2nd Edition</a> <br />
<a href="http://www.tuxedo.org/~esr/jargon/html/entry/Camel-Book.html">Camel 
Book</a> <br />
<a href="http://www.cise.ufl.edu/perl/camel.html">The Camel 
Book<a> <br />

67.8 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.

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    Chapter 6. Google Web API Applications
    6.1 Hacks #60-85
    6.2 The Ingenuity of Millions
    6.3 Learning to Code
    6.4 What You'll Find Here
    6.5 Finding More Google API Applications
    6.6 The Possibilities Aren't Endless, but They're Expanding
    Hack 60 Date-Range Searching with a Client-Side Application
    Hack 61 Adding a Little Google to Your Word
    Hack 62 Permuting a Query
    Hack 63 Tracking Result Counts over Time
    Hack 64 Visualizing Google Results
    Hack 65 Meandering Your Google Neighborhood
    Hack 66 Running a Google Popularity Contest
    Hack 67 Building a Google Box
    Hack 68 Capturing a Moment in Time
    Hack 69 Feeling Really Lucky
    Hack 70 Gleaning Phonebook Stats
    Hack 71 Performing Proximity Searches
    Hack 72 Blending the Google and Amazon Web Services
    Hack 73 Getting Random Results (On Purpose)
    Hack 74 Restricting Searches to Top-Level Results
    Hack 75 Searching for Special Characters
    Hack 76 Digging Deeper into Sites
    Hack 77 Summarizing Results by Domain
    Hack 78 Scraping Yahoo! Buzz for a Google Search
    Hack 79 Measuring Google Mindshare
    Hack 80 Comparing Google Results with Those of Other Search Engines
    Hack 81 SafeSearch Certifying URLs
    Hack 82 Syndicating Google Search Results
    Hack 83 Searching Google Topics
    Hack 84 Finding the Largest Page
    Hack 85 Instant Messaging Google
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele