1 # $Id: Feed.pm 1956 2006-08-08 04:34:54Z btrott $
6 use base qw( Class::ErrorHandler );
11 our $VERSION = '0.11';
17 my $format_class = 'XML::Feed::' . $format;
18 eval "use $format_class";
19 Carp::croak("Unsupported format $format: $@") if $@;
20 my $feed = bless {}, join('::', __PACKAGE__, $format);
21 $feed->init_empty or return $class->error($feed->errstr);
30 return $class->error("Stream parameter is required") unless $stream;
31 my $feed = bless {}, $class;
33 if (UNIVERSAL::isa($stream, 'URI')) {
34 my $res = URI::Fetch->fetch($stream)
35 or return $class->error(URI::Fetch->errstr);
36 return $class->error("This feed has been permanently removed")
37 if $res->status == URI::Fetch::URI_GONE();
39 } elsif (ref($stream) eq 'SCALAR') {
41 } elsif (ref($stream)) {
42 while (read($stream, my($chunk), 8192)) {
47 or return $class->error("Can't open $stream: $!");
48 while (read $fh, my($chunk), 8192) {
53 return $class->error("Can't get feed XML content from $stream")
55 my $format = $feed->identify_format(\$xml)
56 or return $class->error($feed->errstr);
57 my $format_class = join '::', __PACKAGE__, $format;
58 eval "use $format_class";
59 return $class->error("Unsupported format $format: $@") if $@;
60 bless $feed, $format_class;
61 $feed->init_string(\$xml) or return $class->error($feed->errstr);
68 ## Auto-detect feed type based on first element. This is prone
69 ## to breakage, but then again we don't want to parse the whole
72 while ($$xml =~ /<(\S+)/sg) {
73 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
74 my $first = substr $t, 0, 1;
75 $tag = $t, last unless $first eq '?' || $first eq '!';
77 return $feed->error("Cannot find first element") unless $tag;
79 if ($tag eq 'rss' || $tag eq 'RDF') {
81 } elsif ($tag eq 'feed') {
84 return $feed->error("Cannot detect feed type");
91 my @feeds = Feed::Find->find($uri)
92 or return $class->error(Feed::Find->errstr);
99 my $new = __PACKAGE__->new($format);
100 for my $field (qw( title link description language author copyright modified generator )) {
101 my $val = $feed->$field();
102 next unless defined $val;
105 for my $entry ($feed->entries) {
106 $new->add_entry($entry->convert($format));
114 my %ids = map { $_->id => 1 } $feed->entries;
115 for my $entry ($other->entries) {
116 $feed->add_entry($entry) unless $ids{$entry->id}++;
133 sub tagline { shift->description(@_) }
134 sub items { $_[0]->entries }
141 XML::Feed - Syndication feed parser and auto-discovery
146 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
147 or die XML::Feed->errstr;
148 print $feed->title, "\n";
149 for my $entry ($feed->entries) {
152 ## Find all of the syndication feeds on a given page, using
154 my @feeds = XML::Feed->find_feeds('http://example.com/');
158 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
159 also implements feed auto-discovery for finding feeds, given a URI.
161 I<XML::Feed> supports the following syndication feed formats:
175 The goal of I<XML::Feed> is to provide a unified API for parsing and using
176 the various syndication formats. The different flavors of RSS and Atom
177 handle data in different ways: date handling; summaries and content;
178 escaping and quoting; etc. This module attempts to remove those differences
179 by providing a wrapper around the formats and the classes implementing
180 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
181 handled differently in each of the above formats. To provide a unified API for
182 date handling, I<XML::Feed> converts all date formats transparently into
183 I<DateTime> objects, which it then returns to the caller.
187 =head2 XML::Feed->new($format)
189 Creates a new empty I<XML::Feed> object using the format I<$format>.
191 =head2 XML::Feed->parse($stream)
193 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
194 one of the following:
198 =item * Scalar reference
200 A reference to string containing the XML body of the feed.
204 An open filehandle from which the feed XML will be read.
208 The name of a file containing the feed XML.
212 A URI from which the feed XML will be retrieved.
216 =head2 XML::Feed->find_feeds($uri)
218 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
219 from that page (using I<E<lt>linkE<gt>> tags).
221 Returns a list of feed URIs.
223 =head2 $feed->convert($format)
225 Converts the I<XML::Feed> object into the I<$format> format, and returns
228 =head2 $feed->splice($other_feed)
230 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
231 skipping posts that are already in I<$feed>.
235 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
237 =head2 $feed->title([ $title ])
239 The title of the feed/channel.
241 =head2 $feed->link([ $uri ])
243 The permalink of the feed/channel.
245 =head2 $feed->tagline([ $tagline ])
247 The description or tagline of the feed/channel.
249 =head2 $feed->description([ $description ])
251 Alias for I<$feed-E<gt>tagline>.
253 =head2 $feed->author([ $author ])
255 The author of the feed/channel.
257 =head2 $feed->language([ $language ])
259 The language of the feed.
261 =head2 $feed->copyright([ $copyright ])
263 The copyright notice of the feed.
265 =head2 $feed->modified([ $modified ])
267 A I<DateTime> object representing the last-modified date of the feed.
269 If present, I<$modified> should be a I<DateTime> object.
271 =head2 $feed->generator([ $generator ])
273 The generator of the feed.
275 =head2 $feed->entries
277 A list of the entries/items in the feed. Returns an array containing
278 I<XML::Feed::Entry> objects.
280 =head2 $feed->add_entry($entry)
282 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
283 object in the correct format for the feed.
287 Returns an XML representation of the feed, in the format determined by
288 the current format of the I<$feed> object.
290 =head1 PACKAGE VARIABLES
294 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
296 If you want to use another RSS parser class than XML::RSS (default), you can
297 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
300 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
302 B<Note:> this will only work for parsing feeds, not creating feeds.
308 I<XML::Feed> is free software; you may redistribute it and/or modify it
309 under the same terms as Perl itself.
311 =head1 AUTHOR & COPYRIGHT
313 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2005
314 Six Apart, cpan@sixapart.com. All rights reserved.