Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 68 Capturing a Moment in Time

figs/moderate.giffigs/hack68.gif

Build a Google box for a particular moment in time.

Google boxes are a nice addition to your web pages, whether you run a weblog or a news site. But for many Google box searches, the search results won't change that often, especially for more common search words. The Timely Google box—built upon the ordinary Google box [Hack #67] hack—captures a snapshot of newly indexed or reindexed material at a particular point in time.

68.1 Making the Google Box Timely

As you might remember, Google has a daterange: search syntax available. This version of Google box takes advantage of the daterange:[Hack #11] syntax, allowing you to specifying how many days back you want your query to run. If you don't provide a number, the default is 1, and there's no maximum. I wouldn't go back much further than a month or so. The fewer days back you go the more often the results in the Google box will change.

You'll need the Julian::Day module to get this hack rolling (http://search.cpan.org/search?query=time%3A%3Ajulianday).

68.2 The Code

#!/usr/local/bin/perl
# timebox.pl
# A time-specific Google box
# Usage: perl timebox.pl <query> <# results> <# days back>

# 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;
use Time::JulianDay;

# Bring in those command-line arguments
@ARGV == 3 
  or die "Usage: perl timebox.pl <query> <# results> <# days back>\n";
my($query, $maxResults, $daysBack) = @ARGV;
$maxResults = 10 if ($maxResults < 1 or $maxResults > 10);
$daysBack = 1 if $daysBack <= 0;

# Figure out when yesterday was in Julian days
my $yesterday = int local_julian_day(time) - $daysBack;

# 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 daterange:$yesterday-$yesterday", 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}} );

68.3 Running the Hack

You'll have to provide three items of information on the command line: the query you want to run, maximum number of results you'd prefer (up to 10), and number of days back to travel.

% perl timebox.pl "query" <# of results> <# days back>

68.4 The Results

Here's a sample Google box for the top five "google hacks" results (this book included, hopefully) indexed yesterday:

% perl timebox.pl "google hacks" 5 1
<a href="http://isbn.nu/0596004478">Google Hacks</a> <br />
<a href="http://isbn.nu/0596004478/shipsort">Google Hacks</a> <br />
<a href="http://isbn.nu/0596004478/amazonca">Amazon.ca: Google Hacks</a> <br />
<a href="http://www.oreilly.de/catalog/googlehks/">Google Hacks</a> <br />
<a href="http://www.oreilly.de/catalog/googlehks/author.html">Google Hacks</a> <br />

68.5 Hacking the Hack

Perhaps you'd like your Google box to reflect "this day in 1999." No problem for this slightly tweaked version of the Timely Google box (changes highlighted in bold):

#!/usr/local/bin/perl
# timebox_thisday.pl
# A Google box for this day in <year>
# Usage: perl timebox.pl <query> <# results> [year]

# 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;
use Time::JulianDay;

my @now = localtime(time);

# Bring in those command-line arguments
@ARGV == 2
or die "Usage: perl timebox.pl <query> <# results> [year]\n";
 my($query, $maxResults, $year) = @ARGV;
$maxResults = 10 if ($maxResults < 1 or $maxResults > 10);
$year =~ /^\d{4}$/ or $year = 1999;

# Figure out when this day in the specified year is
my $then = int julian_day($year, $now[4], $now[3]);

# 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 daterange:$then-$then", 0, 
    $maxResults, "false", "",  "false", "", "latin1", "latin1"
  );

# No results?
@{$results->{resultElements}} or die "no results";

print join "\n",
  "$query on this day in $year<p />",
  map( { 
    qq{<a href="$_->{URL}">} . 
    ($_->{title} || $_->{URL}) . 
    qq{</a> <br />} 
  } @{$results->{resultElements}} );
  

68.6 Running the Hacked Hack

The hacked version of Timely Google box runs just like the first version, except that you specify the maximum number of results and a year. Going back further than 1999 doesn't yield particularly useful results given that Google came online in 1998.

Let's take a peek at how Netscape was doing in 1999:

% perl timebox_thisday.pl "netscape" 5 1999
netscape on this day in 1999:<p />
<a href="http://www.showgate.com/aol.html">WINSOCK.DLL and NETSCAPE Info for 
AOL Members</a> <br />
<a href="http://www.univie.ac.at/comment/99-3/993_23.orig.html">Comment 99/3 
- Netscape Communicator</a> <br />
<a href="http://www.ac-nancy-metz.fr/services/docint/netscape.htm">NETSCAPE.
</a> <br />
<a href="http://www.ac-nancy-metz.fr/services/docint/Messeng1.htm">Le 
Courrier électronique avec Netscape Messenger</a> <br />
<a href="http://www.airnews.net/anews_ns.htm">Setting up Netscape 2.0 for 
Airnews Proxy News</a> <br />  
    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