The Code
This code searches for a random number between 0
and 99999 (yes, you can search for
0 with Google) in addition to a modifier pulled
from the @modifiers array. To generate the random
page, you don't, strictly speaking, need something
from the modifer array. However, it helps make the page selection
even more random.
With the combination of a number between 0 and
99999 and a modifier from the
@modifiers array, Google will get a list of search
results, and from that list you'll get a
"random" page. You could go higher
with the numbers if you wanted, but I wasn't sure
that this hack would consistently find numbers higher than
99999. (Zip Codes are five digits, so I knew a
five-digit search would find results more often than not.)
Save the code as a CGI script named
goorandom.cgi. The only change you need to make
is to replaceinsert key
here with your Google API key.
#!/usr/local/bin/perl
# goorandom.cgi
# Creates a random Google query and redirects the browser to
# the top/first result.
# goorandom.cgi is called as a CGI without any form input
# 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;
# A list of search modifiers to be randomly chosen amongst for
# inclusion in the query.
my @modifiers = ( "-site:com", "-site:edu", "-site:net",
"-site:org", "-site:uk", "-filetype:pdf", );
# Picking a random number and modifier combination.
my $random_number = int( rand(99999) );
my $random_modifier = $modifiers[int( rand( scalar(@modifiers) ) )];
# Create a new SOAP object.
my $google_search = SOAP::Lite->service("file:$google_wdsl");
# Query Google.
my $results = $google_search ->
doGoogleSearch(
$google_key, "$random_number $random_modifier",
0, 1, "false", "", "false", "", "latin1", "latin1"
);
# redirect the browser to the URL of the top/first result
print "Location: $results->{resultElements}->[0]->{URL}\n\n";