Google Bounce

screenshot beginner.gifscreenshot tip90.gif

You can get random results from Google with a database of words and random numbers. Why not try a Google Bounce?

The Google Bounce accepts a query word from the user and does a search. It pulls a random title word from one of the search results and searches for that word. It does this a random number of times. In the end, it'll list the top 10 results for the final query. There's a filter to try to make sure that common Google "stop words" (e.g., the, is, a) are removed from the query.

The Code

# Version 1.3, 7/29/2002
# googlebounce.cgi
# Bounce around from a user-specified query to a random set 
# of results.
# googlebounce.cgi is called as a CGI with form input use vars qw/$google_key $google_wsdl $max_bounces $current_bounce/;
# Your Google API developer's key
$google_key='insert key here';
# Location of the GoogleSearch WSDL file
$google_wdsl = "./GoogleSearch.wsdl";
use SOAP::Lite;
use LWP::Simple qw/get/;
use CGI qw/:standard/;
 header( ),
 'Query: ', textfield(-name=»'query'),
 '   ',
 submit(-name=»'submit', -value=»'Search'),
 end_form( ), p( );
print "\n"x4;
if (param('query')) {
 $|++; # turn off buffering
 print h3("Progress Report...");
 # Choose a random number of bounces
 $max_bounces = int(rand(5))+2; 
 # Set the counter to bounce number 1
 $current_bounce = 1;
sub bounce {
 my($query) = @_;
 my $new_query;
 # Filter query for stopwords
 my $stopwords_regex = join '|', qw/the and -- - 1 www com of is a/;
 #$query =~ s/$stopwords_regex//gi;
 # Choose a random number of results
 my $max_results = int(rand(9))+2;
 my $google_search = SOAP::Lite-»service("file:$google_wdsl");
 my $results = $google_search -» 
 $google_key, $query, 0, $max_results, 
 "false", "", "false", "", "latin1", "latin1"
 # Progress Report
 join br( )."\n",
 "«p»Bounce $current_bounce of $max_bounces",
 "Searching for:$query",
 "Asking for $max_results results",
 "Got " . scalar @{$results-»{resultElements}} . " results«/p»";
 my $new_query;
 for (my $ii = $#{$results-»{resultElements}}; $ii »= 0; $ii--) {
 $new_query = $results-»{resultElements}-»[$ii]-»{title};
 $new_query =~ s!«.+?»!!g; # drop all HTML tags
 $new_query =~ /\w/ and last;
 # If there's a new query and we're not overbounced, bounce again
 ++$current_bounce «= $max_bounces and 
 $new_query =~ /\w/ and
 $new_result = bounce($new_query) and
 return $new_result;
 # Otherwise, print out the top 10 for the final query
 print h3("Final Results...");
 my $google_search = SOAP::Lite-»service("file:$google_wdsl");
 my $results = $google_search -» 
 $google_key, $query, 0, 10,
 "false", "", "false", "", "latin1", "latin1"
 @{$results-»{'resultElements'}} or print "None";
 foreach (@{$results-»{'resultElements'}}) {
 print p(
 b($_-»{title}||'no title'), br( ),
 a({href=»$_-»{URL}}, $_-»{URL}), br( ),
 i($_-»{snippet}||'no snippet')
 print end_html( );

Figure 7-4 shows the results.

Figure 7-4. Google Bounce results for perl
screenshot google-tips-0704.gif

See Also