Building a Custom Date-Range Search Form

Search only Google pages indexed today, yesterday, the last 7 days, or last 30 days.

Google has a date-based search [Hack #11] but uses Julian dates. Most people can’t convert Gregorian to Julian in their heads. But with a conversion formula and a little Perl scripting, you can have a Google search form that offers to let users search Google pages indexed today, yesterday, the last seven days, or the last 30 days.

The Form

The frontend to the script is a simple HTML form:

<form action="http://path/to/cgi-bin/goofresh.cgi" 
method="get">
Search for:<br />
<input type="text" name="query" size="30" />
<p />
Search for pages indexed how many days back?<br />
<select name="days_back">
<option value="0">Today</option>
<option value="1">Yesterday</option>
<option value="7">Last 7 Days</option>
<option value="30">Last 30 Days</option>
</select> 
<p />
<input type="submit" value="Search">
</form>

The form prompts for two user inputs. The first is a Google query, replete with support for special syntaxes [Section 1.5] and syntax mixing [Hack #8]; after all, we’ll just be passing your query along to Google itself. The second input, a pull-down list, prompts for how many days’ worth of search the form should perform.

Tip

This hack requires an additional module, Time::JulianDay, and won’t run without it (http://search.cpan.org/search?query=Time%3A%3AJulianDay).

The Code

Note that this script just does a couple of date translations in Perl and redirects the browser to Google, altered query in tow. It’s just a regular query as far as Google is concerned and so doesn’t require a developer’s API key.

#!/usr/local/bin/perl
# goofresh.cgi
# searches for recently-Indexed files on google
# usage: goofresh.cgi is called as a CGI with form input,
# redirecting the browser to Google, altered query in tow

use CGI qw/:standard/;
use Time::JulianDay;

# build a URL-escaped query
(my $query = param('query')) =~ s#(\W)#sprintf("%%%02x", ord($1))#ge;

# how many days back?
my $days_back = int param('days_back') || 0;

# what's the current julian date?
my $julian_date = int local_julian_day(time);

# redirect the browser to Google with query in tow
print redirect(
    'http://www.google.com/search?num=100' .
    "&q=$query" .
    "+daterange%3A" . ($julian_date - $days_back) . "-$julian_date"
);

Hacking the Hack

If you don’t like the date ranges hardcoded into the form, make up your own and adjust the form accordingly:

<form action="http://path/to/cgi-bin/goofresh.cgi" 
method="get">
Search for:<br />
<input type="text" name="query" size="30" />
<p />
Search for pages indexed how many days back?<br />
<select name="days_back">
<option value="0">Today</option>
               <option value="30">Around 1 Month</option>
               <option value="60">Around 2 Months</option>
               <option value="90">Around 3 Months</option>
               <option value="365">1 Year</option>
</select>
<p />
<input type="submit" value="Search">
</form>

Or simply let the user specify how many days to go back in a text field:

<form action="http://path/to/cgi-bin/goofresh.cgi" 
method="get">
Search for:<br />
<input type="text" name="query" size="30" />
<p />
Search for pages indexed how many days back?<br />
<input type="text" name="days_back" size="4" 
               maxlength="4" />
<p />
<input type="submit" value="Search">
</form>

Get Google Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.