atode.ccクローン用CustomFeed::Delicious

Plaggerではあとで読むそれplaggerで出来るわけですが、del.icio.usのtoreadタグをSubsctiption::Configで取得すると、toreadタグがどんどん増えて行きます。

増えるだけだったら特に問題はないのですが、Subscription::Configで取得するとrssの全てを取得してParseしてします(それもFilter::EntryFullTextの処理付)。

ということで、DeliciousのAPIを使用して取得後に消去するプラグインを書いて見ました。


Subscriptionとどちらがいいのか迷いましたが、とりあえずCustomFeedにして見ました。
miyagawaさんに確認してSubscriptionにしました。とは言っても最初からregisterがsubscriptionになっていました。パクリすぎて勘違いしてました。

あと、無理やりでもCPANを使用するというPlaggerのポリシーに反して、Net::Deliciousを使わずに書いてしまいました。反省(w。

 package Plagger::Plugin::Subscription::Delicious;
use strict;
use base qw( Plagger::Plugin );

use List::Util qw(first);
use Plagger::UserAgent;
use XML::LibXML;
use URI;

sub register {
my($self, $context) = @_;
$context->register_hook(
$self,
'subscription.load' => \&load,
);
}

sub load {
my($self, $context) = @_;
my $feed = Plagger::Feed->new;
$feed->aggregator(sub {$self->aggregate(@_) });
$context->subscription->add($feed);
}

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

my %query;
$query{tag} = $self->conf->{tags} || 'toread';
my $response = $self->delicious_url($context, 'all', %query);

my %cache;
my $link;
my $doc = XML::LibXML->new->parse_string($response->content);
my $feed = Plagger::Feed->new;
$feed->type('customfeed.delicious');
$feed->title('Delicious');
my $feeds = 0;
for my $node ( $doc->findnodes('/posts/post')) {
my $url = first { $_ } $node->findvalue('@href');
next unless $url;
next if $cache{$url}++;
$context->log(debug => "get url: $url");
my $entry = Plagger::Entry->new;
$entry->link($url);
$entry->title(first { $_ } $node->findvalue('@description') || $url);
$feed->add_entry($entry);
if($self->conf->{delete}){
my %query;
$query{url} = $url;
my $response = $self->delicious_url($context, 'delete', %query);
$context->log(debug => "del url: $url");
}
$feeds++;
}
$context->update->add($feed) if $feeds != 0;
}

sub delicious_url {
my($self, $context, $mode, %query) = @_;
my $cfg = $self->conf;
my $auth = $cfg->{username} . ':' . $cfg->{password};
my $uri = URI->new(sprintf('http://%s@del.icio.us/api/posts/%s',$auth,$mode));
$uri->query_form(%query);
my $url = HTML::Entities::encode($uri->as_string);
my $ua = Plagger::UserAgent->new;
my $res = $ua->fetch($url);
if ($res->is_error) {
my $message;
$message = 'GET' if $mode == 'all';
$message = 'DEL' if $mode == 'delete';
$context->log(error => "$message $url failed: " .
$res->http_status . " " . $res->http_response->message);
return;
}
return $res;
}

1;

使い方は以下の通り

 - module: CustomFeed::Delicious
config:
username: username
password: password
tags: toread
delete: 1

  • username : del.icio.usのユーザー名
  • password : del.icio.usのパスワード
  • tags : 取得するタグ
  • delete : ここに1を指定すると取得後に削除

これで

  1. Subscription::BloglinesでFeedを取得
  2. Publish::Gmail with Filter::BodyTextSubstrでGmailへ送信
  3. GmailでFeedを確認し、気になる記事があればWidget::Deliciousで'toread'タグを付けてクリップ
  4. CustomFeed::Deliciousで選別したFeedを取得し、あとでゆっくり読む

という流れを作ることが出来ます。

残りは、Filter::BreakEntriesToFeedsのハックだな