先日、更新処理だけを書きましたが、スクリプト全体を晒してみます。ドキュメントも書いてみました

標準モジュールしか使っていないので、導入も簡単だと思う。導入には .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 );
}

トラックバック(1)

このブログ記事を参照しているブログ一覧: メールで twitter を更新するスクリプト

このブログ記事に対するトラックバックURL: http://tsuyuguchi.com/mt/mt-tb.cgi/326

■ Twiitter Twiitter と言いたいがために作ったスクリプト。一... 続きを読む

コメントする

最近の画像

料理が少し楽しくなるかもしれないグッズ
江戸糸あやつり人形
江戸糸あやつり人形
おうち、
新橋青空大判将棋
「天体の回転について」読了
「幼年期の終り」読了
「数学ガール/フェルマーの最終定理」読了
ハリー・ポッター 最終巻
もう炊飯器いらないかも

私の本棚

 

自転車走行距離

Flickr Photos

なかのひと

Y!ログール

このサイトについて

あわせて読みたいブログパーツ

2008年11月

            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            

アーカイブ

My Update

  • Loading...

その他