/usr/share/perl5/AnyEvent/Feed.pm is in libanyevent-feed-perl 0.3-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 | package AnyEvent::Feed;
use strict;
no warnings;
use Carp qw/croak/;
use Encode;
use XML::Feed;
use MIME::Base64;
use AnyEvent::HTTP;
use Digest::SHA qw/sha1_base64/;
use Scalar::Util qw/weaken/;
our $VERSION = '0.3';
=head1 NAME
AnyEvent::Feed - Receiving RSS/Atom Feed reader with XML::Feed
=head1 VERSION
Version 0.3
=head1 SYNOPSIS
use AnyEvent;
use AnyEvent::Feed;
my $feed_reader =
AnyEvent::Feed->new (
url => 'http://example.com/atom.xml',
);
$feed_reader->fetch (sub {
my ($feed_reader, $new_entries, $feed, $error) = @_;
if (defined $error) {
warn "ERROR: $error\n";
return;
}
# $feed is the XML::Feed object belonging to that fetch.
for (@$new_entries) {
my ($hash, $entry) = @$_;
# $hash a unique hash describing the $entry
# $entry is the XML::Feed::Entry object of the new entries
# since the last fetch.
}
});
# Or:
my $feed_reader =
AnyEvent::Feed->new (
url => 'http://example.com/atom.xml',
interval => $seconds,
on_fetch => sub {
my ($feed_reader, $new_entries, $feed, $error) = @_;
if (defined $error) {
warn "ERROR: $error\n";
return;
}
# see above
}
);
=head1 DESCRIPTION
This module implements some glue between L<AnyEvent::HTTP> and L<XML::Feed>.
It can fetch a RSS/Atom feed on a regular interval as well as on customized
times. It also keeps track of already fetched entries so that you will only get
the new entries.
=head1 METHODS
=over 4
=item $feed_reader = AnyEvent::Feed->new (url => $url, %args)
This is the constructor for a new feed reader for the RSS/Atom feed
reachable by the URL C<$url>. C<%args> may contain additional key/value pairs:
=over 4
=item interval => $seconds
If this is set you also have to specify the C<on_fetch> callback (see below).
It will try to fetch the C<$url> every C<$seconds> seconds and call the
callback given by C<on_fetch> with the result.
=item headers => $http_hdrs
Additional HTTP headers for each GET request can be passed in the C<$http_hdrs>
hash reference, just like you would pass it to the C<headers> argument of
the C<http_get> request of L<AnyEvent::HTTP>.
=item username => $http_user
=item password => $http_pass
These are the HTTP username and password that will be used for Basic HTTP
Authentication with the HTTP server when fetching the feed. This is mostly
sugar for you so you don't have to encode them yourself and pass them to the
C<headers> argument above.
=item on_fetch => $cb->($feed_reader, $new_entries, $feed_obj, $error)
This callback is called if the C<interval> parameter is given (see above)
with the same arguments as the callback given to the C<fetch> method (see below).
=item entry_ages => $hash
This will set the hash which keeps track of seen and old entries.
See also the documentation of the C<entry_ages> method below.
The default will be an empty hash reference.
=item max_entry_age => $count
This will set the maximum number of times an entry is kept in the C<entry_ages>
hash after it has not been seen in the feed anymore. The default value is 2
which means that an entry hash is removed from the C<entry_ages> hash after it
has not been seen in the feed for 2 fetches.
=back
=cut
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = { @_ };
bless $self, $class;
$self->{entry_ages} ||= {};
if (defined $self->{interval}) {
unless (defined $self->{on_fetch}) {
croak "no 'on_fetch' callback given!";
}
my $wself = $self;
weaken $wself;
$self->{timer_cb} = sub {
$wself->fetch (sub {
my ($self, $e, $f, $err) = @_;
$self->{on_fetch}->($self, $e, $f, $err);
$self->{timer} =
AnyEvent->timer (
after => $self->{interval}, cb => $self->{timer_cb});
})
};
$self->{timer_cb}->();
}
return $self
}
sub _entry_to_hash {
my ($entry) = @_;
my $x = sha1_base64
encode 'utf-8',
(my $a = join '/',
$entry->title,
($entry->summary ? $entry->summary->body : ''),
($entry->content ? $entry->content->body : ''),
$entry->id,
$entry->link);
$x
}
sub _new_entries {
my ($self) = @_;
$self->{entry_ages} ||= {};
my (@ents) = $self->{feed}->entries;
my @new;
# 'age' the old entries
$self->{entry_ages}->{$_}++ for keys %{$self->{entry_ages}};
for my $ent (@ents) {
my $hash = _entry_to_hash ($ent);
unless (exists $self->{entry_ages}->{$hash}) {
push @new, [$hash, $ent];
}
$self->{entry_ages}->{$hash} = 0; # reset age of old entry.
}
for (keys %{$self->{entry_ages}}) {
delete $self->{entry_ages}->{$_}
if $self->{entry_ages}->{$_} > $self->{max_entry_ages};
}
\@new
}
=item $feed_reader->url
Just returns the url that this feed reader is fetching from.
=cut
sub url { $_[0]->{url} }
=item $feed_reader->entry_ages ($new_entry_ages)
=item my $entry_ages = $feed_reader->entry_ages
This will set the age hash which will keep track of already seen entries.
The keys of the hash will be the calculated hashes of the entries and the
values will be a counter of how often they have NOT been seen anymore (kind of
an age counter). After each fetch this hash is updated and seen entries get
a value of 0.
=cut
sub entry_ages {
defined $_[1]
? $_[0]->{entry_ages} = $_[1]
: $_[0]->{entry_ages}
}
=item $feed_reader->fetch ($cb->($feed_reader, $new_entries, $feed_obj, $error))
This will initiate a HTTP GET on the URL passed to C<new> and call C<$cb> when
done.
C<$feed_reader> is the feed reader object itself. C<$new_entries> is an
array reference containing the new entries. A new entry in that array is
another array containing a calculated hash over the contents of the new entry,
and the L<XML::Feed::Entry> object of that entry. C<$feed_obj> is the
L<XML::Feed> feed object used to parse the fetched feed and contains all
entries (and not just the 'new' ones).
What a 'new' entry is, is decided by a map of hashes as described in the
C<entry_ages> method's documentation above.
=cut
sub _get_headers {
my ($self, %hdrs) = @_;
my %hdrs = %{$self->{headers} || {}};
if (defined $self->{last_mod}) {
$hdrs{'If-Modified-Since'} = $self->{last_mod};
}
$hdrs{Authorization} =
"Basic " . encode_base64 (join ':', $self->{username}, $self->{password}, '')
if defined $self->{username};
\%hdrs
}
sub fetch {
my ($self, $cb) = @_;
unless (defined $cb) {
croak "no callback given to fetch!";
}
http_get $self->{url}, headers => $self->_get_headers, sub {
my ($data, $hdr) = @_;
#d# warn "HEADERS ($self->{last_mod}): "
#d# . (join ",\n", map { "$_:\t$hdr->{$_}" } keys %$hdr)
#d# . "\n";
if ($hdr->{Status} =~ /^2/) {
my $feed;
eval {
$self->{feed} = XML::Feed->parse (\$data);
};
if ($@) {
$cb->($self, undef, undef, "exception: $@");
} elsif (not defined $self->{feed}) {
$cb->($self, undef, undef, XML::Feed->errstr);
} else {
$cb->($self, $self->_new_entries, $self->{feed});
$self->{last_mod} = $hdr->{'last-modified'};
}
} elsif (defined ($self->{last_mod}) && $hdr->{Status} eq '304') {
# do nothing, everything was/is fine!
$cb->($self, [], $self->{feed});
} else {
$cb->($self, undef, undef, "$hdr->{Status} $hdr->{Reason}");
}
};
}
=back
=head1 AUTHOR
Robin Redeker, C<< <elmex@ta-sa.org> >>
=head1 SEE ALSO
L<XML::Feed>
L<AnyEvent::HTTP>
L<AnyEvent>
=head1 BUGS
=head2 Known Bugs
There is actually a known bug with encodings of contents of Atom feeds.
L<XML::Atom> by default gives you UTF-8 encoded data. You have to set
this global variable to be able to use the L<XML::Feed::Entry> interface
without knowledge of the underlying feed type:
$XML::Atom::ForceUnicode = 1;
I've re-reported this bug against L<XML::Feed>, as I think it should
take care of this. L<XML::Atom> should probably just fix it's Unicode
interface, but it seems to be a bit deserted w.r.t. fixing the bugs in
the tracker.
=head2 Contact
Please report any bugs or feature requests to
C<bug-anyevent-feed at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AnyEvent-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 AnyEvent::Feed
You can also look for information at:
=over 4
=item * IRC: AnyEvent::Feed IRC Channel
See the same channel as the L<AnyEvent::XMPP> module:
IRC Network: http://freenode.net/
Server : chat.freenode.net
Channel : #ae_xmpp
Feel free to join and ask questions!
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/AnyEvent-Feed>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/AnyEvent-Feed>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=AnyEvent-Feed>
=item * Search CPAN
L<http://search.cpan.org/dist/AnyEvent-Feed>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2009 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|