<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># (C) 2003 Dan Lenski &lt;moxfyre@geocities.com&gt;
#

package WWW::Google::News;

use strict;
use CGI::Util qw(escape);
use HTTP::Request::Common;
use LWP::UserAgent;
use XML::RSS;
use POSIX qw(strftime);

1;

sub new
{
    my $class = shift;
    my $self = bless {}, ref $class || $class;

    $self-&gt;{ua} = LWP::UserAgent-&gt;new(timeout =&gt; 10,
                                      agent =&gt; "Elinks (0.3; Unix)");
    $self-&gt;{results} = undef;
    $self-&gt;{url} = undef;
    $self-&gt;{query} = undef;
    $self-&gt;{lastbuilddate} = undef;
    $self;
}

sub results
{
    my $self = shift;
    $self-&gt;{results};
}

sub resultsRSS
{
    my $self = shift;

    my $rss = XML::RSS-&gt;new(version =&gt; '0.91');
    $rss-&gt;channel( title =&gt; "Google News Search: $self-&gt;{query}",
		   link =&gt; $self-&gt;{url},
		   description =&gt; "Google News Search: $self-&gt;{query}",
		   pubDate =&gt; $self-&gt;{lastbuilddate},
		   webMaster =&gt; 'moxfyre@geocities.com' );

    foreach ( @{$self-&gt;{results}} ) {
	$rss-&gt;add_item( title =&gt; $_-&gt;{title},
			link =&gt; $_-&gt;{link},
			description =&gt; $_-&gt;{desc} );
    }

    $rss-&gt;as_string;
}
    
sub search
{
    my $self = shift;
    my ($query, $num) = @_;

    ##############################################
    # Screen-scraping code begins
    
    my $url = "http://news.google.com/news?scoring=d&amp;hl=en&amp;q="
	.  escape($query) . "&amp;num=" . escape($num);
    my $item_re = 
        qr(&lt;a\ href=([^&gt;]+)&gt;(.+?)&lt;/a&gt;&lt;br&gt;
           (?: &lt;font\ size=-1&gt;&lt;font\ color=green&gt;([^&lt;]+)&lt;/font&gt;
               &amp;nbsp;-&amp;nbsp;([^&lt;]+)&lt;/font&gt;&lt;br&gt; )?
           &lt;font\ size=-1&gt;(.*?)&lt;/font&gt;&lt;br&gt;)sx;
    
    $self-&gt;{query} = $query;
    $self-&gt;{url} = $url;
    $self-&gt;{lastbuilddate} = strftime('%a, %e %b %Y %H:%M:%S %Z', gmtime);

    # get Google news page
    my $response = $self-&gt;{ua}-&gt;request(GET($url));
    return undef unless $response-&gt;is_success;
    $response-&gt;content =~ m|&lt;div&gt;(.*)&lt;/div&gt;|s;
    my $content = $1;

    my $results = $self-&gt;{results} = [];
    while ($content =~ /$item_re/g) {
        my ($link, $title, $source, $when, $desc) = ($1,$2,$3,$4,$5);
        foreach ($title, $desc) { s[&lt;.+?&gt;][]g; tr[\n][ ]; s/^\s+//; s/\s+$// }

        push @$results, { link =&gt; $link,
                          title =&gt; $title,
                          source =&gt; $source,
                          when =&gt; $when,
                          desc =&gt; $desc };
    }

    # Screen-scraping code ends
    ##############################################

    return $results;
}
</pre></body></html>