package Hatena::Screenshot; use strict; use Carp qw(carp croak); use WWW::Mechanize; sub new { my($class, %opt) = @_; my $self = bless {conf => { %opt }}, $class; if ($opt{cookie_jar}) { $self->{cookie_jar} = $opt{cookie_jar}; } elsif ($opt{cookies}) { $self->load_cookies; } elsif (!$opt{login}) { croak 'Please set option for cookies / login'; } croak 'Please set option for login' unless $opt{login}; $self->{mech} = WWW::Mechanize->new(cookie_jar => $self->{cookie_jar}); $self->{mech}->agent_alias( "Windows IE 6" ); if (!$self->{cookie_jar} && $opt{login}) { croak 'Please set login option for id & password' unless $opt{login}->{id} && $opt{login}->{password}; $self->hatena_login; } $self->{conf}->{date} = '00000000' unless $opt{date} =~ /^\d{8,8}$/; $self->_edit; $self; } sub hatena_login { my $self = shift; # not supported of normal login. } sub load_cookies { my $self = shift; require HTTP::Cookies::Guess; $self->{cookie_jar} = HTTP::Cookies::Guess->create($self->{conf}->{cookies}); } sub _edit { my $self = shift; my $res = $self->{mech}->get('http://d.hatena.ne.jp/' . $self->{conf}->{login}->{id} . '/edit?date=' . $self->{conf}->{date}); croak 'remote server error.' unless $self->{mech}->success; die 'login failed.' unless $res->content =~ m!
{urls} = {}; $self->_edit; my $res = $self->{mech}->submit_form( form_name => 'edit', fields => { body => '[' . join(":image]\n[", @urls) . ':image]' }, button => 'edit', ); unless ($self->{mech}->success) { warn 'convert failed.'; return; } unless ($res->content =~ m{
(.+)}mso) { warn 'diary post failed.'; return; } my $body = $1; my $i = 0; while ($body =~ m!src="http://screenshot.hatena.ne.jp/images/[^/]+/([^"]+)"!g) { $self->{urls}->{$urls[$i++]} = $1; } return $self->{urls}; } sub convert_lite { my($self, $url) = @_; $self->convert($url); $self->normal->{$url}; } sub convert_once { my($self, $url) = @_; $self->convert($url); +{ normal => $self->normal->{$url}, large => $self->large->{$url}, small => $self->small->{$url}, }; } sub _genlist { my($self, $size) = @_; +{ map { $_ => sprintf('http://screenshot.hatena.ne.jp/images/%s/%s', $size, $self->{urls}->{$_}) } keys %{ $self->{urls} } }; } sub normal { my $self = shift; $self->_genlist('120x90'); } sub large { my $self = shift; $self->_genlist('200x150'); } sub small { my $self = shift; $self->_genlist('80x60'); } 1; __END__ =head1 NAME Hatena::Screenshot - screenshot url maker by hatena diary. =head1 SYNOPSIS use Hatena::Screenshot; my $jitensya = Hatena::Screenshot->new( cookies => '/home/user/.w3m/cookie', login => { id => 'hatenaid' } ); use Hatena::Screenshot; use HTTP::Cookies::Guess; my $jitensya = Hatena::Screenshot->new( cookie_jar => HTTP::Cookies::Guess->create('/home/user/.w3m/cookie'), login => { id => 'hatenaid' } ); $jitensya->convert(qw(http://b.hatena.ne.jp/ http://i.yappo.jp/)); my $normal = $jitensya->normal; print $normal->{http://b.hatena.ne.jp/}; my $small = $jitensya->small; print $small->{http://b.hatena.ne.jp/}; my $large = $jitensya->large; print $large->{http://b.hatena.ne.jp/}; print $jitensya->convert_lite('http://www.cpan.org/')->{normal}; print $jitensya->convert_once('http://www.google.com/'); =head1 DESCRIPTION =head1 AUTHOR Kazuhiro Osawa Eko@yappo.ne.jpE =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO L. =cut