root/trunk/plagger/lib/Plagger/Plugin/Subscription/PingServer.pm

Revision 452 (checked in by ko, 14 years ago)

skip for duplicate url

Line 
1 package Plagger::Plugin::Subscription::PingServer;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use HTML::RSSAutodiscovery;
6 use List::Util qw(first);
7 use Plagger::UserAgent;
8 use XML::LibXML;
9
10 sub register {
11     my($self, $context) = @_;
12
13     $context->register_hook(
14         $self,
15         'subscription.load' => \&load,
16     );
17 }
18
19 sub load {
20     my($self, $context) = @_;
21
22     my $servers = $self->conf->{servers};
23        $servers = [ $servers ] unless ref $servers;
24
25     my %cache;
26     for my $server (@{ $servers }) {
27         my $agent    = Plagger::UserAgent->new;
28         my $response = $agent->fetch($server->{url});
29         if ($response->is_error) {
30             $context->log(error => "GET $server->{url} failed: " .
31                           $response->http_status . " " .
32                           $response->http_response->message);
33             return;
34         }
35
36         my $link;
37         my $doc = XML::LibXML->new->parse_string($response->content);
38         my $items = $self->conf->{fetch_items} || 20;
39         for my $node ( $doc->findnodes('/weblogUpdates/weblog')) {
40             my $url = first { $_ } $node->findvalue('@url');
41             next unless $url;
42             next if $cache{$url}++;
43             $context->log(debug => "get url: $url");
44
45             my $feed = Plagger::Feed->new;
46             $feed->url($url);
47             $feed->link($url);
48             $feed->title(first { $_ } $node->findvalue('@name') || $url);
49
50             last if $context->subscription->add($feed) >= $items;
51         }
52     }
53 }
54
55 1;
56
57 __END__
58
59 =head1 NAME
60
61 Plagger::Plugin::Subscription::PingServer - Subscription from ping servers
62
63 =head1 SYNOPSIS
64
65     - module: Subscription::PingServer
66       config:
67         fetch_items: 20
68         servers:
69           - url: http://ping.bloggers.jp/changes.xml?last=100
70
71 =head1 DESCRIPTION
72
73 This plugin allows you to pingserver your subscription in C<config.yaml>.
74
75 =head1 AUTHOR
76
77 Kazuhiro Osawa
78
79 =head1 SEE ALSO
80
81 L<Plagger>
82
83 =cut
Note: See TracBrowser for help on using the browser.