#!/usr/local/bin/perl # gawsh.cgi # GAWSH: Google API Web Search by Host # Searches Google for a query string, and displays a list of # hosts on which search results were found; user can then # expand each host to see results restricted to that host. # See http://www.staggernation.com/gawsh/readme.html for info. # By Kevin Shay, kevin [AT] staggernation [DOT] com # Version 1.0, 4/24/02 # tell perl where to find SOAP::Lite use lib qw( /home/staggernation/lib/5.6.0/ /home/staggernation/lib/site_perl/5.6.0); use SOAP::Lite; use CGI; use URI::Escape; # this library contains generic code for all the Google API scripts require('ga_lib.pl'); # this library contains code for the DHTML outlining require('ga_outlinelib.pl'); my ($cgi, $docs_path, $filter, $google_key, $html_output, $level, $params, $query, $response, $service, $this_script, $top); # constants $this_script = $ENV{'SERVER_URL'} . $ENV{'SCRIPT_NAME'}; $docs_path = 'http://www.staggernation.com/gawsh'; #$google_key = 'YOUR_KEY_HERE'; $google_key = require('key.pl'); $cgi = new CGI; # put CGI params into hash $params = cgi_params($cgi); print "Content-type: text/html\n\n"; # user can enter their own key $google_key = ($params->{'key'} || $google_key); $top = $params->{'host'} ? 0 : 1; # are we at the top level? # parameter validation and defaults $params->{'start'} ||= 0; # do we have a query? if ($params->{'q'}) { # load the Google service definition $service = SOAP::Lite->service("http://api.google.com/GoogleSearch.wsdl"); $query = $params->{'q'}; # if we're expanding a host, this will be passed; # restrict search by host if ($params->{'host'}) { $query .= " site:$params->{'host'}"; $filter = 0; } else { $filter = 1; } $response = google_search($service, $google_key, $query, $params->{'start'}, 10, $filter); $i = 0; if ($top) { $html_output .= <<"EOH"; Click on a triangle to expand or collapse a host.

Hosts with results for $params->{'q'}:

EOH # text to be prepended to navigation for this level $params->{'count_text'} = 'From pages '; } else { # contents of this div will be grabbed from the hidden frame by the main frame $html_output .= qq{
}; $params->{'level'} = 1; } # generate the heading (result count and navigation) for this listing $html_output .= html_level_nav($params, $response, qw(id level q host key)); if (defined $response->{'resultElements'} && @{$response->{'resultElements'}}) { # don't duplicate hosts my %hosts_seen = (); for $result (@{$response->{'resultElements'}}) { $i++; $html_output .= html_result($params, $result, $i, \%hosts_seen); } } # lower outline levels if (!$top) { $html_output .= '
'; if ($params->{'reload'}) { # user clicked navigation link; tell calling frame to reload topic $html_output .= html_js_reload($params->{'id'}); } else { # user expanded a topic; tell calling frame to expand it $html_output .= html_js_expand($params->{'id'}); } } } else { # no query } if ($top) { # top level; return header, search box, etc. print html_page_header('GAWSH', 'Web Search by Host (GAWSH)', "$docs_path/readme.html"); # insert JS code; pass the frameset page to which we should redirect # if this script was called at the top of a window # (it needs to be framed to work) print html_js_funcs("$docs_path/index.html"); print html_searchbox($params); } else { # subsidiary level: return only the search results print ''; } print $html_output; print ''; ###################### # END MAIN, BEGIN SUBS ###################### sub html_result { # format a search result my ($params, $result, $i, $hosts_seen) = @_; if ($params->{'host'}) { # expanding a host: print full listing for result $result->{'title'} ||= "[$result->{'URL'}]"; my $url_escaped = uri_escape($result->{'URL'}); my $indent = '' . html_spacer(1) . ''; return <<"EOH"; $indent
$result->{'title'}
$result->{'snippet'}
$result->{'URL'} - $result->{'cachedSize'} - Cached
EOH } else { # top level: print only the host name and the link to expand it $result->{'URL'} =~ m!^http://([^/]+)!; my $host = $1; # don't duplicate hosts on this results page return '' if ($hosts_seen->{$host}); $hosts_seen->{$host} = 1; my $q = uri_escape($params->{'q'}); return <<"EOH";
$host
EOH } } sub html_searchbox { # generate search form, restoring whatever values are in params hash my ($params) = @_; my $q = $params->{'q'}; $q =~ s/"/"/g; return <<"EOH";
Find:   
License key (optional)    ${\&key_message()}
EOH }