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

Revision 123 (checked in by miyagawa, 15 years ago)

Oops, [122] broke the FeedBurnerPermalink? plugin. Revert it and add $e->id_safe

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