Twitterに投稿するELisp(+Perl Script)
(4/14 追記)
id:sun-basix:20070414:1176556369で、Perl側を大幅に書き直しています。なので、こちらはあまり読まなくて結構です。
書いてみた。もっと詳しく言うと、Twitterに投稿「するだけ」のスクリプトです。elispは初めてなんで勘弁してください。
- twitter-post.el
(defun twitter-post () "Post message from Emacs" (interactive) (if (not twitter-userid) (setq twitter-userid (read-from-minibuffer "Twitter UserID?: "))) (if (not twitter-passwd) (setq twitter-passwd (read-from-minibuffer "Twitter Password?: "))) (if (not twitter-plpath) (setq twitter-plpath (read-from-minibuffer "script path?: "))) (let ((msg (read-from-minibuffer "POST to Twitter!: "))) (start-process "twitter-proc" ; プロセス名 "tw-post" ; バッファ名 twitter-plpath ; コマンド "--user" twitter-userid ; ↓以下、オプション "--pass" twitter-passwd "--message" msg))) (provide 'twitter-post)
- twitter-post.pl
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request::Common qw/ POST /; use Getopt::Long; my $username; my $password; my $message; GetOptions( 'user|u=s' => \$username, 'pass|p=s' => \$password, 'msg|message=s' => \$message, ); die "Parameters are not enough!" unless ( $username || $password || $message ); die "Message too long..." if length($message) > 140; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( POST => "http://twitter.com/statuses/update.xml?status=$message" ); $req->authorization_basic( $username, $password ); my $res = $ua->request($req); print "Message posted.\n" if $res->{_rc} eq 200;
特にPerl側については全く推敲してないので、[これからやる]タグ付きで。他のAPIに対応させるのも[これからやる]タグで。Net::Twitterモジュールがあることも知ってますが、なんか導入する方が面倒臭く感じたので。うそです。あらかた書き終わってから知ったので、意地張ってみただけです。すいません。
あ、そうだ、.emacsには
(load "twitter-post") (setq twitter-userid "something") (setq twitter-passwd "xxxx") (setq twitter-plpath "/path/to/script")
を追加で。使う時に「M-x twitter-post」でプロンプトが出ます。多分、他のAPIに対応するようになったら名前も変わります。といっても、自分しか使わないからいいか。