/usr/share/perl5/XML/RSS/Headline.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 | package XML::RSS::Headline;
use strict;
use warnings;
use Digest::MD5 qw(md5_base64);
use Encode qw(encode_utf8);
use URI;
use Time::HiRes;
use HTML::Entities;
# length of headline when from description
use constant DESCRIPTION_HEADLINE => 45;
=head1 NAME
XML::RSS::Headline - Persistant XML RSS Encapsulation
=head1 VERSION
2.2
=cut
our $VERSION = 2.2;
=head1 SYNOPSIS
Headline object to encapsulate the headline/URL combination of a RSS feed.
It provides a unique id either by way of the URL or by doing an MD5
checksum on the headline (when URL uniqueness fails).
=head1 CONSTRUCTOR
=head2 XML::RSS::Headline->new( headline =E<gt> $headline, url =E<gt> $url )
=head2 XML::RSS::Headline->new( item =E<gt> $item )
A XML::RSS::Headline object can be initialized either with headline/url or
with a parse XML::RSS item structure. The argument 'headline_as_id' is
optional and takes a boolean as its value.
=cut
sub new {
my $class = shift @_;
my $self = bless {}, $class;
my %args = @_;
my $first_seen = $args{first_seen};
my $headline_as_id = $args{headline_as_id} || 0;
delete $args{first_seen} if exists $args{first_seen};
delete $args{headline_as_id} if exists $args{headline_as_id};
if ( $args{item} ) {
unless ( ( $args{item}->{title} || $args{item}->{description} )
&& $args{item}->{link} )
{
warn "item must contain either title/link or description/link";
return;
}
}
else {
unless ( $args{url} && ( $args{headline} || $args{description} ) ) {
warn 'Either item, url/headline. or url/description are required';
return;
}
}
$self->headline_as_id($headline_as_id);
for my $method ( keys %args ) {
if ( $self->can($method) ) {
$self->$method( $args{$method} );
}
else {
warn "Invalid argument: '$method'";
}
}
unless ( $self->headline ) {
warn "Failed to set headline";
return;
}
$self->set_first_seen($first_seen);
return $self;
}
=head1 METHODS
=head2 $headline->id
The id is our unique identifier for a headline/url combination. Its how we
can keep track of which headlines we have seen before and which ones are new.
The id is either the URL or a MD5 checksum generated from the headline text
(if B<$headline-E<gt>headline_as_id> is true);
=cut
sub id {
my ($self) = shift @_;
return $self->{_rss_headline_id} if $self->headline_as_id;
return $self->url;
}
sub _cache_id {
my ($self) = @_;
$self->{_rss_headline_id}
= md5_base64( encode_utf8( $self->{safe_headline} ) )
if $self->{safe_headline};
}
=head2 $headline->multiline_headline
This method returns the headline as either an array or array
reference based on context. It splits headline on newline characters
into the array.
=cut
sub multiline_headline {
my ($self) = @_;
my @multiline_headline = split /\n/, $self->headline;
return wantarray ? @multiline_headline : \@multiline_headline;
}
=head2 $headline->item( $item )
Init the object for a parsed RSS item returned by L<XML::RSS>.
=cut
sub item {
my ( $self, $item ) = @_;
return unless $item;
$self->url( $item->{link} );
$self->headline( $item->{title} );
$self->description( $item->{description} );
}
=head2 $headline->set_first_seen
=head2 $headline->set_first_seen( Time::HiRes::time() )
Set the time of when the headline was first seen. If you pass in a value
it will be used otherwise calls Time::HiRes::time().
=cut
sub set_first_seen {
my ( $self, $hires_time ) = @_;
$self->{hires_timestamp} = $hires_time;
$self->{hires_timestamp} = Time::HiRes::time() unless $hires_time;
return 1;
}
=head2 $headline->first_seen
The time (in epoch seconds) of when the headline was first seen.
=cut
sub first_seen {
my ($self) = @_;
return int $self->{hires_timestamp};
}
=head2 $headline->first_seen_hires
The time (in epoch seconds and milliseconds) of when the headline was
first seen.
=cut
sub first_seen_hires {
my ($self) = @_;
return $self->{hires_timestamp};
}
=head1 GET/SET ACCESSOR METHODS
=head2 $headline->headline
=head2 $headline->headline( $headline )
The rss headline/title. HTML::Entities::decode_entities is used when the
headline is set. (not sure why XML::RSS doesn't do this)
=cut
sub headline {
my ( $self, $headline ) = @_;
if ($headline) {
$self->{headline} = decode_entities $headline;
if ( $self->{headline_as_id} ) {
$self->{safe_headline} = $headline;
$self->_cache_id;
}
}
return $self->{headline};
}
=head2 $headline->url
=head2 $headline->url( $url )
The rss link/url. URI->canonical is called to attempt to normalize the URL
=cut
sub url {
my ( $self, $url ) = @_;
# clean the URL up a bit
$self->{url} = URI->new($url)->canonical if $url;
return $self->{url};
}
=head2 $headline-E<gt>description
=head2 $headline-E<gt>description( $description )
The description of the RSS headline.
=cut
sub description {
my ( $self, $description ) = @_;
if ($description) {
$self->{description} = decode_entities $description;
$self->_description_headline unless $self->headline;
}
return $self->{description};
}
sub _description_headline {
my ($self) = @_;
my $punctuation = qr/[\.\,\?\!\:\;]+/s;
my $description = $self->{description};
$description =~ s/<br *\/*>/\n/g; # turn br into newline
$description =~ s/<.+?>/ /g;
my $headline = ( split $punctuation, $description )[0] || "";
$headline =~ s/^\s+//;
$headline =~ s/\s+$//;
my $build_headline = "";
for my $word ( split /\s+/, $headline ) {
$build_headline .= " " if $build_headline;
$build_headline .= $word;
last if length $build_headline > DESCRIPTION_HEADLINE;
}
return unless $build_headline;
$self->headline( $build_headline .= '...' );
}
=head2 $headline->headline_as_id
=head2 $headline->headline_as_id( $bool )
A bool value that determines whether the URL will be the unique identifier or
the if an MD5 checksum of the RSS title will be used instead. (when the URL
doesn't provide absolute uniqueness or changes within the RSS feed)
This is used in extreme cases when URLs aren't always unique to new healines
(Use Perl Journals) and when URLs change within a RSS feed
(www.debianplanet.org / debianplanet.org / search.cpan.org,search.cpan.org:80)
=cut
sub headline_as_id {
my ( $self, $bool ) = @_;
if ( defined $bool ) {
$self->{headline_as_id} = $bool;
$self->_cache_id;
}
$self->{headline_as_id};
}
=head2 $headline->timestamp
=head2 $headline->timestamp( Time::HiRes::time() )
A high resolution timestamp that is set using Time::HiRes::time() when the
object is created.
=cut
sub timestamp {
my ( $self, $timestamp ) = @_;
$self->{timestamp} = $timestamp if $timestamp;
return $self->{timestamp};
}
=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::Headline
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::Feed>, L<XML::RSS::Headline::PerlJobs>, L<XML::RSS::Headline::Fark>, L<XML::RSS::Headline::UsePerlJournals>, L<POE::Component::RSSAggregator>
=cut
1;
|