1 # $Id: Feed.pm,v 1.6 2004/05/30 16:59:02 btrott Exp $
6 use base qw( XML::Feed::ErrorHandler );
10 use vars qw( $VERSION );
13 use constant FEED_MIME_TYPES => [
14 'application/x.atom+xml',
15 'application/atom+xml',
17 'application/rss+xml',
18 'application/rdf+xml',
24 return $class->error("Stream parameter is required") unless $stream;
25 my $feed = bless {}, $class;
27 if (UNIVERSAL::isa($stream, 'URI')) {
28 my $ua = LWP::UserAgent->new;
29 my $req = HTTP::Request->new(GET => $stream);
30 my $res = $ua->request($req);
31 if ($res->is_success) {
34 } elsif (ref($stream) eq 'SCALAR') {
36 } elsif (ref($stream)) {
37 while (read($stream, my($chunk), 8192)) {
42 or return $class->error("Can't open $stream: $!");
43 while (read $fh, my($chunk), 8192) {
48 return $class->error("Can't get feed XML content from $stream")
50 ## Auto-detect feed type based on first element. This is prone
51 ## to breakage, but then again we don't want to parse the whole
53 my($tag) = $xml =~ /<([a-zA-Z]\S+)/s;
55 if ($tag eq 'rss' || $tag eq 'RDF') {
56 require XML::Feed::RSS;
57 bless $feed, 'XML::Feed::RSS';
58 } elsif ($tag eq 'feed') {
59 require XML::Feed::Atom;
60 bless $feed, 'XML::Feed::Atom';
62 return $class->error("Cannot detect feed type");
64 $feed->init_string($xml) or return;
71 my $ua = LWP::UserAgent->new;
72 my $req = HTTP::Request->new(GET => $uri);
73 my $res = $ua->request($req);
74 return unless $res->is_success;
76 my %is_feed = map { $_ => 1 } @{ FEED_MIME_TYPES() };
77 my $ct = $res->content_type;
80 } elsif ($ct eq 'text/html' || $ct eq 'application/xhtml+xml') {
82 my $find_links = sub {
85 return unless $attr->{rel};
86 my %rel = map { $_ => 1 } split /\s+/, lc($attr->{rel});
87 (my $type = lc $attr->{type}) =~ s/^\s*//;
89 push @feeds, URI->new_abs($attr->{href}, $base_uri)->as_string
91 ($rel{alternate} || $rel{'service.feed'});
92 } elsif ($tag eq 'base') {
93 $base_uri = $attr->{href};
96 my $p = HTML::Parser->new(api_version => 3,
97 start_h => [ $find_links, "tagname, attr" ]);
98 $p->parse($res->content);
113 sub tagline { $_[0]->description }
114 sub items { $_[0]->entries }
121 XML::Feed - Syndication feed parser and auto-discovery
126 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
127 or die XML::Feed->errstr;
128 print $feed->title, "\n";
129 for my $entry ($feed->entries) {
132 ## Find all of the syndication feeds on a given page, using
134 my @feeds = XML::Feed->find_feeds('http://example.com/');
138 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
139 also implements feed auto-discovery for finding feeds, given a URI.
141 I<XML::Feed> supports the following syndication feed formats:
155 The goal of I<XML::Feed> is to provide a unified API for parsing and using
156 the various syndication formats. The different flavors of RSS and Atom
157 handle data in different ways: date handling; summaries and content;
158 escaping and quoting; etc. This module attempts to remove those differences
159 by providing a wrapper around the formats and the classes implementing
160 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
161 handled differently in each of the above formats. To provide a unified API for
162 date handling, I<XML::Feed> converts all date formats transparently into
163 I<DateTime> objects, which it then returns to the caller.
167 =head2 XML::Feed->parse($stream)
169 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
170 one of the following:
174 =item * Scalar reference
176 A reference to string containing the XML body of the feed.
180 An open filehandle from which the feed XML will be read.
184 The name of a file containing the feed XML.
188 A URI from which the feed XML will be retrieved.
192 =head2 XML::Feed->find_feeds($uri)
194 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
195 from that page (using I<E<lt>linkE<gt>> tags).
197 Returns a list of feed URIs.
201 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
205 The title of the feed/channel.
209 The permalink of the feed/channel.
211 =head2 $feed->tagline
213 The description or tagline of the feed/channel.
215 =head2 $feed->description
217 Alias for I<$feed-E<gt>tagline>.
219 =head2 $feed->language
221 The language of the feed.
223 =head2 $feed->copyright
225 The copyright notice of the feed.
227 =head2 $feed->modified
229 A I<DateTime> object representing the last-modified date of the feed.
231 =head2 $feed->generator
233 The generator of the feed.
235 =head2 $feed->entries
237 A list of the entries/items in the feed. Returns an array containing
238 I<XML::Feed::Entry> objects.
242 I<XML::Feed> is free software; you may redistribute it and/or modify it
243 under the same terms as Perl itself.
245 =head1 AUTHOR & COPYRIGHT
247 Except where otherwise noted, I<XML::Feed> is Copyright 2004 Benjamin
248 Trott, cpan@stupidfool.org. All rights reserved.