1 # $Id: Feed.pm 1958 2006-08-14 05:31:27Z btrott $
6 use base qw( Class::ErrorHandler );
11 our $VERSION = '0.21';
15 my $format = shift || 'Atom';
16 my $format_class = 'XML::Feed::' . $format;
17 eval "use $format_class";
18 Carp::croak("Unsupported format $format: $@") if $@;
19 my $feed = bless {}, join('::', __PACKAGE__, $format);
20 $feed->init_empty(@_) or return $class->error($feed->errstr);
28 my($stream, $specified_format) = @_;
29 return $class->error("Stream parameter is required") unless $stream;
30 my $feed = bless {}, $class;
32 if (UNIVERSAL::isa($stream, 'URI')) {
33 my $res = URI::Fetch->fetch($stream)
34 or return $class->error(URI::Fetch->errstr);
35 return $class->error("This feed has been permanently removed")
36 if $res->status == URI::Fetch::URI_GONE();
38 } elsif (ref($stream) eq 'SCALAR') {
40 } elsif (ref($stream)) {
41 while (read($stream, my($chunk), 8192)) {
46 or return $class->error("Can't open $stream: $!");
47 while (read $fh, my($chunk), 8192) {
52 return $class->error("Can't get feed XML content from $stream")
55 if ($specified_format) {
56 $format = $specified_format;
58 $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
61 my $format_class = join '::', __PACKAGE__, $format;
62 eval "use $format_class";
63 return $class->error("Unsupported format $format: $@") if $@;
64 bless $feed, $format_class;
65 $feed->init_string(\$xml) or return $class->error($feed->errstr);
72 ## Auto-detect feed type based on first element. This is prone
73 ## to breakage, but then again we don't want to parse the whole
76 while ($$xml =~ /<(\S+)/sg) {
77 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
78 my $first = substr $t, 0, 1;
79 $tag = $t, last unless $first eq '?' || $first eq '!';
81 return $feed->error("Cannot find first element") unless $tag;
83 if ($tag eq 'rss' || $tag eq 'RDF') {
85 } elsif ($tag eq 'feed') {
88 return $feed->error("Cannot detect feed type");
95 my @feeds = Feed::Find->find($uri)
96 or return $class->error(Feed::Find->errstr);
103 my $new = __PACKAGE__->new($format);
104 for my $field (qw( title link description language author copyright modified generator )) {
105 my $val = $feed->$field();
106 next unless defined $val;
109 for my $entry ($feed->entries) {
110 $new->add_entry($entry->convert($format));
118 my %ids = map { $_->id => 1 } $feed->entries;
119 for my $entry ($other->entries) {
120 $feed->add_entry($entry) unless $ids{$entry->id}++;
139 sub tagline { shift->description(@_) }
140 sub items { $_[0]->entries }
147 XML::Feed - Syndication feed parser and auto-discovery
152 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
153 or die XML::Feed->errstr;
154 print $feed->title, "\n";
155 for my $entry ($feed->entries) {
158 ## Find all of the syndication feeds on a given page, using
160 my @feeds = XML::Feed->find_feeds('http://example.com/');
164 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
165 also implements feed auto-discovery for finding feeds, given a URI.
167 I<XML::Feed> supports the following syndication feed formats:
181 The goal of I<XML::Feed> is to provide a unified API for parsing and using
182 the various syndication formats. The different flavors of RSS and Atom
183 handle data in different ways: date handling; summaries and content;
184 escaping and quoting; etc. This module attempts to remove those differences
185 by providing a wrapper around the formats and the classes implementing
186 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
187 handled differently in each of the above formats. To provide a unified API for
188 date handling, I<XML::Feed> converts all date formats transparently into
189 I<DateTime> objects, which it then returns to the caller.
193 =head2 XML::Feed->new($format)
195 Creates a new empty I<XML::Feed> object using the format I<$format>.
197 $feed = XML::Feed->new('Atom');
198 $feed = XML::Feed->new('RSS');
199 $feed = XML::Feed->new('RSS', version => '0.91');
201 =head2 XML::Feed->parse($stream)
203 =head2 XML::Feed->parse($stream, $format)
205 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
206 one of the following:
210 =item * Scalar reference
212 A reference to string containing the XML body of the feed.
216 An open filehandle from which the feed XML will be read.
220 The name of a file containing the feed XML.
224 A URI from which the feed XML will be retrieved.
228 C<$format> allows you to override format guessing.
230 =head2 XML::Feed->find_feeds($uri)
232 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
233 from that page (using I<E<lt>linkE<gt>> tags).
235 Returns a list of feed URIs.
237 =head2 $feed->convert($format)
239 Converts the I<XML::Feed> object into the I<$format> format, and returns
242 =head2 $feed->splice($other_feed)
244 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
245 skipping posts that are already in I<$feed>.
249 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
251 =head2 $feed->title([ $title ])
253 The title of the feed/channel.
255 =head2 $feed->link([ $uri ])
257 The permalink of the feed/channel.
259 =head2 $feed->tagline([ $tagline ])
261 The description or tagline of the feed/channel.
263 =head2 $feed->description([ $description ])
265 Alias for I<$feed-E<gt>tagline>.
267 =head2 $feed->author([ $author ])
269 The author of the feed/channel.
271 =head2 $feed->language([ $language ])
273 The language of the feed.
275 =head2 $feed->copyright([ $copyright ])
277 The copyright notice of the feed.
279 =head2 $feed->modified([ $modified ])
281 A I<DateTime> object representing the last-modified date of the feed.
283 If present, I<$modified> should be a I<DateTime> object.
285 =head2 $feed->generator([ $generator ])
287 The generator of the feed.
289 =head2 $feed->self_link ([ $uri ])
291 The Atom Self-link of the feed:
293 L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
297 =head2 $feed->entries
299 A list of the entries/items in the feed. Returns an array containing
300 I<XML::Feed::Entry> objects.
302 =head2 $feed->add_entry($entry)
304 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
305 object in the correct format for the feed.
309 Returns an XML representation of the feed, in the format determined by
310 the current format of the I<$feed> object.
312 =head1 PACKAGE VARIABLES
316 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
318 If you want to use another RSS parser class than XML::RSS (default), you can
319 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
322 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
324 B<Note:> this will only work for parsing feeds, not creating feeds.
330 I<XML::Feed> is free software; you may redistribute it and/or modify it
331 under the same terms as Perl itself.
333 =head1 AUTHOR & COPYRIGHT
335 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
336 Six Apart, cpan@sixapart.com. All rights reserved.
340 The latest version of I<XML::Feed> can be found at
342 http://code.sixapart.com/svn/XML-Feed/trunk/