root/branches/hackathon-summary/plagger/lib/Plagger/Plugin/Publish/Feed.pm

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

Plugins refactorthon:

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