root/trunk/plagger/lib/Plagger/Plugin/Subscription/Feed.pm

Revision 861 (checked in by miyagawa, 14 years ago)

Add Subscription::Feed to subscribe to feed entries.

Line 
1 package Plagger::Plugin::Subscription::Feed;
2 use strict;
3 use warnings;
4
5 use base qw( Plagger::Plugin );
6 use Plagger::Util;
7
8 use XML::Feed;
9
10 sub register {
11     my ( $self, $context ) = @_;
12     $context->register_hook(
13         $self,
14         'subscription.load' => \&load,
15     );
16 }
17
18 sub load {
19     my ( $self, $context ) = @_;
20
21     # TODO: Auto-Discovery, XML::Liberal
22     my $uri = URI->new( $self->conf->{url} )
23       or $context->error("config 'url' is missing");
24
25     $self->load_feed( $context, $uri );
26 }
27
28 sub load_feed {
29     my ( $self, $context, $uri ) = @_;
30
31     my $content = Plagger::Util::load_uri($uri);
32     my $feed = eval { XML::Feed->parse(\$content) };
33
34     unless ($feed) {
35         $context->log( error => "Error loading feed $uri: " . ($@ || XML::Feed->errstr) );
36         return;
37     }
38
39     for my $entry ($feed->entries) {
40         my $url = $entry->link or next;
41
42         my $feed = Plagger::Feed->new;
43         $feed->url($url);
44
45         $context->subscription->add($feed);
46     }
47
48     return 1;
49 }
50
51 1;
52
53 =head1 NAME
54
55 Plagger::Plugin::Subscription::Feed - Subscribe entries in a XML feed (RSS/Atom)
56
57 =head1 SYNOPSIS
58
59   - module: Subscription::Feed
60     config:
61       url: http://del.icio.us/rss/miyagawa/mycomments
62
63 =head1 DESCRIPTION
64
65 This module subscribes to entries in a XML feed.
66
67 =head1 AUTHOR
68
69 Tatsuhiko Miyagawa
70
71 =head1 SEE ALSO
72
73 L<Plagger>, L<XML::Feed>
Note: See TracBrowser for help on using the browser.