Commit | Line | Data |
3353d70c |
1 | # $Id$ |
0d5e38d1 |
2 | |
729cd7a8 |
3 | package XML::Feed::Format::RSS; |
0d5e38d1 |
4 | use strict; |
5 | |
6 | use base qw( XML::Feed ); |
0d5e38d1 |
7 | use DateTime::Format::Mail; |
8 | use DateTime::Format::W3CDTF; |
723eaa15 |
9 | use XML::Atom::Util qw(iso2dt); |
0d5e38d1 |
10 | |
973e1f9e |
11 | our $PREFERRED_PARSER = "XML::RSS"; |
12 | |
9b6bc912 |
13 | |
14 | sub identify { |
15 | my $class = shift; |
16 | my $xml = shift; |
17 | my $tag = $class->_get_first_tag($xml); |
18 | return ($tag eq 'rss' || $tag eq 'RDF'); |
19 | } |
20 | |
973e1f9e |
21 | sub init_empty { |
4e9c4625 |
22 | my ($feed, %args) = @_; |
23 | $args{'version'} ||= '2.0'; |
973e1f9e |
24 | eval "use $PREFERRED_PARSER"; die $@ if $@; |
4e9c4625 |
25 | $feed->{rss} = $PREFERRED_PARSER->new(%args); |
fe3b3201 |
26 | $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/'); |
813f78d8 |
27 | $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/'); |
9a36f82c |
28 | $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom'); |
29 | $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#'); |
973e1f9e |
30 | $feed; |
31 | } |
32 | |
0d5e38d1 |
33 | sub init_string { |
34 | my $feed = shift; |
35 | my($str) = @_; |
973e1f9e |
36 | $feed->init_empty; |
b1aa7a62 |
37 | my $opts = { |
38 | hashrefs_instead_of_strings => 1, |
39 | }; |
40 | $opts->{allow_multiple} = [ 'enclosure' ] if $XML::Feed::MULTIPLE_ENCLOSURES; |
0d5e38d1 |
41 | if ($str) { |
b1aa7a62 |
42 | $feed->{rss}->parse($$str, $opts ); |
0d5e38d1 |
43 | } |
44 | $feed; |
45 | } |
46 | |
47 | sub format { 'RSS ' . $_[0]->{rss}->{'version'} } |
48 | |
49 | ## The following elements are the same in all versions of RSS. |
973e1f9e |
50 | sub title { shift->{rss}->channel('title', @_) } |
51 | sub link { shift->{rss}->channel('link', @_) } |
52 | sub description { shift->{rss}->channel('description', @_) } |
0d5e38d1 |
53 | |
813f78d8 |
54 | # This doesn't exist in RSS |
55 | sub id { } |
56 | |
0d5e38d1 |
57 | ## This is RSS 2.0 only--what's the equivalent in RSS 1.0? |
973e1f9e |
58 | sub copyright { shift->{rss}->channel('copyright', @_) } |
0d5e38d1 |
59 | |
5383a560 |
60 | sub base { |
61 | my $feed = shift; |
62 | if (@_) { |
63 | $feed->{rss}->{'xml:base'} = $_[0]; |
64 | } else { |
65 | $feed->{rss}->{'xml:base'}; |
66 | } |
67 | } |
68 | |
0d5e38d1 |
69 | ## The following all work transparently in any RSS version. |
70 | sub language { |
973e1f9e |
71 | my $feed = shift; |
72 | if (@_) { |
73 | $feed->{rss}->channel('language', $_[0]); |
74 | $feed->{rss}->channel->{dc}{language} = $_[0]; |
75 | } else { |
76 | $feed->{rss}->channel('language') || |
77 | $feed->{rss}->channel->{dc}{language}; |
78 | } |
0d5e38d1 |
79 | } |
80 | |
9a36f82c |
81 | sub self_link { |
82 | my $feed = shift; |
83 | |
84 | if (@_) { |
85 | my $uri = shift; |
86 | |
87 | $feed->{rss}->channel->{'atom'}{'link'} = |
88 | { |
89 | rel => "self", |
90 | href => $uri, |
91 | type => "application/rss+xml", |
92 | }; |
93 | } |
94 | |
95 | return $feed->{rss}->channel->{'atom'}{'link'}; |
96 | } |
97 | |
98 | |
0d5e38d1 |
99 | sub generator { |
973e1f9e |
100 | my $feed = shift; |
101 | if (@_) { |
102 | $feed->{rss}->channel('generator', $_[0]); |
103 | $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} = |
104 | $_[0]; |
105 | } else { |
106 | $feed->{rss}->channel('generator') || |
107 | $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent}; |
108 | } |
0d5e38d1 |
109 | } |
110 | |
111 | sub author { |
973e1f9e |
112 | my $feed = shift; |
113 | if (@_) { |
114 | $feed->{rss}->channel('webMaster', $_[0]); |
115 | $feed->{rss}->channel->{dc}{creator} = $_[0]; |
116 | } else { |
117 | $feed->{rss}->channel('webMaster') || |
118 | $feed->{rss}->channel->{dc}{creator}; |
119 | } |
0d5e38d1 |
120 | } |
121 | |
122 | sub modified { |
973e1f9e |
123 | my $rss = shift->{rss}; |
124 | if (@_) { |
125 | $rss->channel('pubDate', |
126 | DateTime::Format::Mail->format_datetime($_[0])); |
127 | ## XML::RSS is so weird... if I set this, it will try to use |
128 | ## the value for the lastBuildDate, which I don't want--because |
129 | ## this date is formatted for an RSS 1.0 feed. So it's commented out. |
130 | #$rss->channel->{dc}{date} = |
131 | # DateTime::Format::W3CDTF->format_datetime($_[0]); |
fe3b3201 |
132 | } else { |
133 | my $date; |
134 | eval { |
135 | if (my $ts = $rss->channel('pubDate')) { |
136 | $date = DateTime::Format::Mail->parse_datetime($ts); |
137 | } elsif ($ts = $rss->channel->{dc}{date}) { |
138 | $date = DateTime::Format::W3CDTF->parse_datetime($ts); |
139 | } |
140 | }; |
141 | return $date; |
0d5e38d1 |
142 | } |
143 | } |
144 | |
145 | sub entries { |
146 | my $rss = $_[0]->{rss}; |
147 | my @entries; |
148 | for my $item (@{ $rss->{items} }) { |
729cd7a8 |
149 | push @entries, XML::Feed::Entry::Format::RSS->wrap($item); |
3bdbab6f |
150 | $entries[-1]->{_version} = $rss->{'version'}; |
0d5e38d1 |
151 | } |
152 | @entries; |
153 | } |
154 | |
973e1f9e |
155 | sub add_entry { |
33d4cb3f |
156 | my $feed = shift; |
157 | my $entry = shift || return; |
158 | $entry = $feed->_convert_entry($entry); |
973e1f9e |
159 | $feed->{rss}->add_item(%{ $entry->unwrap }); |
160 | } |
161 | |
162 | sub as_xml { $_[0]->{rss}->as_string } |
163 | |
729cd7a8 |
164 | package XML::Feed::Entry::Format::RSS; |
0d5e38d1 |
165 | use strict; |
166 | |
3bdbab6f |
167 | sub format { 'RSS ' . $_[0]->{'_version'} } |
168 | |
a749d9b9 |
169 | use XML::Feed::Content; |
170 | |
0d5e38d1 |
171 | use base qw( XML::Feed::Entry ); |
172 | |
973e1f9e |
173 | sub init_empty { $_[0]->{entry} = { } } |
174 | |
5383a560 |
175 | sub base { |
176 | my $entry = shift; |
177 | @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'}; |
178 | } |
179 | |
973e1f9e |
180 | sub title { |
181 | my $entry = shift; |
182 | @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title}; |
183 | } |
184 | |
185 | sub link { |
186 | my $entry = shift; |
187 | if (@_) { |
188 | $entry->{entry}{link} = $_[0]; |
189 | ## For RSS 2.0 output from XML::RSS. Sigh. |
190 | $entry->{entry}{permaLink} = $_[0]; |
191 | } else { |
192 | $entry->{entry}{link} || $entry->{entry}{guid}; |
193 | } |
194 | } |
a749d9b9 |
195 | |
196 | sub summary { |
973e1f9e |
197 | my $item = shift->{entry}; |
198 | if (@_) { |
199 | $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ? |
200 | $_[0]->body : $_[0]; |
201 | ## Because of the logic below, we need to add some dummy content, |
202 | ## so that we'll properly recognize the description we enter as |
203 | ## the summary. |
fe3b3201 |
204 | if (!$item->{content}{encoded} && |
973e1f9e |
205 | !$item->{'http://www.w3.org/1999/xhtml'}{body}) { |
fe3b3201 |
206 | $item->{content}{encoded} = ' '; |
973e1f9e |
207 | } |
208 | } else { |
209 | ## Some RSS feeds use <description> for a summary, and some use it |
210 | ## for the full content. Pretty gross. We don't want to return the |
211 | ## full content if the caller expects a summary, so the heuristic is: |
212 | ## if the <entry> contains both a <description> and one of the elements |
213 | ## typically used for the full content, use <description> as summary. |
214 | my $txt; |
215 | if ($item->{description} && |
fe3b3201 |
216 | ($item->{content}{encoded} || |
973e1f9e |
217 | $item->{'http://www.w3.org/1999/xhtml'}{body})) { |
218 | $txt = $item->{description}; |
723eaa15 |
219 | ## Blogspot's 'short' RSS feeds do this in the Atom namespace |
220 | ## for no obviously good reason. |
221 | } elsif ($item->{'http://www.w3.org/2005/Atom'}{summary}) { |
222 | $txt = $item->{'http://www.w3.org/2005/Atom'}{summary}; |
973e1f9e |
223 | } |
224 | XML::Feed::Content->wrap({ type => 'text/plain', body => $txt }); |
a749d9b9 |
225 | } |
a749d9b9 |
226 | } |
0d5e38d1 |
227 | |
228 | sub content { |
973e1f9e |
229 | my $item = shift->{entry}; |
230 | if (@_) { |
e3b96b9c |
231 | my $c; |
232 | if (ref($_[0]) eq 'XML::Feed::Content') { |
233 | if (defined $_[0]->base) { |
234 | $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base }; |
235 | } else { |
236 | $c = $_[0]->body; |
237 | } |
238 | } else { |
239 | $c = $_[0]; |
240 | } |
fe3b3201 |
241 | $item->{content}{encoded} = $c; |
973e1f9e |
242 | } else { |
7c89ffff |
243 | my $base; |
973e1f9e |
244 | my $body = |
fe3b3201 |
245 | $item->{content}{encoded} || |
973e1f9e |
246 | $item->{'http://www.w3.org/1999/xhtml'}{body} || |
e3b96b9c |
247 | $item->{description}; |
248 | if ('HASH' eq ref($body)) { |
249 | $base = $body->{'xml:base'}; |
250 | $body = $body->{content}; |
251 | } |
7c89ffff |
252 | XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base }); |
973e1f9e |
253 | } |
0d5e38d1 |
254 | } |
255 | |
256 | sub category { |
a0cca2a4 |
257 | my $entry = shift; |
258 | my $item = $entry->{entry}; |
973e1f9e |
259 | if (@_) { |
a0cca2a4 |
260 | my @tmp = ($entry->category, @_); |
261 | $item->{category} = [@tmp]; |
262 | $item->{dc}{subject} = [@tmp]; |
973e1f9e |
263 | } else { |
a0cca2a4 |
264 | my $r = $item->{category} || $item->{dc}{subject}; |
e5a65e5e |
265 | my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : (); |
a0cca2a4 |
266 | return wantarray? @r : $r[0]; |
973e1f9e |
267 | } |
0d5e38d1 |
268 | } |
269 | |
270 | sub author { |
973e1f9e |
271 | my $item = shift->{entry}; |
272 | if (@_) { |
273 | $item->{author} = $item->{dc}{creator} = $_[0]; |
274 | } else { |
275 | $item->{author} || $item->{dc}{creator}; |
276 | } |
0d5e38d1 |
277 | } |
278 | |
279 | ## XML::RSS doesn't give us access to the rdf:about for the <item>, |
280 | ## so we have to fall back to the <link> element in RSS 1.0 feeds. |
281 | sub id { |
973e1f9e |
282 | my $item = shift->{entry}; |
283 | if (@_) { |
284 | $item->{guid} = $_[0]; |
285 | } else { |
286 | $item->{guid} || $item->{link}; |
287 | } |
0d5e38d1 |
288 | } |
289 | |
290 | sub issued { |
973e1f9e |
291 | my $item = shift->{entry}; |
292 | if (@_) { |
293 | $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]); |
294 | $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]); |
295 | } else { |
ecac864a |
296 | ## Either of these could die if the format is invalid. |
297 | my $date; |
298 | eval { |
299 | if (my $ts = $item->{pubDate}) { |
300 | my $parser = DateTime::Format::Mail->new; |
301 | $parser->loose; |
302 | $date = $parser->parse_datetime($ts); |
daba7240 |
303 | } elsif ($ts = $item->{dc}{date} or $ts = $item->{dcterms}{date}) { |
304 | $date = DateTime::Format::W3CDTF->parse_datetime($ts); |
ecac864a |
305 | } |
306 | }; |
307 | return $date; |
0d5e38d1 |
308 | } |
309 | } |
310 | |
311 | sub modified { |
973e1f9e |
312 | my $item = shift->{entry}; |
313 | if (@_) { |
fe3b3201 |
314 | $item->{dcterms}{modified} = |
973e1f9e |
315 | DateTime::Format::W3CDTF->format_datetime($_[0]); |
316 | } else { |
723eaa15 |
317 | if (my $ts = $item->{dcterms}{modified} || $item->{'http://www.w3.org/2005/Atom'}{updated}) { |
318 | return eval { DateTime::Format::W3CDTF->parse_datetime($ts) } || eval { XML::Atom::Util::iso2dt($ts) }; |
319 | } |
0d5e38d1 |
320 | } |
321 | } |
322 | |
9a36f82c |
323 | sub lat { |
324 | my $item = shift->{entry}; |
325 | if (@_) { |
bd19b444 |
326 | $item->{geo}{lat} = $_[0]; |
9a36f82c |
327 | } else { |
bd19b444 |
328 | return $item->{geo}{lat}; |
9a36f82c |
329 | } |
330 | } |
331 | |
332 | sub long { |
333 | my $item = shift->{entry}; |
334 | if (@_) { |
bd19b444 |
335 | $item->{geo}{long} = $_[0]; |
9a36f82c |
336 | } else { |
bd19b444 |
337 | return $item->{geo}{long}; |
9a36f82c |
338 | } |
339 | } |
340 | |
12a4079f |
341 | sub enclosure { |
342 | my $entry = shift; |
343 | |
344 | if (@_) { |
345 | my $enclosure = shift; |
b1aa7a62 |
346 | my $val = { |
af6b00a4 |
347 | url => $enclosure->{url}, |
348 | type => $enclosure->{type}, |
349 | length => $enclosure->{length} |
b1aa7a62 |
350 | }; |
351 | if ($XML::Feed::MULTIPLE_ENCLOSURES) { |
352 | push @{$entry->{entry}->{enclosure}}, $val; |
353 | } else { |
354 | $entry->{entry}->{enclosure} = $val; |
355 | } |
12a4079f |
356 | } else { |
b1aa7a62 |
357 | my $tmp = $entry->{entry}->{enclosure}; |
358 | my @encs = map { XML::Feed::Enclosure->new($_) } (ref $tmp eq 'ARRAY')? @$tmp : ($tmp); |
359 | return ($XML::Feed::MULTIPLE_ENCLOSURES)? @encs : $encs[-1]; |
12a4079f |
360 | } |
af6b00a4 |
361 | } |
9a36f82c |
362 | |
0d5e38d1 |
363 | 1; |