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

Revision 169 (checked in by miyagawa, 14 years ago)

Fixed Aggregator::Simple to handle multiple categories set to tags

  • 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 Plagger::UserAgent;
6 use URI;
7 use XML::Feed;
8 use XML::Feed::RSS;
9
10 $XML::Feed::RSS::PREFERRED_PARSER = 'XML::RSS::LibXML';
11
12 sub register {
13     my($self, $context) = @_;
14     $context->register_hook(
15         $self,
16         'aggregator.aggregate.feed'  => \&aggregate,
17     );
18 }
19
20 sub aggregate {
21     my($self, $context, $args) = @_;
22
23     my $url = $args->{feed}->url;
24     $context->log(info => "Fetch $url");
25
26     my $agent = Plagger::UserAgent->new;
27     my $response = $agent->get($url);
28
29     unless ($response->is_success) {
30         $context->log(error => "GET $url failed: " . $response->status_line);
31         return;
32     }
33
34     $self->handle_feed($url, \$response->content);
35 }
36
37 sub handle_feed {
38     my($self, $url, $xml_ref) = @_;
39
40     my $context = Plagger->context;
41     my $remote = eval { XML::Feed->parse($xml_ref) };
42
43     unless ($remote) {
44         $context->log(error => "Parsing $url failed. " . XML::Feed->errstr);
45         next;
46     }
47
48     my $feed = Plagger::Feed->new;
49     $feed->title($remote->title);
50     $feed->url($url);
51     $feed->link($remote->link);
52     $feed->description($remote->tagline);
53     $feed->language($remote->language);
54     $feed->author($remote->author);
55     $feed->updated($remote->modified);
56
57     if ($remote->format eq 'Atom') {
58         $feed->id( $remote->{atom}->id );
59     }
60
61     if ($remote->format =~ /^RSS/) {
62         $feed->image( $remote->{rss}->image )
63             if $remote->{rss}->image;
64     } elsif ($remote->format eq 'Atom') {
65         $feed->image({ url => $remote->{atom}->logo })
66             if $remote->{atom}->logo;
67     }
68
69     for my $e ($remote->entries) {
70         my $entry = Plagger::Entry->new;
71         $entry->title($e->title);
72         $entry->author($e->author);
73
74         my $category = $e->category;
75            $category = [ $category ] if $category && !ref($category);
76         $entry->tags($category) if $category;
77
78         $entry->date( Plagger::Date->rebless($e->issued) )
79             if eval { $e->issued };
80         $entry->link($e->link);
81         $entry->id($e->id);
82         $entry->body($e->content->body);
83
84         $feed->add_entry($entry);
85     }
86
87     $context->log(info => "Aggregate $url success: " . $feed->count . " entries.");
88     $context->update->add($feed);
89 }
90
91 1;
Note: See TracBrowser for help on using the browser.