root/trunk/plagger/lib/Plagger/Plugin/Aggregator/Simple.pm

Revision 1330 (checked in by miyagawa, 14 years ago)
  • Added new plugin Filter::ExtractAuthorName? to extract author name from RSS 2.0 <author> field
  • Fixed Aggregator::Simple bug to deal with single MediaRSS (magic) element
  • Property svn:keywords set to Id Revision
Line 
1 package Plagger::Plugin::Aggregator::Simple;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use Feed::Find;
6 use Plagger::Enclosure;
7 use Plagger::UserAgent;
8 use List::Util qw(first);
9 use UNIVERSAL::require;
10 use URI;
11 use XML::Feed;
12 use XML::Feed::RSS;
13
14 $XML::Feed::RSS::PREFERRED_PARSER = first { $_->require } qw( XML::RSS::Liberal XML::RSS::LibXML XML::RSS );
15
16 sub register {
17     my($self, $context) = @_;
18     $context->register_hook(
19         $self,
20         'customfeed.handle'  => \&aggregate,
21     );
22 }
23
24 sub aggregate {
25     my($self, $context, $args) = @_;
26
27     my $url = $args->{feed}->url;
28     my $res = $self->fetch_content($url) or return;
29
30     my $content_type = eval { $res->content_type } ||
31                        $res->http_response->content_type ||
32                        "text/xml";
33
34     $content_type =~ s/;.*$//; # strip charset= cruft
35
36     my $content = $res->content;
37     if ( $Feed::Find::IsFeed{$content_type} || $self->looks_like_feed(\$content) ) {
38         $self->handle_feed($url, \$content, $args->{feed});
39     } else {
40         $content = Plagger::Util::decode_content($res);
41         my @feeds = Feed::Find->find_in_html(\$content, $url);
42         if (@feeds) {
43             $url = $feeds[0];
44             $res = $self->fetch_content($url) or return;
45             $self->handle_feed($url, \$res->content, $args->{feed});
46         } else {
47             return;
48         }
49     }
50
51     return 1;
52 }
53
54 sub looks_like_feed {
55     my($self, $content_ref) = @_;
56     $$content_ref =~ m!<rss |<rdf:RDF\s+.*?xmlns="http://purl\.org/rss|<feed\s+xmlns="!s;
57 }
58
59 sub fetch_content {
60     my($self, $url) = @_;
61
62     my $context = Plagger->context;
63     $context->log(info => "Fetch $url");
64
65     my $agent = Plagger::UserAgent->new;
66        $agent->parse_head(0);
67     my $response = $agent->fetch($url, $self);
68
69     if ($response->is_error) {
70         $context->log(error => "GET $url failed: " .
71                       $response->http_status . " " .
72                       $response->http_response->message);
73         return;
74     }
75
76     # TODO: handle 301 Moved Permenently and 410 Gone
77     $context->log(debug => $response->status . ": $url");
78
79     $response;
80 }
81
82 sub handle_feed {
83     my($self, $url, $xml_ref, $feed) = @_;
84
85     my $context = Plagger->context;
86
87     my $args = { content => $$xml_ref };
88     $context->run_hook('aggregator.filter.feed', $args);
89
90     # override XML::LibXML with Liberal
91     my $sweeper; # XML::Liberal >= 0.13
92
93     eval { require XML::Liberal };
94     if (!$@ && $XML::Liberal::VERSION >= 0.10) {
95         $sweeper = XML::Liberal->globally_override('LibXML');
96     }
97
98     my $remote = eval { XML::Feed->parse(\$args->{content}) };
99
100     unless ($remote) {
101         $context->log(error => "Parsing $url failed. " . ($@ || XML::Feed->errstr));
102         return;
103     }
104
105     $feed ||= Plagger::Feed->new;
106     $feed->title(_u($remote->title)) unless defined $feed->title;
107     $feed->url($url);
108     $feed->link($remote->link);
109     $feed->description(_u($remote->tagline)); # xxx should support Atom 1.0
110     $feed->language($remote->language);
111     $feed->author(_u($remote->author));
112     $feed->updated($remote->modified);
113     $feed->source_xml($$xml_ref);
114
115     if ($remote->format eq 'Atom') {
116         $feed->id( $remote->{atom}->id );
117     }
118
119     if ($remote->format =~ /^RSS/) {
120         $feed->image( $remote->{rss}->image )
121             if $remote->{rss}->image;
122     } elsif ($remote->format eq 'Atom') {
123         $feed->image({ url => $remote->{atom}->logo })
124             if $remote->{atom}->logo;
125     }
126
127     for my $e ($remote->entries) {
128         my $entry = Plagger::Entry->new;
129         $entry->title(_u($e->title));
130         $entry->author(_u($e->author));
131
132         my $category = $e->category;
133            $category = [ $category ] if $category && (!ref($category) || ref($category) ne 'ARRAY');
134         $entry->tags([ map _u($_), @$category ]) if $category;
135
136         # XXX XML::Feed doesn't support extracting atom:category yet
137         if ($remote->format eq 'Atom' && $e->{entry}->can('categories')) {
138             my @categories = $e->{entry}->categories;
139             for my $cat (@categories) {
140                 $entry->add_tag( $cat->label || $cat->term );
141             }
142         }
143
144         my $date = eval { $e->issued } || eval { $e->modified };
145         $entry->date( Plagger::Date->rebless($date) ) if $date;
146
147         # xxx nasty hack. We should remove this once XML::Atom or XML::Feed is fixed
148         if (!$entry->date && $remote->format eq 'Atom' && $e->{entry}->version eq '1.0') {
149             if ( $e->{entry}->published ) {
150                 my $dt = XML::Atom::Util::iso2dt( $e->{entry}->published );
151                 $entry->date( Plagger::Date->rebless($dt) ) if $dt;
152             }
153         }
154
155         $entry->link($e->link);
156         $entry->feed_link($feed->link);
157         $entry->id($e->id);
158         $entry->body(_u($e->content->body || $e->summary->body));
159
160         # enclosure support, to be added to XML::Feed
161         if ($remote->format =~ /^RSS / and my $encls = $e->{entry}->{enclosure}) {
162             # some RSS feeds contain multiple enclosures, and we support them
163             $encls = [ $encls ] unless ref $encls eq 'ARRAY';
164
165             for my $encl (@$encls) {
166                 my $enclosure = Plagger::Enclosure->new;
167                 $enclosure->url( URI->new($encl->{url}) );
168                 $enclosure->length($encl->{length});
169                 $enclosure->auto_set_type($encl->{type});
170                 $entry->add_enclosure($enclosure);
171             }
172         } elsif ($remote->format eq 'Atom') {
173             for my $link ( grep { defined $_->rel && $_->rel eq 'enclosure' } $e->{entry}->link ) {
174                 my $enclosure = Plagger::Enclosure->new;
175                 $enclosure->url( URI->new($link->href) );
176                 $enclosure->length($link->length);
177                 $enclosure->auto_set_type($link->type);
178                 $entry->add_enclosure($enclosure);
179             }
180         }
181
182         # TODO: move MediaRSS, Hatena, iTunes and those specific parser to be subclassed
183
184         # Media RSS
185         my $media_ns = "http://search.yahoo.com/mrss";
186         my $media = $e->{entry}->{$media_ns}->{group} || $e->{entry};
187         my $content = $media->{$media_ns}->{content} || [];
188            $content = [ $content ] unless ref $content && ref $content eq 'ARRAY';
189
190         for my $media_content (@{$content}) {
191             my $enclosure = Plagger::Enclosure->new;
192             $enclosure->url( URI->new($media_content->{url}) );
193             $enclosure->auto_set_type($media_content->{type});
194             $entry->add_enclosure($enclosure);
195         }
196
197         if (my $thumbnail = $media->{$media_ns}->{thumbnail}) {
198             $entry->icon({
199                 url   => $thumbnail->{url},
200                 width => $thumbnail->{width},
201                 height => $thumbnail->{height},
202             });
203         }
204
205         # Hatena Image extensions
206         my $hatena = $e->{entry}->{"http://www.hatena.ne.jp/info/xmlns#"} || {};
207         if ($hatena->{imageurl}) {
208             my $enclosure = Plagger::Enclosure->new;
209             $enclosure->url($hatena->{imageurl});
210             $enclosure->auto_set_type;
211             $entry->add_enclosure($enclosure);
212         }
213
214         if ($hatena->{imageurlsmall}) {
215             $entry->icon({ url   => $hatena->{imageurlsmall} });
216         }
217
218         # Apple photocast feed
219         my $apple = $e->{entry}->{"http://www.apple.com/ilife/wallpapers"} || {};
220         if ($apple->{image}) {
221             my $enclosure = Plagger::Enclosure->new;
222             $enclosure->url( URI->new($apple->{image}) );
223             $enclosure->auto_set_type;
224             $entry->add_enclosure($enclosure);
225         }
226         if ($apple->{thumbnail}) {
227             $entry->icon({ url => $apple->{thumbnail} });
228         }
229
230         my $args = {
231             entry      => $entry,
232             feed       => $feed,
233             orig_entry => $e,
234             orig_feed  => $remote,
235         };
236         $context->run_hook('aggregator.entry.fixup', $args);
237
238         $feed->add_entry($entry);
239     }
240
241     $context->log(info => "Aggregate $url success: " . $feed->count . " entries.");
242     $context->update->add($feed);
243 }
244
245 sub _u {
246     my $str = shift;
247     Encode::_utf8_on($str);
248     $str;
249 }
250
251 1;
252
253 __END__
254
255 =head1 NAME
256
257 Plagger::Plugin::Aggregator::Simple - Dumb simple aggregator
258
259 =head1 SYNOPSIS
260
261   - module: Aggregator::Simple
262
263 =head1 DESCRIPTION
264
265 This plugin implements a Plagger dumb aggregator. It crawls
266 subscription sequentially and parses XML feeds using L<XML::Feed>
267 module.
268
269 It can be also used as a base class for custom aggregators. See
270 L<Plagger::Plugin::Aggregator::Xango> for example.
271
272 =head1 AUTHOR
273
274 Tatsuhiko Miyagawa
275
276 =head1 SEE ALSO
277
278 L<Plagger>, L<XML::Feed>, L<XML::RSS::LibXML>
279
280 =cut
Note: See TracBrowser for help on using the browser.