Dear Home Owner, このページをアンテナに追加 RSSフィード

 | 

2006-05-05

P::P::Subscription::XPath  P::P::Subscription::XPath - Dear Home Owner, を含むブックマーク はてなブックマーク -  P::P::Subscription::XPath - Dear Home Owner,  P::P::Subscription::XPath - Dear Home Owner, のブックマークコメント

lib/Plagger/Plugin/Subscription/XPath.pm

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

use HTML::TreeBuilder::XPath;
use Plagger::Util;
use URI;

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

    $context->register_hook(
        $self,
        'subscription.load' => $self->can('load'),
    );
}

sub load {
    my($self, $context) = @_;
    my $uri = URI->new($self->conf->{url})
        or $context->error("config 'url' is missing");

    my $xhtml = Plagger::Util::load_uri($uri, $self);
    my $tree = HTML::TreeBuilder::XPath->new;
    $tree->parse($xhtml);
    $tree->eof;

    $self->find_feed($tree);
}

sub find_feed {
    my($self, $tree) = @_;
    for my $child ($tree->findnodes($self->conf->{xpath} || '//a')) {
	my $href  = $child->attr('href') or next;
	my $title = $child->attr('title') || $child->as_text;
	
	my $feed = Plagger::Feed->new;
	$feed->url($href);
	$feed->title($title);

	Plagger->context->subscription->add($feed);
    }
}

1;

config.xoxo.yaml

  - module: Subscription::XPath
    config:
      url: http://d.hatena.ne.jp/antipop/20050628/1119966355
      xpath: //ul[@class="xoxo" or @class="subscriptionlist"]//a
トラックバック - http://subtech.g.hatena.ne.jp/youpy/20060505
 |