Created
July 2, 2010 04:08
-
-
Save ksz/460918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::import('Component', 'Twitter'); | |
class UltrasoulBotShell extends Shell | |
{ | |
var $uses = array('LastTimeline', 'LastMention'); | |
// Twitter Component | |
var $twitter = null; | |
//名前 | |
var $name = 'ultrasoul_bot'; | |
//Consumer keyの値 | |
var $consumer_key = ""; | |
//Consumer secretの値 | |
var $consumer_secret = ""; | |
//Access Tokenの値 | |
var $access_token = ""; | |
//Access Token Secretの値 | |
var $access_token_secret = ""; | |
/* | |
* 初期化 | |
*/ | |
function initialize() | |
{ | |
parent::initialize(); | |
$this->twitter = new TwitterComponent(); | |
$this->twitter->initialize($this, array( | |
$this->consumer_key, | |
$this->consumer_secret, | |
$this->access_token, | |
$this->access_token_secret, | |
$this->name)); | |
} | |
/* | |
* ツイート | |
*/ | |
function tweet() | |
{ | |
$tweets = $this->IwaibotTweet->findAll(); | |
$status = $tweets[rand(0, count($tweets) - 1)]['IwaibotTweet']['tweet']; | |
$this->twitter->replyToAll('sawabebot', $status); | |
echo $status . "\n"; | |
} | |
/* | |
* タイムラインを解析 | |
*/ | |
function timeline() | |
{ | |
$last_timeline = $this->LastTimeline->findByName('ultrasoul_bot'); | |
$last_status_id = $last_timeline['LastTimeline']['status_id']; | |
$last_created_at = $last_timeline['LastTimeline']['created_at']; | |
if (!$timeline = $this->twitter->getFriendTimeline(array('count' => 30))) { | |
exit(); | |
} | |
// 1回の実行につき1ユーザ1リプライ | |
$post_users = array(); | |
foreach($timeline->status as $value) { | |
$status_id = $value->id; //個別発言のステータスID | |
$created_at = $value->created_at; //発言時刻 | |
$text = $value->text; //発言内容 | |
$screen_name = $value->user->screen_name; //発言者のtwitterID | |
if (!$status_id) { | |
exit(0); | |
} | |
// 最後に処理した発言IDor発言時刻より前は処理しない | |
if ($last_status_id == $status_id || $last_created_at > $created_at) { | |
break; | |
} | |
// 今回処理済のユーザは無視 | |
if (in_array($screen_name, $post_users)) { | |
continue; | |
} | |
// 自分の発言は拾わない | |
if ($screen_name == 'ultrasoul_bot') { | |
continue; | |
} | |
if (strstr($text, '@')) { | |
continue; | |
} | |
if (strstr($text, 'RT') || strstr($text, 'QT')) { | |
continue; | |
} | |
$status = $this->ultrasoul($text); | |
if ($status) { | |
$result = $this->twitter->reply($screen_name, array("status" => $status, "in_reply_to_status_id" => "$status_id")); | |
echo $result . "\n"; | |
$post_users[] = (String)$screen_name; | |
} | |
} | |
$value = $timeline->status[0]; | |
$last_timeline['LastTimeline']['status_id'] = $value->id; | |
$last_timeline['LastTimeline']['created_at'] = date('Y-m-d H:i:s', strtotime($value->created_at)); | |
$this->LastTimeline->save($last_timeline); | |
} | |
function mention() | |
{ | |
$last_mention = $this->LastMention->findByName('ultrasoul_bot'); | |
$last_status_id = $last_mention['LastMention']['status_id']; | |
$last_created_at = $last_mention['LastMention']['created_at']; | |
if (!$timeline = $this->twitter->getMention(array('count' => 10))) { | |
exit(); | |
} | |
foreach($timeline->status as $value) { | |
$status_id = $value->id; //個別発言のステータスID | |
$created_at = $value->created_at; //発言時刻 | |
$text = $value->text; //発言内容 | |
$screen_name = $value->user->screen_name; //発言者のtwitterID | |
if (!$status_id) { | |
exit(0); | |
} | |
// 最後に処理した発言IDor発言時刻より前は処理しない | |
if ($last_status_id == $status_id || $last_created_at > $created_at) { | |
break; | |
} | |
// 自分の発言は拾わない | |
if ($screen_name == 'ultrasoul_bot') { | |
continue; | |
} | |
// RTされたらふぁぼる | |
if (strstr($text, 'RT') || strstr($text, 'QT')) { | |
$this->twitter->favorite($status_id); | |
continue; | |
} | |
$status = $this->ultrasoul($text); | |
if ($status) { | |
$result = $this->twitter->reply($screen_name, array("status" => $status, "in_reply_to_status_id" => "$status_id")); | |
echo $result . "\n"; | |
} else { | |
$result = $this->twitter->reply($screen_name, array("status" => 'Ultra Soul!!(Hi!)', "in_reply_to_status_id" => "$status_id")); | |
echo $result . "\n"; | |
} | |
} | |
$value = $timeline->status[0]; | |
$last_mention['LastMention']['status_id'] = $value->id; | |
$last_mention['LastMention']['created_at'] = date('Y-m-d H:i:s', strtotime($value->created_at)); | |
$this->LastMention->save($last_mention); | |
} | |
/* | |
* 自動リフォロー | |
*/ | |
function refollow() | |
{ | |
$this->twitter->refollow(); | |
} | |
/* | |
* 自動フォロー | |
*/ | |
function follow() | |
{ | |
$results = $this->twitter->search('ウルトラソウル'); | |
foreach ($results as $result) { | |
$this->twitter->follow($result['screen_name']); | |
} | |
} | |
/* | |
* Ultra Soul!! | |
*/ | |
function ultrasoul($text) | |
{ | |
$ret = ''; | |
$text = r('@ultrasoul_bot ', '', $text); | |
$xml = $this->twitter->analysSentence($text, ''); | |
foreach ($xml->ma_result->word_list->word as $cur) { | |
$ret .= $cur->surface; | |
if (mb_strlen($ret) < 7) { | |
continue; | |
} | |
if ($cur->pos == '動詞' || $cur->pos == '助動詞' || $cur->pos == '助詞') { | |
$ret .= 'Ultra Soul!!(Hi!)'; | |
return $ret; | |
} | |
} | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment