Allow specification of the parsing format.
[catagits/XML-Feed.git] / lib / XML / Feed.pm
1 # $Id: Feed.pm 1958 2006-08-14 05:31:27Z btrott $
2
3 package XML::Feed;
4 use strict;
5
6 use base qw( Class::ErrorHandler );
7 use Feed::Find;
8 use URI::Fetch;
9 use Carp;
10
11 our $VERSION = '0.12';
12
13 sub new {
14     my $class = shift;
15     my($format) = @_;
16     $format ||= 'Atom';
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);
22     $feed;
23 }
24
25 sub init_empty { 1 }
26
27 sub parse {
28     my $class = shift;
29     my($stream, $specified_format) = @_;
30     return $class->error("Stream parameter is required") unless $stream;
31     my $feed = bless {}, $class;
32     my $xml = '';
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();
38         $xml = $res->content;
39     } elsif (ref($stream) eq 'SCALAR') {
40         $xml = $$stream;
41     } elsif (ref($stream)) {
42         while (read($stream, my($chunk), 8192)) {
43             $xml .= $chunk;
44         }
45     } else {
46         open my $fh, $stream
47             or return $class->error("Can't open $stream: $!");
48         while (read $fh, my($chunk), 8192) {
49             $xml .= $chunk;
50         }
51         close $fh;
52     }
53     return $class->error("Can't get feed XML content from $stream")
54         unless $xml;
55     my $format;
56     if ($specified_format) {
57         $format = $specified_format;
58     } else {
59         $feed->identify_format(\$xml) or return $class->error($feed->errstr);
60     }
61
62     my $format_class = join '::', __PACKAGE__, $format;
63     eval "use $format_class";
64     return $class->error("Unsupported format $format: $@") if $@;
65     bless $feed, $format_class;
66     $feed->init_string(\$xml) or return $class->error($feed->errstr);
67     $feed;
68 }
69
70 sub identify_format {
71     my $feed = shift;
72     my($xml) = @_;
73     ## Auto-detect feed type based on first element. This is prone
74     ## to breakage, but then again we don't want to parse the whole
75     ## feed ourselves.
76     my $tag;
77     while ($$xml =~ /<(\S+)/sg) {
78         (my $t = $1) =~ tr/a-zA-Z0-9:\-\?!//cd;
79         my $first = substr $t, 0, 1;
80         $tag = $t, last unless $first eq '?' || $first eq '!';
81     }
82     return $feed->error("Cannot find first element") unless $tag;
83     $tag =~ s/^.*://;
84     if ($tag eq 'rss' || $tag eq 'RDF') {
85         return 'RSS';
86     } elsif ($tag eq 'feed') {
87         return 'Atom';
88     } else {
89         return $feed->error("Cannot detect feed type");
90     }
91 }
92
93 sub find_feeds {
94     my $class = shift;
95     my($uri) = @_;
96     my @feeds = Feed::Find->find($uri)
97         or return $class->error(Feed::Find->errstr);
98     @feeds;
99 }
100
101 sub convert {
102     my $feed = shift;
103     my($format) = @_;
104     my $new = __PACKAGE__->new($format);
105     for my $field (qw( title link description language author copyright modified generator )) {
106         my $val = $feed->$field();
107         next unless defined $val;
108         $new->$field($val);
109     }
110     for my $entry ($feed->entries) {
111         $new->add_entry($entry->convert($format));
112     }
113     $new;
114 }
115
116 sub splice {
117     my $feed = shift;
118     my($other) = @_;
119     my %ids = map { $_->id => 1 } $feed->entries;
120     for my $entry ($other->entries) {
121         $feed->add_entry($entry) unless $ids{$entry->id}++;
122     }
123 }
124
125 sub format;
126 sub title;
127 sub link;
128 sub description;
129 sub language;
130 sub author;
131 sub copyright;
132 sub modified;
133 sub generator;
134 sub add_entry;
135 sub entries;
136 sub as_xml;
137
138 sub tagline { shift->description(@_) }
139 sub items   { $_[0]->entries     }
140
141 1;
142 __END__
143
144 =head1 NAME
145
146 XML::Feed - Syndication feed parser and auto-discovery
147
148 =head1 SYNOPSIS
149
150     use XML::Feed;
151     my $feed = XML::Feed->parse(URI->new('http://example.com/atom.xml'))
152         or die XML::Feed->errstr;
153     print $feed->title, "\n";
154     for my $entry ($feed->entries) {
155     }
156
157     ## Find all of the syndication feeds on a given page, using
158     ## auto-discovery.
159     my @feeds = XML::Feed->find_feeds('http://example.com/');
160
161 =head1 DESCRIPTION
162
163 I<XML::Feed> is a syndication feed parser for both RSS and Atom feeds. It
164 also implements feed auto-discovery for finding feeds, given a URI.
165
166 I<XML::Feed> supports the following syndication feed formats:
167
168 =over 4
169
170 =item * RSS 0.91
171
172 =item * RSS 1.0
173
174 =item * RSS 2.0
175
176 =item * Atom
177
178 =back
179
180 The goal of I<XML::Feed> is to provide a unified API for parsing and using
181 the various syndication formats. The different flavors of RSS and Atom
182 handle data in different ways: date handling; summaries and content;
183 escaping and quoting; etc. This module attempts to remove those differences
184 by providing a wrapper around the formats and the classes implementing
185 those formats (I<XML::RSS> and I<XML::Atom::Feed>). For example, dates are
186 handled differently in each of the above formats. To provide a unified API for
187 date handling, I<XML::Feed> converts all date formats transparently into
188 I<DateTime> objects, which it then returns to the caller.
189
190 =head1 USAGE
191
192 =head2 XML::Feed->new($format)
193
194 Creates a new empty I<XML::Feed> object using the format I<$format>.
195
196 =head2 XML::Feed->parse($stream)
197
198 =head2 XML::Feed->parse($stream, $format)
199
200 Parses a syndication feed identified by I<$stream>. I<$stream> can be any
201 one of the following:
202
203 =over 4
204
205 =item * Scalar reference
206
207 A reference to string containing the XML body of the feed.
208
209 =item * Filehandle
210
211 An open filehandle from which the feed XML will be read.
212
213 =item * File name
214
215 The name of a file containing the feed XML.
216
217 =item * URI object
218
219 A URI from which the feed XML will be retrieved.
220
221 =back
222
223 C<$format> allows you to override format guessing.
224
225 =head2 XML::Feed->find_feeds($uri)
226
227 Given a URI I<$uri>, use auto-discovery to find all of the feeds linked
228 from that page (using I<E<lt>linkE<gt>> tags).
229
230 Returns a list of feed URIs.
231
232 =head2 $feed->convert($format)
233
234 Converts the I<XML::Feed> object into the I<$format> format, and returns
235 the new object.
236
237 =head2 $feed->splice($other_feed)
238
239 Splices in all of the entries from the feed I<$other_feed> into I<$feed>,
240 skipping posts that are already in I<$feed>.
241
242 =head2 $feed->format
243
244 Returns the format of the feed (C<Atom>, or some version of C<RSS>).
245
246 =head2 $feed->title([ $title ])
247
248 The title of the feed/channel.
249
250 =head2 $feed->link([ $uri ])
251
252 The permalink of the feed/channel.
253
254 =head2 $feed->tagline([ $tagline ])
255
256 The description or tagline of the feed/channel.
257
258 =head2 $feed->description([ $description ])
259
260 Alias for I<$feed-E<gt>tagline>.
261
262 =head2 $feed->author([ $author ])
263
264 The author of the feed/channel.
265
266 =head2 $feed->language([ $language ])
267
268 The language of the feed.
269
270 =head2 $feed->copyright([ $copyright ])
271
272 The copyright notice of the feed.
273
274 =head2 $feed->modified([ $modified ])
275
276 A I<DateTime> object representing the last-modified date of the feed.
277
278 If present, I<$modified> should be a I<DateTime> object.
279
280 =head2 $feed->generator([ $generator ])
281
282 The generator of the feed.
283
284 =head2 $feed->entries
285
286 A list of the entries/items in the feed. Returns an array containing
287 I<XML::Feed::Entry> objects.
288
289 =head2 $feed->add_entry($entry)
290
291 Adds an entry to the feed. I<$entry> should be an I<XML::Feed::Entry>
292 object in the correct format for the feed.
293
294 =head2 $feed->as_xml
295
296 Returns an XML representation of the feed, in the format determined by
297 the current format of the I<$feed> object.
298
299 =head1 PACKAGE VARIABLES
300
301 =over 4
302
303 =item C<$XML::Feed::RSS::PREFERRED_PARSER>
304
305 If you want to use another RSS parser class than XML::RSS (default), you can
306 change the class by setting C<$PREFERRED_PARSER> variable in XML::Feed::RSS
307 package.
308
309     $XML::Feed::RSS::PREFERRED_PARSER = "XML::RSS::LibXML";
310
311 B<Note:> this will only work for parsing feeds, not creating feeds.
312
313 =back
314
315 =head1 LICENSE
316
317 I<XML::Feed> is free software; you may redistribute it and/or modify it
318 under the same terms as Perl itself.
319
320 =head1 AUTHOR & COPYRIGHT
321
322 Except where otherwise noted, I<XML::Feed> is Copyright 2004-2005
323 Six Apart, cpan@sixapart.com. All rights reserved.
324
325 =cut