3 package XML::Feed::Format::RSS;
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
10 our $PREFERRED_PARSER = "XML::RSS";
16 my $tag = $class->_get_first_tag($xml);
17 return ($tag eq 'rss' || $tag eq 'RDF');
21 my ($feed, %args) = @_;
22 $args{'version'} ||= '2.0';
23 eval "use $PREFERRED_PARSER"; die $@ if $@;
24 $feed->{rss} = $PREFERRED_PARSER->new(%args);
25 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
26 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');
27 $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom');
28 $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#');
37 $feed->{rss}->parse($$str, { hashrefs_instead_of_strings => 1 } );
42 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
44 ## The following elements are the same in all versions of RSS.
45 sub title { shift->{rss}->channel('title', @_) }
46 sub link { shift->{rss}->channel('link', @_) }
47 sub description { shift->{rss}->channel('description', @_) }
49 # This doesn't exist in RSS
52 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
53 sub copyright { shift->{rss}->channel('copyright', @_) }
58 $feed->{rss}->{'xml:base'} = $_[0];
60 $feed->{rss}->{'xml:base'};
64 ## The following all work transparently in any RSS version.
68 $feed->{rss}->channel('language', $_[0]);
69 $feed->{rss}->channel->{dc}{language} = $_[0];
71 $feed->{rss}->channel('language') ||
72 $feed->{rss}->channel->{dc}{language};
82 $feed->{rss}->channel->{'atom'}{'link'} =
86 type => "application/rss+xml",
90 return $feed->{rss}->channel->{'atom'}{'link'};
97 $feed->{rss}->channel('generator', $_[0]);
98 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
101 $feed->{rss}->channel('generator') ||
102 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
109 $feed->{rss}->channel('webMaster', $_[0]);
110 $feed->{rss}->channel->{dc}{creator} = $_[0];
112 $feed->{rss}->channel('webMaster') ||
113 $feed->{rss}->channel->{dc}{creator};
118 my $rss = shift->{rss};
120 $rss->channel('pubDate',
121 DateTime::Format::Mail->format_datetime($_[0]));
122 ## XML::RSS is so weird... if I set this, it will try to use
123 ## the value for the lastBuildDate, which I don't want--because
124 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
125 #$rss->channel->{dc}{date} =
126 # DateTime::Format::W3CDTF->format_datetime($_[0]);
130 if (my $ts = $rss->channel('pubDate')) {
131 $date = DateTime::Format::Mail->parse_datetime($ts);
132 } elsif ($ts = $rss->channel->{dc}{date}) {
133 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
141 my $rss = $_[0]->{rss};
143 for my $item (@{ $rss->{items} }) {
144 push @entries, XML::Feed::Entry::Format::RSS->wrap($item);
145 $entries[-1]->{_version} = $rss->{'version'};
152 my $entry = shift || return;
153 $entry = $feed->_convert_entry($entry);
154 $feed->{rss}->add_item(%{ $entry->unwrap });
157 sub as_xml { $_[0]->{rss}->as_string }
159 package XML::Feed::Entry::Format::RSS;
162 sub format { 'RSS ' . $_[0]->{'_version'} }
164 use XML::Feed::Content;
166 use base qw( XML::Feed::Entry );
168 sub init_empty { $_[0]->{entry} = { } }
172 @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
177 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
183 $entry->{entry}{link} = $_[0];
184 ## For RSS 2.0 output from XML::RSS. Sigh.
185 $entry->{entry}{permaLink} = $_[0];
187 $entry->{entry}{link} || $entry->{entry}{guid};
192 my $item = shift->{entry};
194 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
196 ## Because of the logic below, we need to add some dummy content,
197 ## so that we'll properly recognize the description we enter as
199 if (!$item->{content}{encoded} &&
200 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
201 $item->{content}{encoded} = ' ';
204 ## Some RSS feeds use <description> for a summary, and some use it
205 ## for the full content. Pretty gross. We don't want to return the
206 ## full content if the caller expects a summary, so the heuristic is:
207 ## if the <entry> contains both a <description> and one of the elements
208 ## typically used for the full content, use <description> as summary.
210 if ($item->{description} &&
211 ($item->{content}{encoded} ||
212 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
213 $txt = $item->{description};
215 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
220 my $item = shift->{entry};
223 if (ref($_[0]) eq 'XML::Feed::Content') {
224 if (defined $_[0]->base) {
225 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
232 $item->{content}{encoded} = $c;
236 $item->{content}{encoded} ||
237 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
238 $item->{description};
239 if ('HASH' eq ref($body)) {
240 $base = $body->{'xml:base'};
241 $body = $body->{content};
243 XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
249 my $item = $entry->{entry};
251 my @tmp = ($entry->category, @_);
252 $item->{category} = [@tmp];
253 $item->{dc}{subject} = [@tmp];
255 my $r = $item->{category} || $item->{dc}{subject};
256 my @r = ref($r)? @$r : ($r);
257 return wantarray? @r : $r[0];
262 my $item = shift->{entry};
264 $item->{author} = $item->{dc}{creator} = $_[0];
266 $item->{author} || $item->{dc}{creator};
270 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
271 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
273 my $item = shift->{entry};
275 $item->{guid} = $_[0];
277 $item->{guid} || $item->{link};
282 my $item = shift->{entry};
284 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
285 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
287 ## Either of these could die if the format is invalid.
290 if (my $ts = $item->{pubDate}) {
291 my $parser = DateTime::Format::Mail->new;
293 $date = $parser->parse_datetime($ts);
294 } elsif ($ts = $item->{dc}{date}) {
295 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
303 my $item = shift->{entry};
305 $item->{dcterms}{modified} =
306 DateTime::Format::W3CDTF->format_datetime($_[0]);
308 if (my $ts = $item->{dcterms}{modified}) {
309 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };
315 my $item = shift->{entry};
317 $item->{geo}{lat} = $_[0];
319 return $item->{geo}{lat};
324 my $item = shift->{entry};
326 $item->{geo}{long} = $_[0];
328 return $item->{geo}{long};