/usr/share/perl5/XML/RSS/Feed.pm is in libxml-rss-feed-perl 2.212-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 | package XML::RSS::Feed;
use strict;
use warnings;
use XML::RSS;
use XML::RSS::Headline;
use Time::HiRes;
use Storable qw(store retrieve);
=head1 NAME
XML::RSS::Feed - Persistant XML RSS Encapsulation
=head1 VERSION
2.212
=cut
our $VERSION = 2.212;
=head1 SYNOPSIS
A quick and dirty non-POE example that uses a blocking B<sleep>. The
magic is in the B<late_breaking_news> method that returns only
headlines it hasn't seen.
use XML::RSS::Feed;
use LWP::Simple qw(get);
my $feed = XML::RSS::Feed->new(
url => "http://www.jbisbee.com/rdf/",
name => "jbisbee",
delay => 10,
debug => 1,
tmpdir => "/tmp", # optional caching
);
while (1) {
$feed->parse(get($feed->url));
print $_->headline . "\n" for $feed->late_breaking_news;
sleep($feed->delay);
}
ATTENTION! - If you want a non-blocking way to watch multiple RSS sources
with one process use L<POE::Component::RSSAggregator>.
=head1 CONSTRUCTOR
=head2 XML::RSS::Feed->new( url => $url, name => $name )
=over 4
=item B<Required Params>
=over 4
=item * B<name>
Identifier and hash lookup key for the RSS feed.
=item * B<url>
The URL of the RSS feed
=back
=item B<Optional Params>
=over 4
=item * B<delay>
Number of seconds between updates (defaults to 600)
=item * B<tmpdir>
Directory to keep a cached feed (using Storable) to keep persistance between instances.
=item * B<debug>
Turn debuging on.
=item * B<headline_as_id>
Boolean value to use the headline as the id when URL isn't unique within a feed.
=item * B<hlobj>
A class name sublcassed from L<XML::RSS::Headline>
=item * B<max_headlines>
The max number of headlines to keep. (default is unlimited)
=back
=back
=cut
sub new {
my $class = shift;
my $self = bless {
process_count => 0,
rss_headlines => [],
rss_headline_ids => {},
max_headlines => 0,
}, $class;
my %args = @_;
foreach my $method ( keys %args ) {
if ( $self->can($method) ) {
$self->$method( $args{$method} );
}
else {
warn "Invalid argument '$method'";
}
}
$self->_load_cached_headlines if $self->{tmpdir};
$self->delay(3600) unless $self->delay;
return $self;
}
sub _load_cached_headlines {
my ($self) = @_;
my $filename_sto = $self->{tmpdir} . '/' . $self->name . '.sto';
my $filename_xml = $self->{tmpdir} . '/' . $self->name;
if ( -s $filename_sto ) {
my $cached = retrieve($filename_sto);
my $title = $self->title || $cached->{title} || "";
$self->set_last_updated( $cached->{last_updated} );
$self->{process_count}++;
$self->process( $cached->{items}, $title, $cached->{link} );
warn "[$self->{name}] Loaded Cached RSS Storable\n" if $self->{debug};
}
elsif ( -T $filename_xml ) { # legacy XML caching
open( my $fh, $filename_xml );
my $xml = do { local $/, <$fh> };
close $fh;
warn "[$self->{name}] Loaded Cached RSS XML\n" if $self->{debug};
$self->{process_count}++;
$self->parse($xml);
}
else {
warn "[$self->{name}] No Cache File Found\n" if $self->{debug};
}
}
sub _strip_whitespace {
my ($string) = @_;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub _mark_all_headlines_seen {
my ($self) = @_;
return unless $self->{process_count};
$self->{rss_headline_ids}{ $_->id } = 1 for $self->late_breaking_news;
}
=head1 METHODS
=head2 $feed->parse( $xml_string )
Pass in a xml string to parse with XML::RSS and then call
process to process the results.
=cut
sub parse {
my ( $self, $xml ) = @_;
my $rss = XML::RSS->new();
eval { $rss->parse($xml) };
if ($@) {
warn "[$self->{name}] [!!] Failed to parse RSS XML: $@\n";
return 0;
}
else {
warn "[$self->{name}] Parsed RSS XML\n" if $self->{debug};
my $items = [ map { { item => $_ } } @{ $rss->{items} } ];
$self->process(
$items,
( $self->title || $rss->{channel}{title} ),
$rss->{channel}{link}
);
return 1;
}
}
=head2 $feed->process( $items, $title, $link )
=head2 $feed->process( $items, $title )
=head2 $feed->process( $items )
Calls B<pre_process>, B<process_items>, B<post_process>, B<title>, and B<link>
methods to process the parsed results of an RSS XML feed.
=over 4
=item * B<$items>
An array of hash refs which will eventually become L<XML::RSS::Headline> objects. Look
at XML::RSS::Headline->new() for acceptable arguments.
=item * B<$title>
The title of the RSS feed.
=item * B<$link>
The RSS channel link (normally a URL back to the homepage) of the RSS feed.
=back
=cut
sub process {
my ( $self, $items, $title, $link ) = @_;
if ($items) {
$self->pre_process;
$self->process_items($items);
$self->title($title) if $title;
$self->link($link) if $link;
$self->post_process;
return 1;
}
return 0;
}
=head2 $feed->pre_process
Mark all headlines from previous run as seen.
=cut
sub pre_process {
my ($self) = @_;
$self->_mark_all_headlines_seen;
}
=head2 $feed->process_items( $items )
Turn an array refs of hash refs into L<XML::RSS::Headline> objects and
added to the internal list of headlines.
=cut
sub process_items {
my ( $self, $items ) = @_;
if ($items) {
# used 'reverse' so order seen is preserved
for my $item ( reverse @$items ) {
$self->create_headline(%$item);
}
return 1;
}
return 0;
}
=head2 $feed->post_process
Post process cleanup, cache headlines (if tmpdir), and debug messages.
=cut
sub post_process {
my ($self) = @_;
if ( $self->init ) {
warn "[$self->{name}] "
. $self->late_breaking_news
. " New Headlines Found\n"
if $self->{debug};
}
else {
$self->_mark_all_headlines_seen;
$self->init(1);
warn "[$self->{name}] "
. $self->num_headlines
. " Headlines Initialized\n"
if $self->{debug};
}
$self->{process_count}++;
$self->cache;
$self->set_last_updated;
}
=head2 $feed->create_headline( %args)
Create a new L<XML::RSS::Headline> object and add it to the interal list.
Check B<< XML::RSS::Headline->new() >> for acceptable values for B<< %args >>.
=cut
sub create_headline {
my ( $self, %args ) = @_;
my $hlobj = $self->{hlobj} || "XML::RSS::Headline";
$args{headline_as_id} = $self->{headline_as_id};
my $headline = $hlobj->new(%args);
return unless $headline;
unshift( @{ $self->{rss_headlines} }, $headline )
unless $self->seen_headline( $headline->id );
# remove the oldest if the new headline put us over the max_headlines limit
if ( $self->max_headlines ) {
while ( $self->num_headlines > $self->max_headlines ) {
my $garbage = pop @{ $self->{rss_headlines} };
# just in case max_headlines < number of headlines in the feed
$self->{rss_headline_ids}{ $garbage->id } = 1;
warn "[$self->{name}] Exceeded maximum headlines, removing "
. "oldest headline\n"
if $self->{debug};
}
}
}
=head2 $feed->num_headlines
Returns the number of headlines for the feed.
=cut
sub num_headlines {
my ($self) = @_;
return scalar @{ $self->{rss_headlines} };
}
=head2 $feed->seen_headline( $id )
Just a boolean test to see if we've seen a headline or not.
=cut
sub seen_headline {
my ( $self, $id ) = @_;
return 1 if exists $self->{rss_headline_ids}{$id};
return 0;
}
=head2 $feed->headlines
Returns an array or array reference (based on context) of
L<XML::RSS::Headline> objects
=cut
sub headlines {
my ($self) = @_;
return wantarray ? @{ $self->{rss_headlines} } : $self->{rss_headlines};
}
=head2 $feed->late_breaking_news
Returns an array or the number of elements (based on context) of the
B<latest> L<XML::RSS::Headline> objects.
=cut
sub late_breaking_news {
my ($self) = @_;
my @list = grep { !$self->seen_headline( $_->id ); }
@{ $self->{rss_headlines} };
return wantarray ? @list : scalar @list;
}
=head2 $feed->cache
If tmpdir is defined the rss info is cached.
=cut
sub cache {
my ($self) = @_;
return unless $self->tmpdir;
if ( -d $self->tmpdir && $self->num_headlines ) {
my $tmp_filename = $self->tmpdir . '/' . $self->{name} . ".sto";
eval { store( $self->_build_dump_structure, $tmp_filename ) };
if ($@) {
warn "[$self->{name}] Could not cache RSS XML to $tmp_filename\n";
return;
}
else {
warn "[$self->{name}] Cached RSS Storable to $tmp_filename\n"
if $self->{debug};
return 1;
}
}
return;
}
sub _build_dump_structure {
my ($self) = @_;
my $cached = {};
$cached->{title} = $self->title;
$cached->{link} = $self->link;
$cached->{last_updated} = $self->{timestamp_hires};
$cached->{items} = [];
for my $headline ( $self->headlines ) {
push @{ $cached->{items} },
{
headline => $headline->headline,
url => $headline->url,
description => $headline->description,
first_seen => $headline->first_seen_hires,
};
}
return $cached;
}
=head2 $feed->set_last_updated
=head2 $feed->set_last_updated( Time::HiRes::time )
Set the time of when the feed was last processed. If you pass in a value
it will be used otherwise calls Time::HiRes::time.
=cut
sub set_last_updated {
my ( $self, $hires_time ) = @_;
$self->{hires_timestamp} = $hires_time if $hires_time;
$self->{hires_timestamp} = Time::HiRes::time()
unless $self->{hires_timestamp};
}
=head2 $feed->last_updated
The time (in epoch seconds) of when the feed was last processed.
=cut
sub last_updated {
my ($self) = @_;
return int $self->{hires_timestamp};
}
=head2 $feed->last_updated_hires
The time (in epoch seconds and milliseconds) of when the feed was last
processed.
=cut
sub last_updated_hires {
my ($self) = @_;
return $self->{hires_timestamp};
}
=head1 SET/GET ACCESSOR METHODS
=head2 $feed->title
=head2 $feed->title( $title )
The title of the RSS feed.
=cut
sub title {
my ( $self, $title ) = @_;
if ($title) {
$title = _strip_whitespace($title);
$self->{title} = $title if $title;
}
$self->{title};
}
=head2 $feed->debug
=head2 $feed->debug( $bool )
Turn on debugging messages
=cut
sub debug {
my $self = shift @_;
$self->{debug} = shift if @_;
$self->{debug};
}
=head2 $feed->init
=head2 $feed->init( $bool )
init is used so that we just load the current headlines and don't return all
headlines. in other words we initialize them. Takes a boolean argument.
=cut
sub init {
my $self = shift @_;
$self->{init} = shift if @_;
$self->{init};
}
=head2 $feed->name
=head2 $feed->name( $name )
The identifier of an RSS feed.
=cut
sub name {
my $self = shift;
$self->{name} = shift if @_;
$self->{name};
}
=head2 $feed->delay
=head2 $feed->delay( $seconds )
Number of seconds between updates.
=cut
sub delay {
my $self = shift @_;
$self->{delay} = shift if @_;
$self->{delay};
}
=head2 $feed->link
=head2 $feed->link( $rss_channel_url )
The url in the RSS feed with a link back to the site where the RSS feed
came from.
=cut
sub link {
my $self = shift @_;
$self->{link} = shift if @_;
$self->{link};
}
=head2 $feed->url
=head2 $feed->url( $url )
The url in the RSS feed with a link back to the site where the RSS feed
came from.
=cut
sub url {
my $self = shift @_;
$self->{url} = shift if @_;
$self->{url};
}
=head2 $feed->headline_as_id
=head2 $feed->headline_as_id( $bool )
Within some RSS feeds the URL may not always be unique, in these cases
you can use the headline as the unique id. The id is used to check whether
or not a feed is new or has already been seen.
=cut
sub headline_as_id {
my ( $self, $bool ) = @_;
if ( defined $bool ) {
$self->{headline_as_id} = $bool;
$_->headline_as_id($bool) for $self->headlines;
}
$self->{headline_as_id};
}
=head2 $feed->hlobj
=head2 $feed->hlobj( $class )
Ablity to use a subclass of L<XML::RSS::Headline>. (See Perl Jobs example in
L<XML::RSS::Headline::PerlJobs>). This should just be the name of the subclass.
=cut
sub hlobj {
my ( $self, $hlobj ) = @_;
$self->{hlobj} = $hlobj if defined $hlobj;
$self->{hlobj};
}
=head2 $feed->tmpdir
=head2 $feed->tmpdir( $tmpdir )
Temporay directory to store cached RSS XML between instances for persistance.
=cut
sub tmpdir {
my $self = shift @_;
$self->{tmpdir} = shift if @_;
$self->{tmpdir};
}
=head2 $feed->max_headlines
=head2 $feed->max_headlines( $integer )
The maximum number of headlines you'd like to keep track of.
(0 means infinate)
=cut
sub max_headlines {
my $self = shift @_;
$self->{max_headlines} = shift if @_;
$self->{max_headlines};
}
=head1 DEPRECATED METHODS
=head2 $feed->failed_to_fetch
This should was deprecated because, the object shouldn't really know
anything about fetching, it just processes the results. This method
currently will always return false
=cut
sub failed_to_fetch {
warn __PACKAGE__ . "::failed_to_fetch has been deprecated";
return;
}
=head2 $feed->failed_to_parse
This method was deprecated because, $feed->parse now returns a bool value.
This method will always return false
=cut
sub failed_to_parse {
warn __PACKAGE__ . "::failed_to_parse has been deprecated";
return;
}
=head1 AUTHOR
Jeff Bisbee, C<< <jbisbee at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-xml-rss-feed at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-RSS-Feed>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc XML::RSS::Feed
You can also look for information at:
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/XML-RSS-Feed>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/XML-RSS-Feed>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-RSS-Feed>
=item * Search CPAN
L<http://search.cpan.org/dist/XML-RSS-Feed>
=back
=head1 ACKNOWLEDGEMENTS
Special thanks to Rocco Caputo, Martijn van Beers, Sean Burke, Prakash Kailasa
and Randal Schwartz for their help, guidance, patience, and bug reports. Guys
thanks for actually taking time to use the code and give good, honest feedback.
=head1 COPYRIGHT & LICENSE
Copyright 2006 Jeff Bisbee, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SEE ALSO
L<XML::RSS::Headline>, L<XML::RSS::Headline::PerlJobs>, L<XML::RSS::Headline::Fark>, L<XML::RSS::Headline::UsePerlJournals>, L<POE::Component::RSSAggregator>
=cut
1;
|