Google, the search engine of fashion these days, is as close to an authority as you can get on the Internet. If it’s not mentioned on Google, the feeling is, it’s not really online. And if it is, conversely, then people will find it. Keeping track of things in Google’s database, therefore, is an important job. Whether you are trying to find things out or monitoring for other people’s discoveries, an RSS feed is the perfect way to do it. Happily, Google provides a nice interface to work with its systems. This makes things very easy.
We start, as ever, with the usual pragmas, CGI
and
XML::RSS
. This time, you also need
SOAP::Lite
and HTML::Entitie
.
use warnings; use strict; use XML::RSS; use CGI qw(:standard); use HTML::Entities ( ); use SOAP::Lite;
We’ll set up the query term, and the Google API key,
from the CGI input. Then, we fire up the
SOAP::Lite
by pointing it at
Google’s WSDL file. WSDL files contain the finer
details of a SOAP interface, telling the script exactly where to send
commands and so on.
my $query = param("q"); my $key = param("k"); my $service = SOAP::Lite -> service('http://api.google.com/GoogleSearch.wsdl'),
Now, run the search using the SOAP interface we just set up; then set up the feed:
my $result = $service -> doGoogleSearch ($key, $query, 0, 10, "false", "", "false","", "latin1", "latin1"); my $rss = new XML::RSS (version => '2.00'), $rss->channel( title => "Google Search for $query", link => "http://www.google.com/search?q=$query", description => "Google search for $query", language => "en", );
Now, it’s just a matter of going through the results
and using them as values for the feed’s
item
s. To make it valid XML, use the
HTML::Entities
module to encode any stray
characters that might need it:
foreach my $element (@{$result->{'resultElements'}}) { $rss->add_item( title => HTML::Entities::encode($element->{'title'}), link => HTML::Entities::encode($element->{'URL'}) ); }
And finally, serve the feed:
print header('application/xml+rss'), $rss->as_string;
#!/usr/bin/perl use warnings; use strict; use XML::RSS; use CGI qw(:standard); use HTML::Entities ( ); use SOAP::Lite; my $query = param("q"); my $key = param("k"); my $service = SOAP::Lite -> service('http://api.google.com/GoogleSearch.wsdl'), my $result = $service -> doGoogleSearch ($key, $query, 0, 10, "false", "", "false", "", "latin1", "latin1"); my $rss = new XML::RSS (version => '2.00'), $rss->channel( title => "Google Search for $query", link => "http://www.google.com/search?q=$query", description => "Google search for $query", language => "en", ); foreach my $element (@{$result->{'resultElements'}}) { $rss->add_item( title => HTML::Entities::encode($element->{'title'}), link => HTML::Entities::encode($element->{'URL'}) ); } print header('application/xml+rss'), $rss->as_string;
18.118.140.108