< Day Day Up > |
Hack 48. Build a Custom Date Range Search FormSearch only Google pages indexed today, yesterday, the last 7 days, or last 30 days. Google has a date-based search [Hack #16] 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 7 days, or the last 30 days. 2.30.1. The FormThe 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, complete with support for special syntax ["Special Syntax" in Chapter 1] and syntax mixing ["Mixing Syntaxes" in Chapter 1]; 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. 2.30.2. The CodeNote 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, so it 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" ); 2.30.3. Running the HackPoint your browser at the location of the form you just created. Enter a query, choose how many days to go back, and click the Search button. You'll be sent on to Google with the appropriate daterange: restriction in tow. 2.30.4. Hacking the HackIf 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> |
< Day Day Up > |