1 # $Id: RSS.pm 1934 2006-04-22 05:13:55Z btrott $
3 package XML::Feed::RSS;
6 use base qw( XML::Feed );
7 use DateTime::Format::Mail;
8 use DateTime::Format::W3CDTF;
10 our $PREFERRED_PARSER = "XML::RSS";
13 my ($feed, %args) = @_;
14 $args{'version'} ||= '2.0';
15 eval "use $PREFERRED_PARSER"; die $@ if $@;
16 $feed->{rss} = $PREFERRED_PARSER->new(%args);
17 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
18 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/dc/terms/');
19 $feed->{rss}->add_module(prefix => "atom", uri => 'http://www.w3.org/2005/Atom');
20 $feed->{rss}->add_module(prefix => "geo", uri => 'http://www.w3.org/2003/01/geo/wgs84_pos#');
29 $feed->{rss}->parse($$str);
34 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
36 ## The following elements are the same in all versions of RSS.
37 sub title { shift->{rss}->channel('title', @_) }
38 sub link { shift->{rss}->channel('link', @_) }
39 sub description { shift->{rss}->channel('description', @_) }
41 # This doesn't exist in RSS
44 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
45 sub copyright { shift->{rss}->channel('copyright', @_) }
50 $feed->{rss}->{'xml:base'} = $_[0];
52 $feed->{rss}->{'xml:base'};
56 ## The following all work transparently in any RSS version.
60 $feed->{rss}->channel('language', $_[0]);
61 $feed->{rss}->channel->{dc}{language} = $_[0];
63 $feed->{rss}->channel('language') ||
64 $feed->{rss}->channel->{dc}{language};
74 $feed->{rss}->channel->{'atom'}{'link'} =
78 type => "application/rss+xml",
82 return $feed->{rss}->channel->{'atom'}{'link'};
89 $feed->{rss}->channel('generator', $_[0]);
90 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
93 $feed->{rss}->channel('generator') ||
94 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
101 $feed->{rss}->channel('webMaster', $_[0]);
102 $feed->{rss}->channel->{dc}{creator} = $_[0];
104 $feed->{rss}->channel('webMaster') ||
105 $feed->{rss}->channel->{dc}{creator};
110 my $rss = shift->{rss};
112 $rss->channel('pubDate',
113 DateTime::Format::Mail->format_datetime($_[0]));
114 ## XML::RSS is so weird... if I set this, it will try to use
115 ## the value for the lastBuildDate, which I don't want--because
116 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
117 #$rss->channel->{dc}{date} =
118 # DateTime::Format::W3CDTF->format_datetime($_[0]);
122 if (my $ts = $rss->channel('pubDate')) {
123 $date = DateTime::Format::Mail->parse_datetime($ts);
124 } elsif ($ts = $rss->channel->{dc}{date}) {
125 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
133 my $rss = $_[0]->{rss};
135 for my $item (@{ $rss->{items} }) {
136 push @entries, XML::Feed::Entry::RSS->wrap($item);
143 my $entry = shift || return;
144 $entry = $feed->_convert_entry($entry);
145 $feed->{rss}->add_item(%{ $entry->unwrap });
148 sub as_xml { $_[0]->{rss}->as_string }
150 package XML::Feed::Entry::RSS;
153 use XML::Feed::Content;
155 use base qw( XML::Feed::Entry );
157 sub init_empty { $_[0]->{entry} = { } }
161 @_ ? $entry->{entry}->{'xml:base'} = $_[0] : $entry->{entry}->{'xml:base'};
166 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
172 $entry->{entry}{link} = $_[0];
173 ## For RSS 2.0 output from XML::RSS. Sigh.
174 $entry->{entry}{permaLink} = $_[0];
176 $entry->{entry}{link} || $entry->{entry}{guid};
181 my $item = shift->{entry};
183 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
185 ## Because of the logic below, we need to add some dummy content,
186 ## so that we'll properly recognize the description we enter as
188 if (!$item->{content}{encoded} &&
189 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
190 $item->{content}{encoded} = ' ';
193 ## Some RSS feeds use <description> for a summary, and some use it
194 ## for the full content. Pretty gross. We don't want to return the
195 ## full content if the caller expects a summary, so the heuristic is:
196 ## if the <entry> contains both a <description> and one of the elements
197 ## typically used for the full content, use <description> as summary.
199 if ($item->{description} &&
200 ($item->{content}{encoded} ||
201 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
202 $txt = $item->{description};
204 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
209 my $item = shift->{entry};
212 if (ref($_[0]) eq 'XML::Feed::Content') {
213 if (defined $_[0]->base) {
214 $c = { 'content' => $_[0]->body, 'xml:base' => $_[0]->base };
221 $item->{content}{encoded} = $c;
225 $item->{content}{encoded} ||
226 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
227 $item->{description};
228 if ('HASH' eq ref($body)) {
229 $base = $body->{'xml:base'};
230 $body = $body->{content};
232 XML::Feed::Content->wrap({ type => 'text/html', body => $body, base => $base });
237 my $item = shift->{entry};
239 $item->{category} = $item->{dc}{subject} = $_[0];
241 $item->{category} || $item->{dc}{subject};
246 my $item = shift->{entry};
248 $item->{author} = $item->{dc}{creator} = $_[0];
250 $item->{author} || $item->{dc}{creator};
254 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
255 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
257 my $item = shift->{entry};
259 $item->{guid} = $_[0];
261 $item->{guid} || $item->{link};
266 my $item = shift->{entry};
268 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
269 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
271 ## Either of these could die if the format is invalid.
274 if (my $ts = $item->{pubDate}) {
275 my $parser = DateTime::Format::Mail->new;
277 $date = $parser->parse_datetime($ts);
278 } elsif ($ts = $item->{dc}{date}) {
279 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
287 my $item = shift->{entry};
289 $item->{dcterms}{modified} =
290 DateTime::Format::W3CDTF->format_datetime($_[0]);
292 if (my $ts = $item->{dcterms}{modified}) {
293 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };
299 my $item = shift->{entry};
301 $item->{geo}{lat} = $_[0];
303 return $item->{geo}{lat};
308 my $item = shift->{entry};
310 $item->{geo}{long} = $_[0];
312 return $item->{geo}{long};