はてなハイクとmixiボイスに投稿する
おうちサーバの特定のアドレスにメールを送るとその内容を解析してはてなハイクとmixiボイスに投稿する仕掛けがとりあえずできた。mixiボイスにははてなハイクの画像のurl(というか、はてなフォトライフのurl)を貼り付ける。mixiは自動リンクしてくれる。
使用したクラスは下記から。
マルチパートなメールを解析する PEAR::Mail::mimeDecode をラップするクラス - お前の予定!! 日記
まずは共通部分。
<?php #美乳 require_once('ReceiptMailDecoder.class.php'); require_once 'HTTP/Request.php'; include "php_mixi.php"; function analize_mail($mail){ $decoder =& new ReceiptMailDecoder($mail); //メールの処理 // text/planなメール本文を取得する $body = mb_convert_encoding($decoder->body['text'],"eucjp-win","jis"); $subject = $decoder->getDecodedHeader( 'subject' ); // マルチパートのデータを取得する if ( $decoder->isMultipart() ) { $imagefiles = array(); $num_of_attaches = $decoder->getNumOfAttach(); for ( $i=0 ; $i < $num_of_attaches ; ++$i ) { $filename = $decoder->attachments[$i]['file_name']; $fpath = "d:/text/scripts/write_hatena/temp/" . $filename; if ( $decoder->saveAttachFile( $i, $fpath ) ) { $imagefiles[] = $fpath; } } } $ret = array('subject'=>$subject,'body'=>$body,'imagefiles' =>$imagefiles); return $ret; } //はてなハイクに投稿 function write_haiku($body,$keyword ="",$imagefiles){ global $hatena_username,$hatena_password; $body = mb_convert_encoding($body,'UTF-8','auto'); $url = 'http://h.hatena.ne.jp/api/statuses/update.xml?'; $req =& new HTTP_Request($url); $req->setMethod(HTTP_REQUEST_METHOD_POST); $req->addHeader("Authorization",'Basic '. base64_encode($hatena_username. ':'. $hatena_password)); if($keyword != ""){$req->addPostData("keyword", mb_convert_encoding($keyword,'UTF-8','auto'));} $req->addPostData("status", $body); for ( $i=0 ; $i < count($imagefiles) ; ++$i ) { $req->addFile("file", $imagefiles[$i], "Content-Type: image/jpeg"); } $req->sendRequest(); $result = $req->getResponseBody(); //返り値を分析 $matches = preg_match("/<text>(.*?)<\/text>/sm",$result,$temp); if($matches === FALSE){ echo "no match\n"; exit; } return $temp[1]; } //mixi voiceに投稿 function write_voice($body){ global $mixi_email,$mixi_pass; $body=mb_convert_encoding($body,'EUC-JP','auto'); $mixi=new PHP_Mixi(); $mixi->PHP_Mixi($mixi_email,$mixi_pass, 0); $mixi->login("home.pl"); $ret = $mixi->fetch("http://mixi.jp/recent_voice.pl"); preg_match('/<input type="hidden" name="post_key" id="post_key" value="(\w*)" \/>/',$ret,$temp); $key = $temp[1]; $params = array("body"=>$body, "post_key"=>$key); $ret = $mixi->submit("add_voice.pl",$params); } ?>
呼び出す部分。haikuvoice_common.phpは上記の共通部分のファイル名。はてなハイクでのタイトルは「自転車部」で統一。
<?php #美乳 include "haikuvoice_common.php"; $hatena_username = 'doroace'; $hatena_password = 'nanchara'; $mixi_email = "doroyamada@nanchara"; $mixi_pass = "kanchara"; $mail = file_get_contents(str_replace("\\\\?\\",'',$argv[1])); #xmailの不具合?対応 $ret =analize_mail($mail); $ret_h = write_haiku($ret['body'],"自転車部",$ret['imagefiles']); write_voice(preg_replace("/^[^=]+=/","",$ret_h)); ?>
メインアカウントで、メールからタイトルを引き継いで投稿。mixiボイスはタイトル機能がないのでタイトル省略。
<?php #美乳 include "haikuvoice_common.php"; $hatena_username = 'doroyamada'; $hatena_password = 'untara'; $mixi_email = "doroyamada@kanchara"; $mixi_pass = "nanchara"; $mail = file_get_contents(str_replace("\\\\?\\",'',$argv[1])); $ret =analize_mail($mail); $ret_h = write_haiku($ret['body'],$ret['subject'],$ret['imagefiles']); write_voice(preg_replace("/^[^=]+=/","",$ret_h)); ?>