Section 10.6. Google to RSS with SOAP


10.6. Google to RSS with SOAP

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.

10.6.1. Walking Through the Code

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 items. 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;

10.6.2. The Entire Listing

#!/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;



    Developing Feeds with RSS and Atom
    Developing Feeds with Rss and Atom
    ISBN: 0596008813
    EAN: 2147483647
    Year: 2003
    Pages: 118

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net