Capturing a Moment in Time

screenshot moderate.gifscreenshot tip68.gif

Build a Google box for a particular moment in time.
link

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 [Tip #67] tip - captures a snapshot of newly indexed or reindexed material at a particular point in time.

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:[Tip #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 tip rolling (http://search.cpan.org/search?query=time%3A%3Ajulianday).


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

Running the Tip

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»

The Results

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

% perl timebox.pl "google tips" 5 1
«a  HREF="http://cnpj.nu/0596004478"»Google Tips«/a» «br /»
«a  HREF="http://cnpj.nu/0596004478/shipsort"»Google Tips«/a» «br /»
«a  HREF="http://cnpj.nu/0596004478/amazonca"»Amazon.ca: Google Tips«/a» «br /»
«a  HREF="http://www.oracle.de/catalog/google-tips/"»Google Tips«/a» «br /»
«a  HREF="http://www.oracle.de/catalog/google-tips/author.html"»Google Tips«/a» «br /»

Tiping the Tip

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

Running the Tiped Tip

The tiped 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 /»