PlaggerでPubSubHubbub

PubSubHubbubでフィード更新を通知するのが流行りだしているので、Plaggerプラグインをいぢってみました。
まず、フィードに

  <link rel="hub" href="http://pubsubhubbub.appspot.com/"/>
  <link rel="self" href="http://foo.bar/baz.xml"/>

というリンクを入れる必要があるので、Publish::Feedに

--- Feed.pm.orig	2006-12-05 16:01:29.000000000 +0900
+++ Feed.pm	2009-08-11 21:53:36.000000000 +0900
@@ -62,4 +62,12 @@
     }
 
+    if ($self->conf->{hub} && $feed_format eq 'Atom') {
+        $feed->{atom}->add_link({ rel => 'hub', href => $self->conf->{hub}, });
+    }
+
+    if ($self->conf->{self} && $feed_format eq 'Atom') {
+        $feed->{atom}->add_link({ rel => 'self', href => $self->conf->{self}, });
+    }
+
     # add entry
     for my $e ($f->entries) {

というのを追加しました(すみませんAtomしか考えてません)。
更新時のPOSTによる通知は、cpanのNet::PubSubHubbub::Publisherを使って、以下のNotify::PubSubHubbubでやります:

package Plagger::Plugin::Notify::PubSubHubbub;

use strict;
use base qw( Plagger::Plugin );

use Net::PubSubHubbub::Publisher;

sub register {
    my ( $self, $context ) = @_;
    $context->register_hook(
        $self,
        'publish.entry' => \&update,
        'publish.finalize' => \&finalize,
    );
    $self->{count} = 0;
}

sub update {
    my($self, $context, $args) = @_;
    $self->{count}++ if $args->{feed}->count;
}

sub finalize {
    my($self, $context, $args) = @_;

    if ($self->{count}) {
	if ((my $hub = $self->conf->{hub}) && (my $s = $self->conf->{self})) {
	    $context->log(info => "Notifying " . $s . " to " . $hub);
	    my $pub = Net::PubSubHubbub::Publisher->new(hub => $hub);
	    $pub->publish_update($s) or
		die "Ping failed: " . $pub->last_response->status_line;
	    $context->log(info => "Status: " . $pub->last_response->status_line);
	}
    }
}
1;

使い方はこう(hubとselfが2つずつあるのがなんですが):

  - module: Publish::Feed
    config:
      filename: baz.xml
      hub: http://pubsubhubbub.appspot.com/
      self: http://foo.bar/baz.xml
  - module: Notify::PubSubHubbub
    config:
      hub: http://pubsubhubbub.appspot.com/
      self: http://foo.bar/baz.xml

これで作ったフィードをFriendFeedに登録してみたところ、Plagger終了直後に更新がFriendFeedに反映されているようです。
ちなみにフィードをPOSTしても、誰もそれをsubscribeしていないと、Publisher Diagnosticsで見てもCould not find any recordと言われます(しばらくこれで悩みました)。

次はGoogle Readerがsubscribe対応してくれるとうれしいんですが(今はin-progress)。