home
api
develop
badge
wallpaper
rhaco.org
PHP library & setup framework
lingr
bugs
jaja
arbo
rhaco_1_x
arbo rev. 39
trunk/network/browser/MixiBrowser.php
仕様変更に対応とか
39: 仕様変更に対応とか
* @author SHIGETA Takeshiro * @package rhacolibs
* @license New BSD License * @version $Id: MixiBrowser.php 32 2008-01-28 04:34:06Z riafweb $ */ class MixiBrowser extends Browser { var $id; var $islogin = false; var $_post_diary_options = array('news_id', 'campaign_id', 'news_title', 'news_url', 'movie_id', 'movie_title', 'movie_url'); function MixiBrowser($email=null, $password=null){ if(!is_null($email) && !is_null($password)) $this->login($email, $password); } /** * ログインを行う * * @param string $email * @param string $password * @return bool */ function login($email, $password){ if($this->islogin == true) return true; $this->clearVariable(); $this->setVariable('email', $email); $this->setVariable('password', $password); $this->setVariable('next_url', '/home.pl'); $login = StringUtil::encode($this->post('http://mixi.jp/login.pl')); SimpleTag::setof($tag, $login, 'html'); foreach($tag->getIn('input') as $input){ if($input->getParameter('name') == 'email'){ $this->islogin = false; return false; } } $session = explode('_', $this->cookie['BF_SESSION']); $this->id = array_shift($session); $this->islogin = true; return true; } /** * ログアウトを行う * * @return void */ function logout(){ if($this->islogin){ $this->get('http://mixi.jp/logout.pl'); $this->islogin = false; } } /** * プロフィールを取得 * * @param int $id * @return array */ function showFriend($id){ if($this->islogin == false) return array('profile' => array()); $this->clearVariable(); $this->setVariable('id', $id); $html = $this->get('http://mixi.jp/show_friend.pl'); SimpleTag::setof($tag, StringUtil::encode($html), 'body'); $profile = array(); $result = $this->_getTag($tag, array('div' => array('id' => 'bodyArea')), array('div' => array('id' => 'bodyMainArea')), array('div' => array('id' => 'bodyContents')), array('div' => array('id' => 'profile')) ); foreach($result->getIn('dl') as $dl){ $profile[] = array('name' => $dl->getInValue('dt'), 'value' => $dl->getInValue('dd')); } return compact('profile'); } /** * 日記を書く * * @param string $title * @param string $body * @param array $options * @return bool */ function addDiary($title, $body, $options=array()){ if($this->islogin == false) return false; return $this->_postDiary(null, $title, $body, $options); } /** * 日記を編集する * * @param int $id * @param string $title * @param string $body * @param array $options * @return bool */ function editDiary($id, $title, $body, $options=array()){ if($this->islogin == false) return false; return $this->_postDiary($id, $title, $body, $options); } /** * 日記を削除する * * @param int $id * @return void */ function deleteDiary($id){ if($this->islogin == false) return array('profile' => array()); $this->clearVariable(); $this->setVariable('diary_id', $id); $this->setVariable('list_page_number', 1); $this->post('http://mixi.jp/delete_diary.pl'); $this->submit(); } function _postDiary($id, $title, $body, $options=array()){ $this->clearVariable(); $this->setVariable('diary_title', StringUtil::encode($title, StringUtil::EUC())); $this->setVariable('diary_body', StringUtil::encode($body, StringUtil::EUC())); foreach($this->_post_diary_options as $key){ $this->setVariable( $key, isset($options[$key]) ? StringUtil::encode($options[$key], StringUtil::EUC()) : '' ); } if(is_null($id)){ $this->setVariable('id', $this->id); // mixi側の意図がわからない $url = 'http://mixi.jp/add_diary.pl'; } else { $this->setVariable('id', $id); $this->setVariable('form_date', ''); $url = 'http://mixi.jp/edit_diary.pl'; } $this->setVariable('submit', 'main'); $this->post($url); $this->clearVariable('submit'); $result = $this->submit(); SimpleTag::setof($tag, $result, 'body'); foreach($tag->getIn('input') as $input){ if($input->getParameter('name') == 'title'){ return false; } } return true; } /** * 日記の一覧を取得 * * @param int $id ユーザーID * @param int $limit * @param int $wait * @return array */ function listDiary($id, $limit=30, $wait=2){ if($this->islogin == false) return array(); $count = 0; $page = 0; $hasNext = true; $list = array(); $this->clearVariable(); if($id != $this->id) $this->setVariable('id', $id); while($limit >= $count && $hasNext == true){ $hasNext = false; $page++; $this->setVariable('page', $page); $html = $this->get('http://mixi.jp/list_diary.pl'); SimpleTag::setof($tag, StringUtil::encode($html), 'body'); $result = $this->_getTag($tag, array('div' => array('id' => 'bodyArea')), array('div' => array('id' => 'bodyMainArea')), array('div' => array('id' => 'bodyMainAreaMain')) ); foreach($result->getIn('div') as $div){ if($div->getParameter('class') == 'pageNavigation01'){ $hasNext = preg_match('/次を表示/' , $div->getValue()) ? true : false;// 手抜き continue; } else if(!in_array($div->getParameter('class'), array('listDiaryBlock', 'listDiaryBlockLast'))){ continue; } if($count >= $limit) break 2; $a = array_shift($div->getIn('a')); $list[] = array( 'link' => 'http://mixi.jp/' . $a->getParameter('href'), 'title' => $a->getValue(), 'body' => $div->getInValue('p'), 'date' => StringUtil::replace(StringUtil::toULD($div->getInValue('dd')), "\n", ' '), ); $count++; } $this->clearVariable('page'); if($limit >= $count && $hasNext) sleep($wait); } return $list; } /** * 日記を取得 * * @param int $id * @param int $owner_id * @return array */ function viewDiary($id, $owner_id){ if($this->islogin == false) return array(); $this->clearVariable(); $this->setVariable('id', $id); $this->setVariable('owner_id', $owner_id); $html = $this->get('http://mixi.jp/view_diary.pl'); SimpleTag::setof($tag, StringUtil::encode($html), 'body'); $diary = array(); $result = $this->_getTag($tag, array('div' => array('id' => 'bodyArea')), array('div' => array('id' => 'bodyMainArea')), array('div' => array('id' => 'bodyMainAreaMain')), array('div' => array('class' => 'viewDiaryBox')) ); foreach($result->getIn('div') as $div){ if($div->getParameter('class') == 'listDiaryTitle'){ $diary['title'] = trim(strip_tags($div->getInValue('dt'))); $diary['date'] = $div->getInValue('dd'); } else if($div->getParameter('class') == 'txtconfirmArea'){ $diary['body'] = $div->getInValue('div'); } } $comments = array(); if(preg_match('@<\!--comment-->(.*?)<\!--\/comment-->@ims', StringUtil::encode($html), $match)){ //div[@class=diaryMainArea02]',$tag); SimpleTag::setof($tag, $match[1]); if($dls = $tag->getIn("dl")){ foreach($dls as $dl){ $dt = array_pop($dl->getIn('dt')); $author = $dt->getInValue('a'); $time = array_pop($dt->getIn('span')); $comment = $dl->getInValue('dd'); $comments[] = array( 'author' => $author, 'timestamp' => strtotime(str_replace(array('年', '月', '日',' '), array('/','/','',' '), $time->getValue().':00')), 'body' => $comment, ); } } } $diary['comments'] = $comments; return $diary; } /** * メッセージのリストを取得する * ! this is draft version ! * * @param string $box (inbox|outbox|savebox|thrash) * @return array * @todo もう少し詳細な情報も取得できるようにする * @notice inboxしか動きません>< */ function listMessage($box='inbox', $page=1){ $this->clearVariable(); $this->setVariable('box', $box); $this->setVariable('page', $page); $html = StringUtil::encode($this->get('http://mixi.jp/list_message.pl')); $messages = array(); SimpleTag::setof($tag, $html, 'form'); foreach($tag->getIn('a') as $a){ if(preg_match('@^view_message@', $a->getParameter('href'))){ $this->clearVariable(); $message = StringUtil::encode($this->get('http://mixi.jp/' . $a->getParameter('href'))); if(preg_match('@(差出人|宛 先).*?
(.*?)@', $message, $match)){ $author = $match[2]; } if(preg_match('@(日 付|日 付)(.*?)$@im', $message, $match)){ $date = str_replace(array(':','年','月','日','時','分',' '), array('','/','/','',':','',''), $match[2]) . ':00'; } SimpleTag::setof($t, $message); $input = V::arrays($t->getIn('input'), 1, 2); $messages[] = array( 'author' => $author, 'subject' => $input[0]->getParameter('value'), 'body' => $input[1]->getParameter('value'), 'date' => $date, ); } } return $messages; } function _getTag(){ $args = func_get_args(); $tag = array_shift($args); foreach($args as $key => $arg){ $tag = array_shift($tag->find(new SimpleTag(key($arg), null, $arg[key($arg)]))); } return $tag; } function clearVariable(){ $this->variables = array(); } }