#!/usr/bin/perl -w

# $Id: gen_rss_cpanratings,v 1.5 2005/04/20 16:33:41 mrodrigu Exp $

use strict;

use XML::RSS::SimpleGen;

my $VERSION=0.02;

my $LIVE=1;

my $url         = "http://cpanratings.perl.org/";
my $local_rss   = "/home/mrodrigu/tmp/cpanratings.rss";
my $webmaster   = 'mirod@xmltwig.com';

my $local_cache = "cpanratings.html";

my $rss = XML::RSS::SimpleGen->new( $url, "CPAN Ratings", "Ratings and Reviews for CPAN");
$rss->language( 'en' );
$rss->webmaster( $webmaster );
$rss->rss_every_other_hour();
$rss->item_limit( 25);
$rss->docs( 'http://xmltwig.com/rss/');

if( $LIVE)
  { $rss->get_url( $url ); }
else
  { open( my $html, '<', $local_cache) or die "$!";
    local undef $/;
    $_=<$html>;
  }

while( m{<h3>\s*
         <a \s+ href="([^"]*)">([^<]*)</a> # $1: module URL, $2: module name
         \s*\(([^\(]*)\)                   # $3: version
         \s*<img.*?alt="(\d) \s stars?">   # $4: rating
         \s*</h3>
         \s*<p>\s*
         (.*?)                             # $5: text (all lines)
         \s*</p>
         \s*<p>
         \s*<a[^>]*>([^<]*)</a>            # $6: author
         .*?                               # date, unused for now
         \s*<br \s* />
         \s*\(<a \s+ href="/([^"]*)"       # $7: url
         }xgs
     )
  { my( $module_url, $module_name, $module_version, $rating, $text, $author, $review_url)= ($1, $2, $3, $4, $5, $6, $7);
    my $url= "http://cpanratings.perl.org/$review_url";
    my $title= "$module_name ($module_version): $rating stars - by $author";
    my $descr= qq{<![CDATA[$text<p><a href="$module_url">$module_name</p>]]>}; 
    $rss->item( $url, $title, \$descr); # pass $descr ref to bypass HTML escaping in XML::RSS::SimpleGen
    unless( $LIVE) { warn "title: '$title' - url: '$url' - descr: '$descr'\n"; }
  }

$rss->save( $local_rss, 5);           # the 5 means that the script will scream if the RSS does not change for 5 days in a row


exit;

__END__

=head1 NAME

  gen_rss_cpanratings - generate an RSS feed for CPAN Ratings

=head1 SYNOPSYS

just run the script from cron twice a day

=head1 REQUIREMENTS

Perl 5, 

XML::RSS::SimpleGen

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHOR

Michel Rodriguez <mirod@xmltwig.com>

