Hack 27. Get Random Results (on Purpose)

brilliant finds .Why would any
researcher worth her salt be interested in random pages? While surfing random pages
isn't what one might call a focused search,
you'd be surprised at some of the brilliant finds
that you'd never have come across otherwise.
I've loved random page generators associated with
search engines ever since discovering Random Yahoo! Link
(http://random.yahoo.com/bin/ryl,
although no longer working at the time of this writing). It made me
think that creating such a thing to work with the Google API might
prove interesting, useful even.
2.9.1. 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 replace insert 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";
2.9.2. Running the Hack
This hack runs as a CGI script ["How to Run the
Hacks" in the Preface]. Point your web browser at
goorandom.cgi.
2.9.3. Hacking the Hack
There are a couple of ways to hack this hack.
2.9.3.1 Modifying the modifiers
You'll notice each modifier in the
@modifier array is preceded by a negative (which
means "exclude this"). You can, of
course, add anything you wish, but it's highly
recommended that you keep to the negative theme; including something
like "computers" in the list gives you a
chancea slight chance, but a chance neverthelessof
coming up with no search results at all. The hack randomly excludes
domains; here are a few more possibilities: -intitle:queryword
-inurl:www
-inurl:queryword
-internet
-yahoo
-intitle:the If you want to, you could create modifiers that use
OR (|) instead of negatives,
and then slant them to a particular topic. For example, you could
create an array with a medical slant that looks like this: (medicine | treatment | therapy)
(cancer | chemotherapy | drug)
(symptoms | "side effects")
(medical | research | hospital)
(inurl:edu | inurl:gov ) Using the OR modifier does not guarantee finding a
search result like using a negative does, so don't
narrow your possible results by restricting your search to the
page's title or URL.
2.9.3.2 Adding a touch more randomness
The hack, as it stands, always picks the first result. While
it's already highly unlikely that
you'll ever see the same random page twice, you can
achieve a touch more randomness by choosing a random returned result.
Take a gander at the actual search itself in the
hack's code: my $results = $google_search ->
doGoogleSearch(
$google_key, "$random_number $random_modifier",
0, 1, "false", ", "false", ", "latin1", "latin1"
); You see that 0 at the beginning of the fourth
line? That's the offset: the number of the first
result to return. Change that number to anything between
0 and 999, and
you'll shift the results returned by that
numberassuming, of course, that the number you choose is
smaller than the number of results for the query at hand. For the
sake of just about guaranteeing a result, it's
probably best to stick to numbers between 0 and
10. How about randomizing the offset? Simply alter
the code as follows (changes in bold): ...
# picking a random number, modifier, and offset combination
my $random_number = int( rand(99999) );
my $random_modifier = $modifiers[int( rand( scalar(@modifiers) ) )];
my $random_offset = int( rand(10) );
...
my $results = $google_search ->
doGoogleSearch(
$google_key, "$random_number $random_modifier",
$random_offset, 1, "false", ", "false", ", "latin1", "latin1"
);
...