Net::Tumblr

http://fuba.tumblr.com/post/2327086
こういう仕様なら作るまでもない気がしたけど画像Botのために作ってみたけど一瞬で容量制限溢れたので存在意義がなくなってとりあえず放出。コードはやっぱりはてダの方がいいな…。

usage.pl

use Net::Tumblr;
my $url = shift;

my $t = Net::Tumblr->new(
    email => '[email protected]',
    password => 'password'
)->write(
    type => 'photo',
    source => $url,
    caption => "<a href=\"$url\">$url</a>",
);

詳しくはTumblr - API参照。

Net/Tumblr.pm

package Net::Tumblr;
use strict;
use warnings;

use LWP::UserAgent;
use URI::Escape qw/uri_escape_utf8/;

use constant API_WRITE => 'http://www.tumblr.com/api/write';

sub new {
    my $pkg = shift;
    my %opt = @_;
    
    my $ua = LWP::UserAgent->new();
    
    bless {
        email => ($opt{email} or die 'no email'),
        password => ($opt{password} or die 'no password'),
        generator => ($opt{generator} or 'Net::Tumblr'),
        ua => $ua,
    }, $pkg;
}

sub write {
    my $self = shift;
    my %opt = @_;
    
    $opt{email} = $self->{email};
    $opt{password} = $self->{password};
    $opt{generator} = $self->{generator};
    
    my $req = HTTP::Request->new(POST => &API_WRITE);
    $req->content_type('application/x-www-form-urlencoded');
    
    my $param = join '&',
        map {"$_=".uri_escape_utf8($opt{$_})}
        grep {$opt{$_}}
        keys %opt;
    
    $req->content($param);
    
    return $self->{ua}->request($req);
}

1;