1 # $Id: RSS.pm 1917 2006-02-07 07:11:44Z 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";
14 eval "use $PREFERRED_PARSER"; die $@ if $@;
15 $feed->{rss} = $PREFERRED_PARSER->new( version => '2.0' );
24 $feed->{rss}->parse($$str);
29 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
31 ## The following elements are the same in all versions of RSS.
32 sub title { shift->{rss}->channel('title', @_) }
33 sub link { shift->{rss}->channel('link', @_) }
34 sub description { shift->{rss}->channel('description', @_) }
36 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
37 sub copyright { shift->{rss}->channel('copyright', @_) }
39 ## The following all work transparently in any RSS version.
43 $feed->{rss}->channel('language', $_[0]);
44 $feed->{rss}->channel->{dc}{language} = $_[0];
46 $feed->{rss}->channel('language') ||
47 $feed->{rss}->channel->{dc}{language};
54 $feed->{rss}->channel('generator', $_[0]);
55 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
58 $feed->{rss}->channel('generator') ||
59 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
66 $feed->{rss}->channel('webMaster', $_[0]);
67 $feed->{rss}->channel->{dc}{creator} = $_[0];
69 $feed->{rss}->channel('webMaster') ||
70 $feed->{rss}->channel->{dc}{creator};
75 my $rss = shift->{rss};
77 $rss->channel('pubDate',
78 DateTime::Format::Mail->format_datetime($_[0]));
79 ## XML::RSS is so weird... if I set this, it will try to use
80 ## the value for the lastBuildDate, which I don't want--because
81 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
82 #$rss->channel->{dc}{date} =
83 # DateTime::Format::W3CDTF->format_datetime($_[0]);
85 if (my $ts = $rss->channel('pubDate')) {
86 return DateTime::Format::Mail->parse_datetime($ts);
87 } elsif ($ts = $rss->channel->{dc}{date}) {
88 return DateTime::Format::W3CDTF->parse_datetime($ts);
94 my $rss = $_[0]->{rss};
96 for my $item (@{ $rss->{items} }) {
97 push @entries, XML::Feed::Entry::RSS->wrap($item);
105 $feed->{rss}->add_item(%{ $entry->unwrap });
108 sub as_xml { $_[0]->{rss}->as_string }
110 package XML::Feed::Entry::RSS;
113 use XML::Feed::Content;
115 use base qw( XML::Feed::Entry );
117 sub init_empty { $_[0]->{entry} = { } }
121 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
127 $entry->{entry}{link} = $_[0];
128 ## For RSS 2.0 output from XML::RSS. Sigh.
129 $entry->{entry}{permaLink} = $_[0];
131 $entry->{entry}{link} || $entry->{entry}{guid};
136 my $item = shift->{entry};
138 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
140 ## Because of the logic below, we need to add some dummy content,
141 ## so that we'll properly recognize the description we enter as
143 if (!$item->{'http://purl.org/rss/1.0/modules/content/'}{encoded} &&
144 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
145 $item->{'http://purl.org/rss/1.0/modules/content/'}{encoded} = ' ';
148 ## Some RSS feeds use <description> for a summary, and some use it
149 ## for the full content. Pretty gross. We don't want to return the
150 ## full content if the caller expects a summary, so the heuristic is:
151 ## if the <entry> contains both a <description> and one of the elements
152 ## typically used for the full content, use <description> as summary.
154 if ($item->{description} &&
155 ($item->{'http://purl.org/rss/1.0/modules/content/'}{encoded} ||
156 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
157 $txt = $item->{description};
159 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
164 my $item = shift->{entry};
166 my $c = ref($_[0]) eq 'XML::Feed::Content' ? $_[0]->body : $_[0];
167 $item->{'http://purl.org/rss/1.0/modules/content/'}{encoded} = $c;
170 $item->{'http://purl.org/rss/1.0/modules/content/'}{encoded} ||
171 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
172 $item->{description};
173 XML::Feed::Content->wrap({ type => 'text/html', body => $body });
178 my $item = shift->{entry};
180 $item->{category} = $item->{dc}{subject} = $_[0];
182 $item->{category} || $item->{dc}{subject};
187 my $item = shift->{entry};
189 $item->{author} = $item->{dc}{creator} = $_[0];
191 $item->{author} || $item->{dc}{creator};
195 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
196 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
198 my $item = shift->{entry};
200 $item->{guid} = $_[0];
202 $item->{guid} || $item->{link};
207 my $item = shift->{entry};
209 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
210 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
212 ## Either of these could die if the format is invalid.
215 if (my $ts = $item->{pubDate}) {
216 my $parser = DateTime::Format::Mail->new;
218 $date = $parser->parse_datetime($ts);
219 } elsif ($ts = $item->{dc}{date}) {
220 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
228 my $item = shift->{entry};
230 $item->{'http://purl.org/rss/1.0/modules/dcterms/'}{modified} =
231 DateTime::Format::W3CDTF->format_datetime($_[0]);
233 if (my $ts = $item->{'http://purl.org/rss/1.0/modules/dcterms/'}{modified}) {
234 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };