先日、更新処理だけを書きましたが、スクリプト全体を晒してみます。ドキュメントも書いてみました。
標準モジュールしか使っていないので、導入も簡単だと思う。導入には .forward とか /etc/aliase を編集できる環境が必要なので、誰でもって訳じゃないけど、twittermail を使うのもちょっと気が引けてしまうと言う場合はおすすめです。
メールから更新というわりにはエンコード変換ルーチン from_to の引数が Guess だったりと、無駄に汎用的に作ってしまったため、短い文章だと文字コードの判定に失敗して文字化けする可能性があるかも。
syntaxhighlighter って perl に対応してないんですね。他のに変えようかな。
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Encode qw/from_to/;
use Encode::Guess qw/euc-jp shiftjis iso-2022-jp /;
my %enable;
my $username = ''; # your username
my $password = ''; # your password
my $test_username = ''; # your test account if you have
my $test_password = ''; # your test account if you have
my $logfile = '';
# enable functions
$enable{'update'} = 1; # if set to 0, not update to twitter.
$enable{'testaccount'} = 0; # if set to 1, update as test account
$enable{'log'} = 1; # if set to 0, no logging
if ( $enable{'testaccount'} ){
$username = $test_username;
$password = $test_password;
}
my $from;
# throw away header part
while( <STDIN> ){
last if $_ eq "\n";
$from = $1 if /^From:\s+(.*)$/i;
}
# join body part and delete space
my $status = join( "", <STDIN> );
$status =~ s/[\x0a\x0d]/ /g;
$status =~ s/^\s+|\s+$//g;
# convert to utf-8
from_to( $status, 'Guess', 'utf-8' );
# post to twitter
my $res = undef;
if( $enable{'update'} ){
my $endpoint = 'http://twitter.com/statuses/update.json';
my $netloc = 'twitter.com:80';
my $realm = 'Twitter API';
my $ua = LWP::UserAgent->new;
$ua->credentials( $netloc, $realm, $username, $password );
$res = $ua->post( $endpoint, [ status => $status ] );
}
# write log in file
if( $enable{'log'} ){
open( LOG, ">>" . $logfile ) || die "cannot open $logfile";
print LOG "[$username] via $from";
print LOG " on " . $res->header( "Date" ) . ", Status: " . $res->header( "Status" ) if $enable{'update'};
print LOG "\n";
print LOG $status . "\n\n";
close( LOG );
}











コメントする