| 1 | package Plagger::Plugin::Subscription::XPath; | ||
| 2 | use strict; | ||
| 3 | use base qw( Plagger::Plugin ); | ||
| 4 | |||
| 5 | use HTML::TreeBuilder::XPath; | ||
| 6 | use Plagger::Util; | ||
| 7 | use URI; | ||
| 8 | |||
| 9 | sub register { | ||
| 10 | my($self, $context) = @_; | ||
| 11 | |||
| 12 | $context->register_hook( | ||
| 13 | $self, | ||
| 14 | 'subscription.load' => $self->can('load'), | ||
| 15 | ); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub load { | ||
| 19 | my($self, $context) = @_; | ||
| 20 | my $uri = URI->new($self->conf->{url}) | ||
| 21 | or $context->error("config 'url' is missing"); | ||
| 22 | |||
| 23 | my $xhtml = Plagger::Util::load_uri($uri, $self); | ||
| 24 | my $tree = HTML::TreeBuilder::XPath->new; | ||
| 25 | $tree->parse($xhtml); | ||
| 26 | $tree->eof; | ||
| 27 | |||
| 28 | $self->find_feed($tree, $uri); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub find_feed { | ||
| 32 | my($self, $tree, $uri) = @_; | ||
| 33 | for my $child ($tree->findnodes($self->conf->{xpath} || '//a')) { | ||
| 34 | my $href = $child->attr('href') or next; | ||
| 35 | my $title = $child->attr('title') || $child->as_text; | ||
| 36 | |||
| 37 | my $feed = Plagger::Feed->new; | ||
| 38 | $feed->url( URI->new_abs($href, $uri) ); | ||
| 39 | $feed->title($title); | ||
| 40 | |||
| 41 | Plagger->context->subscription->add($feed); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; | ||
| 46 | |||
| 47 | __END__ |