最新 追記

やすみたい


2010.05.05 (Wed) [長年日記]

§ [tool] twitterにメールで投稿する何か OAuth対応版

 約1年ぶりの更新。日記的なものはtwitterにはき出すだけになってるので。ということで、休みの間にtwitterにメールで投稿する何かをOAuth対応にしてみた。主な変更点は:
  • OAuth対応
  • Net::TwitterモジュールからNet::Twitter::Liteに変更
  • 短縮URLをtinyurlからbit.lyにした
#!/usr/local/bin/perl
# Twitter Email gateway with Uploading Images to Flickr
# tmgfl.pl by yasumitai
# ver 2010.5.5

use strict; use warnings; use Net::Twitter::Lite; use MIME::Words qw(:all); use MIME::Parser; use Encode; use LWP::Simple; use File::Basename; use URI::Escape; use WebService::Simple::Flickr; use Data::Dumper; #for debug
# Config use constant TMPDIR => ''; #一時ファイル保管ディレクトリ my $debugmode ='log'; # '' for debug OFF my $truncate = 1; #文字数制限チェック ## Twitter OAuth keys ## Reference: http://d.hatena.ne.jp/layerzero/20090930/p1 my $c_key = ''; #Twitter API consumer key my $c_secret = ''; #Tiwtter API consumer secret my $a_token = ''; #Tiwtter API authrized token my $a_secret = ''; #Tiwtter API authrized token secret
## Flickr API keys, ## See http://flickr.com/services/api/auth.howto.desktop.html my $flickr_key = ''; my $flickr_secret = ''; my $flickr_token = ''; ## How to get Flickr AUTH token using WebService::Simple::Flickr, SEE ## http://blog.riywo.com/2009/01/21/001939 my $flickr_home = 'http://flickr.com/photos/USERNAME/';
# MIME parse my $mail; while (<>) { $mail .= $_; }
if ($debugmode) { $debugmode = TMPDIR. $debugmode; open(FH, " > $debugmode"); print FH "----- import message:: \n"; print FH $mail; print FH "----- End of Message \n"; close(FH); }

my $parser = new MIME::Parser; $parser->output_dir(TMPDIR); my $entity = $parser->parse_data( $mail ); my ( $charset, $message, $enclosure ) = &parse_mail( $entity );
&logging($debugmode, "FileName:: $enclosure") if ($debugmode) ; ###
# ファイルアップロード $enclosure = &tiny_url( $flickr_home . &uploadfile( $enclosure, $flickr_key, $flickr_secret, $flickr_token )) if ($enclosure); &logging($debugmode, "TinyURL:: $enclosure") if ($debugmode) ;

# メッセージを処理 $message = &encode_message( $message, $charset, $enclosure, $truncate); #$message = Encode::encode('utf8', $message);
&logging($debugmode, "EncodedMSG:: $message") if ($debugmode) ; ###
#Twitter POST my $nt = Net::Twitter::Lite->new( consumer_key => $c_key, consumer_secret => $c_secret, );
$nt->access_token($a_token); $nt->access_token_secret($a_secret);
my $status = $nt->update( $message );
&logging($debugmode, Dumper($status)) if ($debugmode) ; ###

sub parse_mail { my $ent = shift; my $body =''; my $charset =''; my $outputfile = ''; unless ( $ent->is_multipart) { $body = $ent->bodyhandle->as_string; $charset = $ent->head->mime_attr( 'Content-Type.charset' ) } else { $body = $ent->parts(0)->bodyhandle->as_string; $charset = $ent->parts(0)->head->mime_attr( 'Content-Type.charset' ); my $image = $ent->parts(1)->bodyhandle->as_string; #添付ファイルを書き出し $outputfile = TMPDIR . (fileparse( $ent->parts(1)->bodyhandle->path ))[0] ; open(FILE, "> $outputfile"); print FILE $image; close(FILE);
} return ( $charset, $body, $outputfile ); }

sub encode_message { my ( $mes, $char, $url, $t ) = @_; $mes =~ s/\s/ /g; my $umes = Encode::decode($char ,$mes); if ( $t ) { if ( length( $umes ) > 140 ) { $umes = substr( $umes, 0, 139 ); } } if ( $url ) { $umes = substr ($umes, 0, 110 ) . " ".$url; } return $umes; }
sub tiny_url { my ($url,) = @_; # my $tinyurl = get( "http://tinyurl.com/api-create.php?url=".$url ); my $tinyurl = get( "http://bit.ly/api?url=".$url );
return $tinyurl; }
sub uploadfile { my ($file, $api_key, $secret_key, $auth_token) = @_; my $flickr = WebService::Simple::Flickr->new( 'api_key' => $api_key, 'api_secret' => $secret_key, 'auth_token' => $auth_token );
my $ref = $flickr->upload_post({'photo' => $file }); return ( $ref->parse_response->{photoid} ); }
sub logging { my ($file, $log) = @_; open(FH, " >> $file"); print FH $log ."\n"; close(FH); }
OAuthの各種token取得は Net::TwitterでOAuthしてみるテスト - 日々積みゲー を 参考に。 今なら、メールで投稿機能は公式サイトにもあるし、写真についてもFlickrが公式に対応しているので今さらスクリプトをわざわざ自分のサーバに置く必要があるのかっていう。好きなクライアント名付けられるくらいか、メリットは。