1 # $Id: Feed.pm 1958 2006-08-14 05:31:27Z btrott $
6 use base qw( Class::ErrorHandler );
12 our $VERSION = '0.23';
16 my $format = shift || 'Atom';
17 my $format_class = 'XML::Feed::Format::' . $format;
18 eval "use $format_class";
19 Carp::croak("Unsupported format $format: $@") if $@;
20 my $feed = bless {}, join('::', __PACKAGE__, "Format", $format);
21 $feed->init_empty(@_) or return $class->error($feed->errstr);
29 my($stream, $specified_format) = @_;
30 return $class->error("Stream parameter is required") unless $stream;
31 my $feed = bless {}, $class;
33 if (UNIVERSAL::isa($stream, 'URI')) {
34 my $ua = LWP::UserAgent->new;
35 $ua->env_proxy; # force allowing of proxies
36 my $res = URI::Fetch->fetch($stream, UserAgent => $ua)
37 or return $class->error(URI::Fetch->errstr);
38 return $class->error("This feed has been permanently removed")
39 if $res->status == URI::Fetch::URI_GONE();
41 } elsif (ref($stream) eq 'SCALAR') {
43 } elsif (ref($stream)) {
44 while (read($stream, my($chunk), 8192)) {
49 or return $class->error("Can't open $stream: $!");
50 while (read $fh, my($chunk), 8192) {
55 return $class->error("Can't get feed XML content from $stream")
58 if ($specified_format) {
59 $format = $specified_format;
61 $format = $feed->identify_format(\$xml) or return $class->error($feed->errstr);
64 my $format_class = join '::', __PACKAGE__, "Format", $format;
65 eval "use $format_class";
66 return $class->error("Unsupported format $format: $@") if $@;
67 bless $feed, $format_class;
68 $feed->init_string(\$xml) or return $class->error($feed->errstr);
75 ## Auto-detect feed type based on first element. This is prone
76 ## to breakage, but then again we don't want to parse the whole
79 while ($$xml =~ /<(\S+)/sg) {
80 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
81 my $first = substr $t, 0, 1;
82 $tag = $t, last unless $first eq '?' || $first eq '!';
84 return $feed->error("Cannot find first element") unless $tag;
86 if ($tag eq 'rss' || $tag eq 'RDF') {
88 } elsif ($tag eq 'feed') {
91 return $feed->error("Cannot detect feed type");
98 my @feeds = Feed::Find->find($uri)
99 or return $class->error(Feed::Find->errstr);
106 my $new = XML::Feed->new($format);
107 for my $field (qw( title link description language author copyright modified generator )) {
108 my $val = $feed->$field();
109 next unless defined $val;
112 for my $entry ($feed->entries) {
113 $new->add_entry($entry->convert($format));
121 my %ids = map { $_->id => 1 } $feed->entries;
122 for my $entry ($other->entries) {
123 $feed->add_entry($entry) unless $ids{$entry->id}++;
130 my $feed_format = ref($feed); $feed_format =~ s!^XML::Feed::Format::!!;
131 my $entry_format = ref($entry); $entry_format =~ s!^XML::Feed::Entry::Format::!!;
132 return $entry if $entry_format eq $feed_format;
133 return $entry->convert($feed_format);
152 sub tagline { shift->description(@_) }
153 sub items { $_[0]->entries }
160 XML::Feed - Syndication feed parser and auto-discovery
165 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
166 or die XML::Feed->errstr;
167 print $feed->title, "\n";
168 for my $entry ($feed->entries) {
171 ## Find all of the syndication feeds on a given page, using
173 my @feeds = XML::Feed->find_feeds('http://example.com/');
177 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
178 also implements feed auto-discovery for finding feeds, given a URI.
180 I<XML::Feed> supports the following syndication feed formats:
194 The goal of I<XML::Feed> is to provide a unified API for parsing and using
195 the various syndication formats. The different flavors of RSS and Atom
196 handle data in different ways: date handling; summaries and content;
197 escaping and quoting; etc. This module attempts to remove those differences
198 by providing a wrapper around the formats and the classes implementing
199 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
200 handled differently in each of the above formats. To provide a unified API for
201 date handling, I<XML::Feed> converts all date formats transparently into
202 I<DateTime> objects, which it then returns to the caller.
206 =head2 XML::Feed->new($format)
208 Creates a new empty I<XML::Feed> object using the format I<$format>.
210 $feed = XML::Feed->new('Atom');
211 $feed = XML::Feed->new('RSS');
212 $feed = XML::Feed->new('RSS', version => '0.91');
214 =head2 XML::Feed->parse($stream)
216 =head2 XML::Feed->parse($stream, $format)
218 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
219 one of the following:
223 =item * Scalar reference
225 A reference to string containing the XML body of the feed.
229 An open filehandle from which the feed XML will be read.
233 The name of a file containing the feed XML.
237 A URI from which the feed XML will be retrieved.
241 I<$format> allows you to override format guessing.
243 =head2 XML::Feed->find_feeds($uri)
245 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
246 from that page (using I<E<lt>linkE<gt>> tags).
248 Returns a list of feed URIs.
250 =head2 XML::Feed->identify_format($xml)
252 Given the xml of a feed return what format it is in (C<Atom>, or some version of C<RSS>).
254 =head2 $feed->convert($format)
256 Converts the I<XML::Feed> object into the I<$format> format, and returns
259 =head2 $feed->splice($other_feed)
261 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
262 skipping posts that are already in I<$feed>.
266 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
268 =head2 $feed->title([ $title ])
270 The title of the feed/channel.
272 =head2 $feed->base([ $base ])
274 The url base of the feed/channel.
276 =head2 $feed->link([ $uri ])
278 The permalink of the feed/channel.
280 =head2 $feed->tagline([ $tagline ])
282 The description or tagline of the feed/channel.
284 =head2 $feed->description([ $description ])
286 Alias for I<$feed-E<gt>tagline>.
288 =head2 $feed->author([ $author ])
290 The author of the feed/channel.
292 =head2 $feed->language([ $language ])
294 The language of the feed.
296 =head2 $feed->copyright([ $copyright ])
298 The copyright notice of the feed.
300 =head2 $feed->modified([ $modified ])
302 A I<DateTime> object representing the last-modified date of the feed.
304 If present, I<$modified> should be a I<DateTime> object.
306 =head2 $feed->generator([ $generator ])
308 The generator of the feed.
310 =head2 $feed->self_link ([ $uri ])
312 The Atom Self-link of the feed:
314 L<http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html>
318 =head2 $feed->entries
320 A list of the entries/items in the feed. Returns an array containing
321 I<XML::Feed::Entry> objects.
325 A synonym for I<$feed->entries>.
327 =head2 $feed->add_entry($entry)
329 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
330 object in the correct format for the feed.
334 Returns an XML representation of the feed, in the format determined by
335 the current format of the I<$feed> object.
337 =head1 PACKAGE VARIABLES
341 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
343 If you want to use another RSS parser class than XML::RSS (default), you can
344 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
347 $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
349 B<Note:> this will only work for parsing feeds, not creating feeds.
355 For reference, this cgi script will create valid, albeit nonsensical feeds
356 (according to C<http://feedvalidator.org> anyway) for Atom 1.0 and RSS 0.90,
363 use CGI::Carp qw(fatalsToBrowser);
368 my @args = ( $cgi->param('format') || "Atom" );
369 push @args, ( version => $cgi->param('version') ) if $cgi->param('version');
371 my $feed = XML::Feed->new(@args);
372 $feed->id("http://".time.rand()."/");
373 $feed->title('Test Feed');
374 $feed->link($cgi->url);
375 $feed->self_link($cgi->url( -query => 1, -full => 1, -rewrite => 1) );
376 $feed->modified(DateTime->now);
378 my $entry = XML::Feed::Entry->new();
379 $entry->id("http://".time.rand()."/");
380 $entry->link("http://example.com");
381 $entry->title("Test entry");
382 $entry->summary("Test summary");
383 $entry->content("Foo");
384 $entry->modified(DateTime->now);
385 $entry->author('test@example.com (Testy McTesterson)');
386 $feed->add_entry($entry);
388 my $mime = ("Atom" eq $feed->format) ? "application/atom+xml" : "application/rss+xml";
389 print $cgi->header($mime);
395 I<XML::Feed> is free software; you may redistribute it and/or modify it
396 under the same terms as Perl itself.
398 =head1 AUTHOR & COPYRIGHT
400 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2008
401 Six Apart, cpan@sixapart.com. All rights reserved.
405 The latest version of I<XML::Feed> can be found at
407 http://code.sixapart.com/svn/XML-Feed/trunk/