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

Revision 177 (checked in by miyagawa, 14 years ago)
  • Fixed tDiaryComment to strictly match with tDiary's feed, and strips comment entry rather than rating it
  • Added $feed->source_xml to get the raw XML
  • 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     $feed->source_xml($$xml_ref);
57
58     if ($remote->format eq 'Atom') {
59         $feed->id( $remote->{atom}->id );
60     }
61
62     if ($remote->format =~ /^RSS/) {
63         $feed->image( $remote->{rss}->image )
64             if $remote->{rss}->image;
65     } elsif ($remote->format eq 'Atom') {
66         $feed->image({ url => $remote->{atom}->logo })
67             if $remote->{atom}->logo;
68     }
69
70     for my $e ($remote->entries) {
71         my $entry = Plagger::Entry->new;
72         $entry->title($e->title);
73         $entry->author($e->author);
74
75         my $category = $e->category;
76            $category = [ $category ] if $category && !ref($category);
77         $entry->tags($category) if $category;
78
79         $entry->date( Plagger::Date->rebless($e->issued) )
80             if eval { $e->issued };
81         $entry->link($e->link);
82         $entry->id($e->id);
83         $entry->body($e->content->body);
84
85         $feed->add_entry($entry);
86     }
87
88     $context->log(info => "Aggregate $url success: " . $feed->count . " entries.");
89     $context->update->add($feed);
90 }
91
92 1;
Note: See TracBrowser for help on using the browser.