Plagger::Plugin::Publish::BlogTimes

MTBlogTimesPlaggerで動かすようにしてみたのですが、なにを寝ぼけていたのかこのままだと複数のフィードの場合うまくいきません。
publish.finalizeで購読しているフィード全体の投稿時間を出力するようにするか、idを基に個別に画像を出力するように変更します。

package Plagger::Plugin::Publish::BlogTimes;

use strict;
use base qw( Plagger::Plugin );
use File::Spec;
use GD;

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

sub feed {
    my ( $self, $context, $args ) = @_;
    my $dir = File::Spec->canonpath( $self->conf->{dir} );

    unless (-e $dir && -d _) {
        mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
    }

    my $feed = $args->{feed};

    my $chart_type  = $self->conf->{style}       || 'bar';
    my $basename    = $self->conf->{name}        || 'blogtimes';
    my $site_url    = $feed->link;
    my $width       = $self->conf->{width}       || 400;
    my $height      = $self->conf->{height}      || 30;
    my $linecolor   = $self->conf->{linecolor}   || '#FFFFFF';
    my $textcolor   = $self->conf->{textcolor}   || '#757575';
    my $fillcolor   = $self->conf->{fillcolor}   || '#757575';
    my $bordercolor = $self->conf->{bordercolor} || '#757575';
    my $pad         = $self->conf->{padding}     || 5;
    my $show_text   = $self->conf->{show_text}   || 'on';

    my (@entry_times);
    for my $entry ($feed->entries) {
        push @entry_times, $entry->date->strftime("%H%M");
    }

    # Actual drawing
    my $txtpad = ($show_text eq 'off')? 0: gdTinyFont->height;
    my $scale_width = $width+($pad*2);
    my $scale_height = $height+($pad*2)+$txtpad+$txtpad;
    my $img = GD::Image->new($scale_width,$scale_height);
    my $white = $img->colorAllocate(255,255,255);
    $linecolor = $img->colorAllocate(&hex2rgb($linecolor));
    $textcolor = $img->colorAllocate(&hex2rgb($textcolor));
    $fillcolor = $img->colorAllocate(&hex2rgb($fillcolor));
    $bordercolor = $img->colorAllocate(&hex2rgb($bordercolor));
    $bordercolor = $img->colorAllocate(&hex2rgb($bordercolor));
    $img->transparent($white);
    my $line_y1 = $pad+$txtpad;
    my $line_y2 = $pad+$txtpad+$height;
    $img->rectangle(0,0,$scale_width-1,$scale_height-1,$bordercolor);
    $img->filledRectangle($pad,$line_y1,$pad+$width,$line_y2,$fillcolor);
    my ($line_x,$i);
    foreach $i (@entry_times) {
        $line_x = $pad + (round((to_minutes($i)/1440)*$width));
        $img->line($line_x,$line_y1,$line_x,$line_y2,$linecolor);
    }
    # Shut off text if width is too less.
    if ($show_text eq 'on') {
        if ($width >= 100) {
            my $ruler_y = $pad+$txtpad+$height+2;
            my $ruler_x;
            for ($i = 0; $i <= 23; $i+=2) {
                $ruler_x = $pad + round($i*$width/24);
                $img->string(gdTinyFont,$ruler_x,$ruler_y,"$i",$textcolor);
            }
            $img->string(gdTinyFont, $pad+$width-2,$ruler_y,"0", $textcolor);
            my $caption_x = $pad;
            my $caption_y = $pad-1;
            my $caption = "B L O G T I M E S  Powerd by Plagger  ";
            $img->string(gdTinyFont,$caption_x,$caption_y,$caption,$textcolor);
        } else {
            my $ruler_y = $pad+$txtpad+$height+2;
            my $ruler_x;
            for ($i = 0; $i <= 23; $i+=6) {
                $ruler_x = $pad + round($i*$width/24);
                $img->string(gdTinyFont,$ruler_x,$ruler_y,"$i",$textcolor);
            }
            $img->string(gdTinyFont, $pad+$width-2,$ruler_y,"0", $textcolor);
            my $caption_x = $pad;
            my $caption_y = $pad-1;
            my $caption = "Powerd by Plagger";
            $img->string(gdTinyFont,$caption_x,$caption_y,$caption,$textcolor);
        }
    }
    # Save Image file
    my $image_file = File::Spec->catfile($dir,"$basename.png");
    open(CHART, ">$image_file") or  $context->error("Cannot open file for writing: $!");
    binmode CHART;
    print CHART $img->png;
    close CHART;
}

sub hex2rgb {
    $_[0] =~ s/^#//;
    return undef unless ($_[0] =~ /[0-9a-fA-F]{6}/);
    return (hex(substr($_[0],0,2)),hex(substr($_[0],2,2)),hex(substr($_[0],4,2)));
}

sub round      { return sprintf("%.0f",$_[0]); }
sub to_minutes { return (((substr($_[0],0,2))*60)+(substr($_[0],2,2))); }

1;

__END__
  - module: Publish::BlogTimes
    config:
      dir: /var/www/blogtimes

http://static.flickr.com/54/145272442_7db83a0554.jpg