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/');
27 $feed->{rss}->parse($$str);
32 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
34 ## The following elements are the same in all versions of RSS.
35 sub title { shift->{rss}->channel('title', @_) }
36 sub link { shift->{rss}->channel('link', @_) }
37 sub description { shift->{rss}->channel('description', @_) }
39 # This doesn't exist in RSS
42 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
43 sub copyright { shift->{rss}->channel('copyright', @_) }
45 ## The following all work transparently in any RSS version.
49 $feed->{rss}->channel('language', $_[0]);
50 $feed->{rss}->channel->{dc}{language} = $_[0];
52 $feed->{rss}->channel('language') ||
53 $feed->{rss}->channel->{dc}{language};
60 $feed->{rss}->channel('generator', $_[0]);
61 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
64 $feed->{rss}->channel('generator') ||
65 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
72 $feed->{rss}->channel('webMaster', $_[0]);
73 $feed->{rss}->channel->{dc}{creator} = $_[0];
75 $feed->{rss}->channel('webMaster') ||
76 $feed->{rss}->channel->{dc}{creator};
81 my $rss = shift->{rss};
83 $rss->channel('pubDate',
84 DateTime::Format::Mail->format_datetime($_[0]));
85 ## XML::RSS is so weird... if I set this, it will try to use
86 ## the value for the lastBuildDate, which I don't want--because
87 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
88 #$rss->channel->{dc}{date} =
89 # DateTime::Format::W3CDTF->format_datetime($_[0]);
93 if (my $ts = $rss->channel('pubDate')) {
94 $date = DateTime::Format::Mail->parse_datetime($ts);
95 } elsif ($ts = $rss->channel->{dc}{date}) {
96 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
104 my $rss = $_[0]->{rss};
106 for my $item (@{ $rss->{items} }) {
107 push @entries, XML::Feed::Entry::RSS->wrap($item);
115 $feed->{rss}->add_item(%{ $entry->unwrap });
118 sub as_xml { $_[0]->{rss}->as_string }
120 package XML::Feed::Entry::RSS;
123 use XML::Feed::Content;
125 use base qw( XML::Feed::Entry );
127 sub init_empty { $_[0]->{entry} = { } }
131 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
137 $entry->{entry}{link} = $_[0];
138 ## For RSS 2.0 output from XML::RSS. Sigh.
139 $entry->{entry}{permaLink} = $_[0];
141 $entry->{entry}{link} || $entry->{entry}{guid};
146 my $item = shift->{entry};
148 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
150 ## Because of the logic below, we need to add some dummy content,
151 ## so that we'll properly recognize the description we enter as
153 if (!$item->{content}{encoded} &&
154 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
155 $item->{content}{encoded} = ' ';
158 ## Some RSS feeds use <description> for a summary, and some use it
159 ## for the full content. Pretty gross. We don't want to return the
160 ## full content if the caller expects a summary, so the heuristic is:
161 ## if the <entry> contains both a <description> and one of the elements
162 ## typically used for the full content, use <description> as summary.
164 if ($item->{description} &&
165 ($item->{content}{encoded} ||
166 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
167 $txt = $item->{description};
169 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
174 my $item = shift->{entry};
176 my $c = ref($_[0]) eq 'XML::Feed::Content' ? $_[0]->body : $_[0];
177 $item->{content}{encoded} = $c;
180 $item->{content}{encoded} ||
181 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
182 $item->{description};
183 XML::Feed::Content->wrap({ type => 'text/html', body => $body });
188 my $item = shift->{entry};
190 $item->{category} = $item->{dc}{subject} = $_[0];
192 $item->{category} || $item->{dc}{subject};
197 my $item = shift->{entry};
199 $item->{author} = $item->{dc}{creator} = $_[0];
201 $item->{author} || $item->{dc}{creator};
205 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
206 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
208 my $item = shift->{entry};
210 $item->{guid} = $_[0];
212 $item->{guid} || $item->{link};
217 my $item = shift->{entry};
219 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
220 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
222 ## Either of these could die if the format is invalid.
225 if (my $ts = $item->{pubDate}) {
226 my $parser = DateTime::Format::Mail->new;
228 $date = $parser->parse_datetime($ts);
229 } elsif ($ts = $item->{dc}{date}) {
230 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
238 my $item = shift->{entry};
240 $item->{dcterms}{modified} =
241 DateTime::Format::W3CDTF->format_datetime($_[0]);
243 if (my $ts = $item->{dcterms}{modified}) {
244 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };