O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Feel Really Lucky
Take the domain in which the first result of a query appears and do more searching within that domain
The Code
[Discuss (0) | Link to this hack]

The Code

Save the code as goolucky.cgi, a CGI script on your web server ["How to Run the Hacks" in the Preface] or that of your Internet service provider.

# goolucky.cgi
# Gleans the domain from the first (read: top) result returned, allows
# you to overlay another query, and returns the results, and so on...
# goolucky.cgi is called as a CGI with 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;
use CGI qw/:standard/;
# Create a new SOAP instance.
my $google_search = SOAP::Lite->service("file:$google_wdsl");
# If this is the second time around, glean the domain.
my $query_domain = param('domain') ? "inurl:" . param('domain') : '';
my $results = $google_search ->
    $google_key, param('query') . " $query_domain", 0, 10,
    "false", "", "false", "", "latin1", "latin1"
# Set domain to the results of the previous query.
param('domain', $results->{'resultElements'}->[0]->{'URL'});
param('domain', param('domain') =~ m#://(.*?)/#);
  header( ),
  start_html("I'm Feeling VERY Lucky"),
  h1("I'm Feeling VERY Lucky"),
  start_form( ),
  'Query: ', textfield(-name=>'query',
  -default=>'"Grace Hopper"'),
  '   ',
  'Domain: ', textfield(-name=>'domain'),
  '   ',
  submit(-name=>'submit', -value=>'Search'),
  p( ),
foreach (@{$results->{'resultElements'}}) {
  print p(
    b($_->Feel Really Lucky), br( ),
    a({href=>$_->{URL}}, $_->{URL}), br( ),
  end_form( ),
  end_html( );

Replace insert key here with your Google API key.

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.