ソーシャルブックマークをMTにアップする方法

#!/usr/local/bin/perl
use strict;
use warnings;

NDO::HatenaBM2MT->new(
    template => './template.html',
    rsd  => 'http://naoya.dyndns.org/~naoya/mt/rsd.xml',
    feedurl  => 'http://b.hatena.ne.jp/naoya/rss',
    mt_user  => 'foo',
    mt_passwd => 'bar',
    mt_blogid => 3,
)->do;

package NDO::HatenaBM2MT;

use Encode;
use HTML::Template;
use LWP::Simple;
use Net::MovableType;
use Time::Piece;
use XML::RSS;

sub new {
    my $class = shift;
    bless {@_}, $class;
}

sub do {
    my $self = shift;
    $self->init;
    $self->getfeed;
    $self->makelist;
    $self->format;
    $self->associate;
    $self->post;
}

sub init {
    my $self = shift;
    $self->{tmpl} = HTML::Template->new(
        type => 'filename',
        source => $self->{template},
        die_on_bad_params => 0
    );
    $self->{mt} = Net::MovableType->new($self->{rsd});
    $self->{mt}->username($self->{mt_user});
    $self->{mt}->password($self->{mt_passwd});
    $self->{mt}->blogId($self->{mt_blogid});
    $self->{rss} = XML::RSS->new;
}

sub getfeed {
    my $self = shift;
    $self->{document} = LWP::Simple::get($self->{feedurl})
        or die "cannnot get feed from $self->{feedurl}: $!";
    1;
}

sub makelist {
    my $self = shift;
    $self->{rss}->parse($self->{document});
    $self->{list} = [];
    my $today = localtime->ymd;
    @{$self->{list}} = map {
        +{
            title => $_->{title} || '',
            link  => $_->{link}  || '',
            comment => $_->{description} || '',
            subject => $_->{dc}->{subject} || '',
        };
    } grep $_->{dc}->{date} =~ /^$today/, @{$self->{rss}->{items}};
}

sub format {
    my $self = shift;
    $self->{list} or return;
    for my $item (@{$self->{list}}) {
        Encode::_utf8_off($item->{$_})
                for qw(title comment subject);
    }
}

sub associate {
    my $self = shift;
    $self->{list} or return;
    $self->{tmpl}->param(
        bookmarklist => $self->{list},
    );
}

sub post {
    my $self = shift;
    $self->{list} or return;
    my $title = $self->{title} || $self->{rss}->{channel}->{title} || '';
    Encode::_utf8_off($title);
    my $id = $self->{mt}->newPost({
        title       => $title,
        description => $self->{tmpl}->output,
    });
    $self->{mt}->publishPost($id) if $id;
}

1;

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です