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

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