1 # $Id: Feed.pm,v 1.8 2004/07/29 16:44:18 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
54 while ($xml =~ /<(\S+)/sg) {
55 (my $t = $1) =~ tr/a-zA-Z0-9:\-\?//cd;
56 $tag = $t, last unless substr($t, 0, 1) eq '?';
58 return $class->error("Cannot find first element") unless $tag;
60 if ($tag eq 'rss' || $tag eq 'RDF') {
61 require XML::Feed::RSS;
62 bless $feed, 'XML::Feed::RSS';
63 } elsif ($tag eq 'feed') {
64 require XML::Feed::Atom;
65 bless $feed, 'XML::Feed::Atom';
67 return $class->error("Cannot detect feed type");
69 $feed->init_string($xml) or return;
76 my $ua = LWP::UserAgent->new;
77 my $req = HTTP::Request->new(GET => $uri);
78 my $res = $ua->request($req);
79 return unless $res->is_success;
81 my %is_feed = map { $_ => 1 } @{ FEED_MIME_TYPES() };
82 my $ct = $res->content_type;
85 } elsif ($ct eq 'text/html' || $ct eq 'application/xhtml+xml') {
87 my $find_links = sub {
90 return unless $attr->{rel};
91 my %rel = map { $_ => 1 } split /\s+/, lc($attr->{rel});
92 (my $type = lc $attr->{type}) =~ s/^\s*//;
94 push @feeds, URI->new_abs($attr->{href}, $base_uri)->as_string
96 ($rel{alternate} || $rel{'service.feed'});
97 } elsif ($tag eq 'base') {
98 $base_uri = $attr->{href};
101 my $p = HTML::Parser->new(api_version => 3,
102 start_h => [ $find_links, "tagname, attr" ]);
103 $p->parse($res->content);
118 sub tagline { $_[0]->description }
119 sub items { $_[0]->entries }
126 XML::Feed - Syndication feed parser and auto-discovery
131 my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
132 or die XML::Feed->errstr;
133 print $feed->title, "\n";
134 for my $entry ($feed->entries) {
137 ## Find all of the syndication feeds on a given page, using
139 my @feeds = XML::Feed->find_feeds('http://example.com/');
143 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
144 also implements feed auto-discovery for finding feeds, given a URI.
146 I<XML::Feed> supports the following syndication feed formats:
160 The goal of I<XML::Feed> is to provide a unified API for parsing and using
161 the various syndication formats. The different flavors of RSS and Atom
162 handle data in different ways: date handling; summaries and content;
163 escaping and quoting; etc. This module attempts to remove those differences
164 by providing a wrapper around the formats and the classes implementing
165 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
166 handled differently in each of the above formats. To provide a unified API for
167 date handling, I<XML::Feed> converts all date formats transparently into
168 I<DateTime> objects, which it then returns to the caller.
172 =head2 XML::Feed->parse($stream)
174 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
175 one of the following:
179 =item * Scalar reference
181 A reference to string containing the XML body of the feed.
185 An open filehandle from which the feed XML will be read.
189 The name of a file containing the feed XML.
193 A URI from which the feed XML will be retrieved.
197 =head2 XML::Feed->find_feeds($uri)
199 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
200 from that page (using I<E<lt>linkE<gt>> tags).
202 Returns a list of feed URIs.
206 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
210 The title of the feed/channel.
214 The permalink of the feed/channel.
216 =head2 $feed->tagline
218 The description or tagline of the feed/channel.
220 =head2 $feed->description
222 Alias for I<$feed-E<gt>tagline>.
224 =head2 $feed->language
226 The language of the feed.
228 =head2 $feed->copyright
230 The copyright notice of the feed.
232 =head2 $feed->modified
234 A I<DateTime> object representing the last-modified date of the feed.
236 =head2 $feed->generator
238 The generator of the feed.
240 =head2 $feed->entries
242 A list of the entries/items in the feed. Returns an array containing
243 I<XML::Feed::Entry> objects.
247 I<XML::Feed> is free software; you may redistribute it and/or modify it
248 under the same terms as Perl itself.
250 =head1 AUTHOR & COPYRIGHT
252 Except where otherwise noted, I<XML::Feed> is Copyright 2004 Benjamin
253 Trott, cpan@stupidfool.org. All rights reserved.