Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 42 Building a Custom Date-Range Search Form

figs/expert.giffigs/hack42.gif

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.

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

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

42.2 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"
);  

42.3 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>
    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
    4.1 Hacks #41-49
    Hack 41 Don't Try This at Home
    Hack 42 Building a Custom Date-Range Search Form
    Hack 43 Building Google Directory URLs
    Hack 44 Scraping Google Results
    Hack 45 Scraping Google AdWords
    Hack 46 Scraping Google Groups
    Hack 47 Scraping Google News
    Hack 48 Scraping Google Catalogs
    Hack 49 Scraping the Google Phonebook
    Chapter 5. Introducing the Google Web API
    Chapter 6. Google Web API Applications
    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