1 # $Id: Feed.pm 1869 2005-08-10 00:02:25Z btrott $
6 use base qw( Class::ErrorHandler );
11 our $VERSION = '0.06';
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 copyright modified generator )) {
101 $new->$field($feed->$field());
103 for my $entry ($feed->entries) {
104 $new->add_entry($entry->convert($format));
122 sub tagline { shift->description(@_) }
123 sub items { $_[0]->entries }
130 XML::Feed - Syndication feed parser and auto-discovery
135 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
136 or die XML::Feed->errstr;
137 print $feed->title, "\n";
138 for my $entry ($feed->entries) {
141 ## Find all of the syndication feeds on a given page, using
143 my @feeds = XML::Feed->find_feeds('http://example.com/');
147 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
148 also implements feed auto-discovery for finding feeds, given a URI.
150 I<XML::Feed> supports the following syndication feed formats:
164 The goal of I<XML::Feed> is to provide a unified API for parsing and using
165 the various syndication formats. The different flavors of RSS and Atom
166 handle data in different ways: date handling; summaries and content;
167 escaping and quoting; etc. This module attempts to remove those differences
168 by providing a wrapper around the formats and the classes implementing
169 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
170 handled differently in each of the above formats. To provide a unified API for
171 date handling, I<XML::Feed> converts all date formats transparently into
172 I<DateTime> objects, which it then returns to the caller.
176 =head2 XML::Feed->new($format)
178 Creates a new empty I<XML::Feed> object using the format I<$format>.
180 =head2 XML::Feed->parse($stream)
182 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
183 one of the following:
187 =item * Scalar reference
189 A reference to string containing the XML body of the feed.
193 An open filehandle from which the feed XML will be read.
197 The name of a file containing the feed XML.
201 A URI from which the feed XML will be retrieved.
205 =head2 XML::Feed->find_feeds($uri)
207 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
208 from that page (using I<E<lt>linkE<gt>> tags).
210 Returns a list of feed URIs.
212 =head2 $feed->convert($format)
214 Converts the I<XML::Feed> object into the I<$format> format, and returns
219 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
221 =head2 $feed->title([ $title ])
223 The title of the feed/channel.
225 =head2 $feed->link([ $uri ])
227 The permalink of the feed/channel.
229 =head2 $feed->tagline([ $tagline ])
231 The description or tagline of the feed/channel.
233 =head2 $feed->description([ $description ])
235 Alias for I<$feed-E<gt>tagline>.
237 =head2 $feed->author([ $author ])
239 The author of the feed/channel.
241 =head2 $feed->language([ $language ])
243 The language of the feed.
245 =head2 $feed->copyright([ $copyright ])
247 The copyright notice of the feed.
249 =head2 $feed->modified([ $modified ])
251 A I<DateTime> object representing the last-modified date of the feed.
253 If present, I<$modified> should be a I<DateTime> object.
255 =head2 $feed->generator([ $generator ])
257 The generator of the feed.
259 =head2 $feed->entries
261 A list of the entries/items in the feed. Returns an array containing
262 I<XML::Feed::Entry> objects.
264 =head2 $feed->add_entry($entry)
266 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
267 object in the correct format for the feed.
271 Returns an XML representation of the feed, in the format determined by
272 the current format of the I<$feed> object.
274 =head1 PACKAGE VARIABLES
278 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
280 If you want to use another RSS parser class than XML::RSS (default), you can
281 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
284 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
286 B<Note:> this will only work for parsing feeds, not creating feeds.
292 I<XML::Feed> is free software; you may redistribute it and/or modify it
293 under the same terms as Perl itself.
295 =head1 AUTHOR & COPYRIGHT
297 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2005
298 Six Apart, cpan@sixapart.com. All rights reserved.