読者です 読者をやめる 読者になる 読者になる

CustomFeed::Hobbit

Plagger

Hobbit / Big Brother のイベントサマリー画面(bb2.html) を読む Feed。サーバ監視のお供に。
status red はメールが飛んでくるからすぐ分かるのだけど、status yellow (軽微な障害) は見落としがちなので。


一応、以下の3つで動作確認。

package Plagger::Plugin::CustomFeed::Hobbit;
use strict;
use base qw( Plagger::Plugin );
use Plagger::Date;
use Plagger::UserAgent;
use Plagger::Util qw( decode_content );
use HTML::TokeParser;

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, $args ) = @_;

    my $url = $self->conf->{url};
    $context->log( info => "GET $url" );

    my $agent = Plagger::UserAgent->new;
    my $res   = $agent->fetch( $url, $self );
    if ( $res->http_response->is_error ) {
        $context->log( error => "GET $url failed: " . $res->status );
        return;
    }

    my $content = decode_content($res);
    my $parser = HTML::TokeParser->new( \$content );

    $parser->get_tag('title');
    my ( $title, $date ) = split /\s+\@\s+/, $parser->get_trimmed_text, 2;

    my $feed = Plagger::Feed->new;
    $feed->title($title);
    $feed->link($url);
    $feed->updated( Plagger::Date->parse_dwim($date) );
    $context->log( debug => qq{title: $title, link: $url, updated: $date} );

    while( my $token = $parser->get_tag('table') ){
        my $summary = $token->[1]->{summary};
        if( $summary =~ /events?/i ){
            $parser->get_tag('tr');
            $feed->description( $parser->get_trimmed_text('tr') );
            last;
        }
    }
    $context->log( debug => "description: ". $feed->description );

    while ( $parser->get_tag('tr') ) {
        my $entry = Plagger::Entry->new;
        $parser->get_tag('td');    # date
        $entry->date( Plagger::Date->parse_dwim( $parser->get_trimmed_text('td') ) );

        $parser->get_tag('td');    # host
        my $host = $parser->get_trimmed_text('td');

        $parser->get_tag('td');    # type
        my $type = $parser->get_trimmed_text('td');

        my $link = $parser->get_tag('a');     # from
        next unless $link->[1]->{href} =~ /bb-histlog/;

        my $from = $parser->get_tag('img')->[1]->{alt};    # from img

        $link = $parser->get_tag('a');
        next unless $link->[1]->{href} =~ /bb-histlog/;
        $entry->link( URI->new_abs( $link->[1]->{href}, $url ) );

        my $to = $parser->get_tag('img')->[1]->{alt};      # to img

        $entry->title("$host:$type [$from]->[$to]");
        $context->log( debug => sprintf("%s %s", $entry->title, $entry->date ) );
        $feed->add_entry($entry);
    }
    $context->update->add($feed);

    return 1;
}
1;
__END__

=head1 NAME

Plagger::Plugin::CustomFeed::Hobbit - Custom feed for Hobbit / BigBrother

=head1 SYNOPSIS

  - module: CustomFeed::Hobbit
    config:
      url: http://example.com/hobbit/bb2.html

=head1 AUTHOR

FUJIWARA Shunichiro <fujiwara at topicmaker.com>

=head1 SEE ALSO

L<Plagger>

=cut