3 package XML::Feed::Format::RSS;
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
9 use XML::Atom::Util qw(iso2dt);
11 our $PREFERRED_PARSER = "XML::RSS";
17 my $tag = $class->_get_first_tag($xml);
18 return ($tag eq 'rss' || $tag eq 'RDF');
22 my ($feed, %args) = @_;
23 $args{'version'} ||= '2.0';
24 eval "use $PREFERRED_PARSER"; die $@ if $@;
25 $feed->{rss} = $PREFERRED_PARSER->new(%args);
26 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
27 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');
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#');
38 hashrefs_instead_of_strings => 1,
40 $opts->{allow_multiple} = [ 'enclosure' ] if $XML::Feed::MULTIPLE_ENCLOSURES;
42 $feed->{rss}->parse($$str, $opts );
47 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
49 ## The following elements are the same in all versions of RSS.
50 sub title { shift->{rss}->channel('title', @_) }
51 sub link { shift->{rss}->channel('link', @_) }
52 sub description { shift->{rss}->channel('description', @_) }
54 # This doesn't exist in RSS
57 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
58 sub copyright { shift->{rss}->channel('copyright', @_) }
63 $feed->{rss}->{'xml:base'} = $_[0];
65 $feed->{rss}->{'xml:base'};
69 ## The following all work transparently in any RSS version.
73 $feed->{rss}->channel('language', $_[0]);
74 $feed->{rss}->channel->{dc}{language} = $_[0];
76 $feed->{rss}->channel('language') ||
77 $feed->{rss}->channel->{dc}{language};
87 $feed->{rss}->channel->{'atom'}{'link'} =
91 type => "application/rss+xml",
95 return $feed->{rss}->channel->{'atom'}{'link'};
102 $feed->{rss}->channel('generator', $_[0]);
103 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
106 $feed->{rss}->channel('generator') ||
107 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
114 $feed->{rss}->channel('webMaster', $_[0]);
115 $feed->{rss}->channel->{dc}{creator} = $_[0];
117 $feed->{rss}->channel('webMaster') ||
118 $feed->{rss}->channel->{dc}{creator};
123 my $rss = shift->{rss};
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]);
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);
146 my $rss = $_[0]->{rss};
148 for my $item (@{ $rss->{items} }) {
149 push @entries, XML::Feed::Entry::Format::RSS->wrap($item);
150 $entries[-1]->{_version} = $rss->{'version'};
157 my $entry = shift || return;
158 $entry = $feed->_convert_entry($entry);
159 $feed->{rss}->add_item(%{ $entry->unwrap });
162 sub as_xml { $_[0]->{rss}->as_string }
164 package XML::Feed::Entry::Format::RSS;
167 sub format { 'RSS ' . $_[0]->{'_version'} }
169 use XML::Feed::Content;
171 use base qw( XML::Feed::Entry );
173 sub init_empty { $_[0]->{entry} = { } }
177 @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
182 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
188 $entry->{entry}{link} = $_[0];
189 ## For RSS 2.0 output from XML::RSS. Sigh.
190 $entry->{entry}{permaLink} = $_[0];
192 $entry->{entry}{link} || $entry->{entry}{guid};
197 my $item = shift->{entry};
199 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
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
204 if (!$item->{content}{encoded} &&
205 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
206 $item->{content}{encoded} = ' ';
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.
215 if ($item->{description} &&
216 ($item->{content}{encoded} ||
217 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
218 $txt = $item->{description};
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};
224 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
229 my $item = shift->{entry};
232 if (ref($_[0]) eq 'XML::Feed::Content') {
233 if (defined $_[0]->base) {
234 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
241 $item->{content}{encoded} = $c;
245 $item->{content}{encoded} ||
246 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
247 $item->{description};
248 if ('HASH' eq ref($body)) {
249 $base = $body->{'xml:base'};
250 $body = $body->{content};
252 XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
258 my $item = $entry->{entry};
260 my @tmp = ($entry->category, @_);
261 $item->{category} = [@tmp];
262 $item->{dc}{subject} = [@tmp];
264 my $r = $item->{category} || $item->{dc}{subject};
265 my @r = ref($r) eq 'ARRAY' ? @$r : defined $r? ($r) : ();
266 return wantarray? @r : $r[0];
271 my $item = shift->{entry};
273 $item->{author} = $item->{dc}{creator} = $_[0];
275 $item->{author} || $item->{dc}{creator};
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.
282 my $item = shift->{entry};
284 $item->{guid} = $_[0];
286 $item->{guid} || $item->{link};
291 my $item = shift->{entry};
293 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
294 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
296 ## Either of these could die if the format is invalid.
299 if (my $ts = $item->{pubDate}) {
300 my $parser = DateTime::Format::Mail->new;
302 $date = $parser->parse_datetime($ts);
303 } elsif ($ts = $item->{dc}{date} or $ts = $item->{dcterms}{date}) {
304 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
312 my $item = shift->{entry};
314 $item->{dcterms}{modified} =
315 DateTime::Format::W3CDTF->format_datetime($_[0]);
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) };
324 my $item = shift->{entry};
326 $item->{geo}{lat} = $_[0];
328 return $item->{geo}{lat};
333 my $item = shift->{entry};
335 $item->{geo}{long} = $_[0];
337 return $item->{geo}{long};
345 my $enclosure = shift;
347 url => $enclosure->{url},
348 type => $enclosure->{type},
349 length => $enclosure->{length}
351 if ($XML::Feed::MULTIPLE_ENCLOSURES) {
352 push @{$entry->{entry}->{enclosure}}, $val;
354 $entry->{entry}->{enclosure} = $val;
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];