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";
14 eval "use $PREFERRED_PARSER"; die $@ if $@;
15 $feed->{rss} = $PREFERRED_PARSER->new( version => '2.0' );
16 $feed->{rss}->add_module(prefix => "content", uri => 'http://purl.org/rss/1.0/modules/content/');
17 $feed->{rss}->add_module(prefix => "dcterms", uri => 'http://purl.org/rss/1.0/modules/dcterms/');
26 $feed->{rss}->parse($$str);
31 sub format { 'RSS ' . $_[0]->{rss}->{'version'} }
33 ## The following elements are the same in all versions of RSS.
34 sub title { shift->{rss}->channel('title', @_) }
35 sub link { shift->{rss}->channel('link', @_) }
36 sub description { shift->{rss}->channel('description', @_) }
38 ## This is RSS 2.0 only--what's the equivalent in RSS 1.0?
39 sub copyright { shift->{rss}->channel('copyright', @_) }
41 ## The following all work transparently in any RSS version.
45 $feed->{rss}->channel('language', $_[0]);
46 $feed->{rss}->channel->{dc}{language} = $_[0];
48 $feed->{rss}->channel('language') ||
49 $feed->{rss}->channel->{dc}{language};
56 $feed->{rss}->channel('generator', $_[0]);
57 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent} =
60 $feed->{rss}->channel('generator') ||
61 $feed->{rss}->channel->{'http://webns.net/mvcb/'}{generatorAgent};
68 $feed->{rss}->channel('webMaster', $_[0]);
69 $feed->{rss}->channel->{dc}{creator} = $_[0];
71 $feed->{rss}->channel('webMaster') ||
72 $feed->{rss}->channel->{dc}{creator};
77 my $rss = shift->{rss};
79 $rss->channel('pubDate',
80 DateTime::Format::Mail->format_datetime($_[0]));
81 ## XML::RSS is so weird... if I set this, it will try to use
82 ## the value for the lastBuildDate, which I don't want--because
83 ## this date is formatted for an RSS 1.0 feed. So it's commented out.
84 #$rss->channel->{dc}{date} =
85 # DateTime::Format::W3CDTF->format_datetime($_[0]);
89 if (my $ts = $rss->channel('pubDate')) {
90 $date = DateTime::Format::Mail->parse_datetime($ts);
91 } elsif ($ts = $rss->channel->{dc}{date}) {
92 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
100 my $rss = $_[0]->{rss};
102 for my $item (@{ $rss->{items} }) {
103 push @entries, XML::Feed::Entry::RSS->wrap($item);
111 $feed->{rss}->add_item(%{ $entry->unwrap });
114 sub as_xml { $_[0]->{rss}->as_string }
116 package XML::Feed::Entry::RSS;
119 use XML::Feed::Content;
121 use base qw( XML::Feed::Entry );
123 sub init_empty { $_[0]->{entry} = { } }
127 @_ ? $entry->{entry}{title} = $_[0] : $entry->{entry}{title};
133 $entry->{entry}{link} = $_[0];
134 ## For RSS 2.0 output from XML::RSS. Sigh.
135 $entry->{entry}{permaLink} = $_[0];
137 $entry->{entry}{link} || $entry->{entry}{guid};
142 my $item = shift->{entry};
144 $item->{description} = ref($_[0]) eq 'XML::Feed::Content' ?
146 ## Because of the logic below, we need to add some dummy content,
147 ## so that we'll properly recognize the description we enter as
149 if (!$item->{content}{encoded} &&
150 !$item->{'http://www.w3.org/1999/xhtml'}{body}) {
151 $item->{content}{encoded} = ' ';
154 ## Some RSS feeds use <description> for a summary, and some use it
155 ## for the full content. Pretty gross. We don't want to return the
156 ## full content if the caller expects a summary, so the heuristic is:
157 ## if the <entry> contains both a <description> and one of the elements
158 ## typically used for the full content, use <description> as summary.
160 if ($item->{description} &&
161 ($item->{content}{encoded} ||
162 $item->{'http://www.w3.org/1999/xhtml'}{body})) {
163 $txt = $item->{description};
165 XML::Feed::Content->wrap({ type => 'text/plain', body => $txt });
170 my $item = shift->{entry};
172 my $c = ref($_[0]) eq 'XML::Feed::Content' ? $_[0]->body : $_[0];
173 $item->{content}{encoded} = $c;
176 $item->{content}{encoded} ||
177 $item->{'http://www.w3.org/1999/xhtml'}{body} ||
178 $item->{description};
179 XML::Feed::Content->wrap({ type => 'text/html', body => $body });
184 my $item = shift->{entry};
186 $item->{category} = $item->{dc}{subject} = $_[0];
188 $item->{category} || $item->{dc}{subject};
193 my $item = shift->{entry};
195 $item->{author} = $item->{dc}{creator} = $_[0];
197 $item->{author} || $item->{dc}{creator};
201 ## XML::RSS doesn't give us access to the rdf:about for the <item>,
202 ## so we have to fall back to the <link> element in RSS 1.0 feeds.
204 my $item = shift->{entry};
206 $item->{guid} = $_[0];
208 $item->{guid} || $item->{link};
213 my $item = shift->{entry};
215 $item->{dc}{date} = DateTime::Format::W3CDTF->format_datetime($_[0]);
216 $item->{pubDate} = DateTime::Format::Mail->format_datetime($_[0]);
218 ## Either of these could die if the format is invalid.
221 if (my $ts = $item->{pubDate}) {
222 my $parser = DateTime::Format::Mail->new;
224 $date = $parser->parse_datetime($ts);
225 } elsif ($ts = $item->{dc}{date}) {
226 $date = DateTime::Format::W3CDTF->parse_datetime($ts);
234 my $item = shift->{entry};
236 $item->{dcterms}{modified} =
237 DateTime::Format::W3CDTF->format_datetime($_[0]);
239 if (my $ts = $item->{dcterms}{modified}) {
240 return eval { DateTime::Format::W3CDTF->parse_datetime($ts) };