#!/usr/bin/perl -w # GoogleSearch.pl # 11apr2002 - matt@interconnected.org http://interconnected.org/home/ # Demonstrates use of the doGoogleSearch method on the Google API. # See http://www.google.com/apis/ to get your key and download the WSDL # (which this script expects to find in its directory). # 2002.04.18 pudge@pobox.com # modified for use with MacPerl, will automatically open # created web page in browser. yay! now Template required. # easy to modify for other systems, just remove Mac::InternetConfig # module and replace GetURL() call. # also added xml_escape() call! use strict; use File::Temp qw(tempfile); use Mac::InternetConfig; use SOAP::Lite; use Template; use XML::Parser; use URI::file; # Configuration my $browser = 1; # print to browser, else print out text # store in var so we can recall values later my %config = ( key => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx', start => 0, max => 6, filter => "true", # boolean restrict => "", safeSearch => "false", # voolean lr => "", ie => "latin1", oe => "latin1", wdsl => 'file:/Bird/src/googleapi/GoogleSearch.wsdl', imgurl => 'file:///Bird/src/googleapi/powered_by_google_135x35.gif', ); # either type on the command line, or it defaults to 'google api' my $query = $ARGV[0] || MacPerl::Ask('Search terms:'); exit() unless defined $query; $config{query} = XML::Parser::Expat->xml_escape($query); my $result = SOAP::Lite->service($config{wdsl})->doGoogleSearch(@config{qw[ key query start max filter restrict safeSearch lr ie oe ]}); # $result is hash of the return structure. Each result is an element in the # array keyed by 'resultElements'. See the WSDL for more details. if (defined $result->{resultElements}) { if ($browser) { my $text; my $tt = Template->new or die; my $template = do { local $/; }; $tt->process(\$template, { result => $result, query => $query, config => \%config, }, \$text) || die $tt->error; my($fh, $filename) = tempfile(); print $fh $text; GetURL(URI::file->new($filename)); } else { print "Found:\n"; for my $elem (@{ $result->{resultElements} }) { my($title, $url) = @{$elem}{'title', 'URL'}; $title =~ s/<\?B>//gi; print "$title\n$url\n\n"; } } } __END__ Google Results: [% query | html %]

Google Search Results: [% query | html %]

    [% FOREACH elem = result.resultElements -%]
  1. [% elem.title %]
    [% elem.snippet %]
    [% elem.URL %] ([% elem.cachedSize %])

  2. [% END -%]

Search time: [% result.searchTime %] seconds / Continue search / Redo search

Powered By Google