Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 90 Google Bounce

figs/beginner.giffigs/hack90.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.

90.1 The Code

#!/usr/local/bin/perl
# 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/;

print
  header(  ),
  start_html("GoogleBounce"),
  h1("GoogleBounce"),
  start_form(-method=>'GET'),
  '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;
  bounce(param('query'));
}

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 -> 
    doGoogleSearch(
      $google_key, $query, 0, $max_results, 
      "false", "",  "false", "", "latin1", "latin1"
    );

  # Progress Report
  print 
    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 -> 
    doGoogleSearch(
      $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(  );
  exit;
}

Figure 7-4 shows the results.

Figure 7-4. Google Bounce results for perl
figs/gooH_0704.gif

90.2 See Also

    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
    Chapter 5. Introducing the Google Web API
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    7.1 Hacks #86-92
    Hack 86 The No-Result Search (Prank)
    Hack 87 Google Whacking
    Hack 88 GooPoetry
    Hack 89 Creating Google Art
    Hack 90 Google Bounce
    Hack 91 Google Mirror
    Hack 92 Finding Recipes
    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