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

Revision 1062 (checked in by youpy, 14 years ago)
  • Exclude inline enclosure
  • Refs #325
Line 
1 package Plagger::Plugin::Publish::Feed;
2
3 use strict;
4 use base qw( Plagger::Plugin );
5
6 our $VERSION = 0.01;
7
8 use XML::Feed;
9 use XML::Feed::Entry;
10 use XML::RSS::LibXML;
11 use File::Spec;
12
13 # xxx ugh
14 $XML::Feed::RSS::PREFERRED_PARSER =
15     $XML::RSS::LibXML::VERSION >= 0.16 ? "XML::RSS::LibXML" : "XML::RSS";
16
17 sub register {
18     my($self, $context) = @_;
19     $context->register_hook(
20         $self,
21         'publish.feed' => \&publish_feed,
22     );
23     $self->init_feed($context);
24 }
25
26 sub init_feed {
27     my($self, $context) = @_;
28
29     # check dir
30     my $dir = $self->conf->{dir};
31     unless (-e $dir && -d _) {
32         mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
33     }
34 }
35
36 sub publish_feed {
37     my($self, $context, $args) = @_;
38
39     my $conf = $self->conf;
40     my $f = $args->{feed};
41     my $feed_format = $conf->{format} || 'Atom';
42
43     # generate feed
44     my $feed = XML::Feed->new($feed_format);
45     $feed->title($f->title);
46     $feed->link($f->link);
47     $feed->modified(Plagger::Date->now);
48     $feed->generator("Plagger/$Plagger::VERSION");
49
50     # add entry
51     for my $e ($f->entries) {
52         my $entry = XML::Feed::Entry->new($feed_format);
53         $entry->title($e->title);
54         $entry->link($e->link);
55         $entry->summary($e->body_text);
56         $entry->content($e->body);
57         $entry->category(join(' ', @{$e->tags}));
58         $entry->issued($e->date) if $e->date;
59         $entry->author($e->author);
60
61         if ($e->has_enclosure) {
62             for my $enclosure (grep { defined $_->url && !$_->is_inline } $e->enclosures) {
63                 $entry->add_enclosure({
64                     url    => $enclosure->url,
65                     length => $enclosure->length,
66                     type   => $enclosure->type,
67                 });
68
69                 # RSS 2.0 by spec doesn't allow multiple enclosures
70                 last if $feed_format eq 'RSS';
71             }
72         }
73
74         $feed->add_entry($entry);
75     }
76
77     # generate file path
78     my $filepath = File::Spec->catfile($self->conf->{dir}, $self->gen_filename($f));
79
80     $context->log(info => "save feed for " . $f->link . " to $filepath");
81
82     my $xml = $feed->as_xml;
83     utf8::decode($xml) unless utf8::is_utf8($xml);
84     open my $output, ">:utf8", $filepath or $context->error("$filepath: $!");
85     print $output $xml;
86     close $output;
87 }
88
89 my %formats = (
90     'u' => sub { my $s = $_[0]->url;  $s =~ s!^https?://!!; $s },
91     'l' => sub { my $s = $_[0]->link; $s =~ s!^https?://!!; $s },
92     't' => sub { $_[0]->title },
93     'i' => sub { $_[0]->id },
94 );
95
96 my $format_re = qr/%(u|l|t|i)/;
97
98 sub gen_filename {
99     my($self, $feed) = @_;
100
101     my $file = $self->conf->{filename} ||
102         '%i.' . ($self->conf->{format} eq 'RSS' ? 'rss' : 'atom');
103     $file =~ s{$format_re}{
104         $self->safe_filename($formats{$1}->($feed))
105     }egx;
106     $file;
107 }
108
109 sub safe_filename {
110     my($self, $path) = @_;
111     $path =~ s![^\w\s]+!_!g;
112     $path =~ s!\s+!_!g;
113     $path;
114 }
115
116 # XXX okay, this is a hack until XML::Feed is updated
117 *XML::Feed::Entry::Atom::add_enclosure = sub {
118     my($entry, $enclosure) = @_;
119     my $link = XML::Atom::Link->new;
120     $link->rel('enclosure');
121     $link->type($enclosure->{type});
122     $link->href($enclosure->{url});
123     $link->length($enclosure->{length});
124     $entry->{entry}->add_link($link);
125 };
126
127 *XML::Feed::Entry::RSS::add_enclosure = sub {
128     my($entry, $enclosure) = @_;
129     $entry->{entry}->{enclosure} = {
130         url    => $enclosure->{url},
131         type   => $enclosure->{type},
132         length => $enclosure->{length},
133     };
134 };
135
136
137 1;
138
139 __END__
140
141 =head1
142
143 Plagger::Plugin::Publish::Feed - republish RSS/Atom feeds
144
145 =head1 SYNOPSYS
146
147   - module: Publish::Feed
148     config:
149       format: RSS
150       dir: /home/yoshiki/plagger/feed
151       filename: my_%t.rss
152
153 =head1 CONFIG
154
155 =head2 format
156
157 Specify the format of feed. C<Plagger::Plugin::Publish::Feed> supports
158 the following syndication feed formats:
159
160 =over 4
161
162 =item * Atom (default)
163
164 =item * RSS
165
166 =back
167
168 =head2 dir
169
170 Directory to save feed files in.
171
172 =head2 filename
173
174 Filename to be used to create feed files. It defaults to C<%i.rss> for
175 RSS and C<%i.atom> for Atom feed. It supports the following format
176 like printf():
177
178 =over 4
179
180 =item * %u url
181
182 =item * %l link
183
184 =item * %t title
185
186 =item * %i id
187
188 =back
189
190 =head1 AUTHOR
191
192 Yoshiki KURIHARA
193
194 Tatsuhiko Miyagawa
195
196 Gosuke Miyashita
197
198 =head1 SEE ALSO
199
200 L<Plagger>, L<XML::Feed>
201
202 =cut
Note: See TracBrowser for help on using the browser.