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.
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).
#!/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> "; 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 " ", map( { qq{<a href="$_->{URL}">} . ($_->{title} || $_->{URL}) . qq{</a> <br />} } @{$results->{resultElements}} );
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>
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 />
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] ";
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 " ", "$query on this day in $year<p />", map( { qq{<a href="$_->{URL}">} . ($_->{title} || $_->{URL}) . qq{</a> <br />} } @{$results->{resultElements}} );
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 />