Hello! Here's my first (very naive) attempt at a Perl module. I'm trying to solve the API w/ Google.com's search. I saw a place-holder on the CPAN list, but no implementation.
You have to have a free key from google to run this (http://www.google.com/apis)... Simply: use Search::Google; my $search = new Search::Google($key); my $response = $search->search($query); my $search_results = $response->{results}; print " ~".$response->{estimated_hits}." results."; foreach (@$search_results) { print $_->{size}; print $_->{snippet}; print $_->{summary}; print $_->{url}; print $_->{title}; print $_->{host}; } How am I doing do far? Is this even worth doing? Thanks! --Jason ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \\\|/// Jason Heddings (( \\ ~ ~ // 303.272.5166 (x75166) C|~~| (/ @ @ /) [EMAIL PROTECTED] `__' ~~oOOo~(_)~oOOo~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package Search::Google; use strict; use SOAP::Lite; sub new { my ($class, $key, %conf) = @_; return undef if (! $key); my $hits = (exists $conf{hits} ? $conf{hits} : 10); my $start = (exists $conf{start} ? $conf{start} : 0); my $filter = (exists $conf{filter} ? $conf{filter} : "false"); my $restrict = (exists $conf{restrict} ? $conf{restrict} : ""); my $safe_search = (exists $conf{safe_search} ? $conf{safe_search} : "false"); my $lr = (exists $conf{lr} ? $conf{lr} : ""); my $ie = (exists $conf{ie} ? $conf{ie} : "latin1"); my $oe = (exists $conf{oe} ? $conf{oe} : "latin1"); my $wsdl = $conf{wsdl}; if (! $wsdl) { foreach (map {"$_/Search/google.wsdl"} @INC) { next if (! -f $_); $wsdl = "file:".$_; last; } } my $self = bless { key => $key, hits => $hits, start => $start, filter => $filter, restrict => $restrict, safe_search => $safe_search, lr => $lr, ie => $ie, oe => $oe, wsdl => $wsdl, }, $class; return $self; } sub search { my ($self, $query) = @_; my $service = new SOAP::Lite->service($self->{wsdl}); my $response = $service->doGoogleSearch($self->{key}, $query, $self->{start}, $self->{hits}, $self->{filter}, $self->{resrtict}, $self->{safe_search}, $self->{lr}, $self->{ie}, $self->{oe}); my $element; my @formatted_elements; my $elements = $response->{resultElements}; foreach (@$elements) { $element->{size} = $_->{cachedSize}; $element->{host} = $_->{hostName}; $element->{snippet} = $_->{snippet}; $element->{summary} = $_->{summary}; $element->{url} = $_->{URL}; $element->{title} = $_->{title}; push @formatted_elements, $element; $element = undef; } my $result; $result->{filtering} = $response->{documentFiltering}; $result->{estimated_hits} = $response->{estimatedTotalResultsCount}; $result->{search_time} = $response->{searchTime}; $result->{results} = \@formatted_elements; return $result; } 1;
<?xml version="1.0"?> <!-- WSDL description of the Google Web APIs. The Google Web APIs are in beta release. All interfaces are subject to change as we refine and extend our APIs. Please see the terms of use for more information. --> <definitions name="urn:GoogleSearch" targetNamespace="urn:GoogleSearch" xmlns:typens="urn:GoogleSearch" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <!-- Types for search - result elements, directory categories --> <types> <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:GoogleSearch"> <xsd:complexType name="GoogleSearchResult"> <xsd:all> <xsd:element name="documentFiltering" type="xsd:boolean"/> <xsd:element name="searchComments" type="xsd:string"/> <xsd:element name="estimatedTotalResultsCount" type="xsd:int"/> <xsd:element name="estimateIsExact" type="xsd:boolean"/> <xsd:element name="resultElements" type="typens:ResultElementArray"/> <xsd:element name="searchQuery" type="xsd:string"/> <xsd:element name="startIndex" type="xsd:int"/> <xsd:element name="endIndex" type="xsd:int"/> <xsd:element name="searchTips" type="xsd:string"/> <xsd:element name="directoryCategories" type="typens:DirectoryCategoryArray"/> <xsd:element name="searchTime" type="xsd:double"/> </xsd:all> </xsd:complexType> <xsd:complexType name="ResultElement"> <xsd:all> <xsd:element name="summary" type="xsd:string"/> <xsd:element name="URL" type="xsd:string"/> <xsd:element name="snippet" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="cachedSize" type="xsd:string"/> <xsd:element name="relatedInformationPresent" type="xsd:boolean"/> <xsd:element name="hostName" type="xsd:string"/> <xsd:element name="directoryCategory" type="typens:DirectoryCategory"/> <xsd:element name="directoryTitle" type="xsd:string"/> </xsd:all> </xsd:complexType> <xsd:complexType name="ResultElementArray"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:ResultElement[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="DirectoryCategoryArray"> <xsd:complexContent> <xsd:restriction base="soapenc:Array"> <xsd:attribute ref="soapenc:arrayType" wsdl:arrayType="typens:DirectoryCategory[]"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="DirectoryCategory"> <xsd:all> <xsd:element name="fullViewableName" type="xsd:string"/> <xsd:element name="specialEncoding" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:schema> </types> <!-- Messages for Google Web APIs - cached page, search, spelling. --> <message name="doGetCachedPage"> <part name="key" type="xsd:string"/> <part name="url" type="xsd:string"/> </message> <message name="doGetCachedPageResponse"> <part name="return" type="xsd:base64Binary"/> </message> <message name="doSpellingSuggestion"> <part name="key" type="xsd:string"/> <part name="phrase" type="xsd:string"/> </message> <message name="doSpellingSuggestionResponse"> <part name="return" type="xsd:string"/> </message> <message name="doGoogleSearch"> <part name="key" type="xsd:string"/> <part name="q" type="xsd:string"/> <part name="start" type="xsd:int"/> <part name="maxResults" type="xsd:int"/> <part name="filter" type="xsd:boolean"/> <part name="restrict" type="xsd:string"/> <part name="safeSearch" type="xsd:boolean"/> <part name="lr" type="xsd:string"/> <part name="ie" type="xsd:string"/> <part name="oe" type="xsd:string"/> </message> <message name="doGoogleSearchResponse"> <part name="return" type="typens:GoogleSearchResult"/> </message> <!-- Port for Google Web APIs, "GoogleSearch" --> <portType name="GoogleSearchPort"> <operation name="doGetCachedPage"> <input message="typens:doGetCachedPage"/> <output message="typens:doGetCachedPageResponse"/> </operation> <operation name="doSpellingSuggestion"> <input message="typens:doSpellingSuggestion"/> <output message="typens:doSpellingSuggestionResponse"/> </operation> <operation name="doGoogleSearch"> <input message="typens:doGoogleSearch"/> <output message="typens:doGoogleSearchResponse"/> </operation> </portType> <!-- Binding for Google Web APIs - RPC, SOAP over HTTP --> <binding name="GoogleSearchBinding" type="typens:GoogleSearchPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="doGetCachedPage"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="doSpellingSuggestion"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="doGoogleSearch"> <soap:operation soapAction="urn:GoogleSearchAction"/> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <!-- Endpoint for Google Web APIs --> <service name="GoogleSearchService"> <port name="GoogleSearchPort" binding="typens:GoogleSearchBinding"> <soap:address location="http://api.google.com/search/beta2"/> </port> </service> </definitions>