Skip to content

Instantly share code, notes, and snippets.

@wktk
Created March 12, 2012 21:13
Show Gist options
  • Save wktk/2024716 to your computer and use it in GitHub Desktop.
Save wktk/2024716 to your computer and use it in GitHub Desktop.
EasyBotter の非推奨 API (Unversioned API 等) 対応
// <?php
/* 既存の "//自動フォロー返し function autoFollow()" を削除 or コメントアウトして
* EasyBotter.php 下部の "//基本的なAPIを叩く" 以下をこのファイルでまとめて上書き
*
* フォロー関連は XML に挫折したので json_decode() 使ってる
*/
// 基本的なAPIを叩く
function _setData($url, $value = array()){
return simplexml_load_string($this->consumer->sendRequest($url, $value, "POST")->getBody());
}
function _getData($url){
return simplexml_load_string($this->consumer->sendRequest($url,array(),"GET")->getBody());
}
function setUpdate($value){
return $this->_setData("https://api.twitter.com/1/statuses/update.xml", $value);
}
function getFriendsTimeline(){
$tl = $this->_getData("https://api.twitter.com/1/statuses/home_timeline.xml");
foreach ($tl as &$tweet) {
if (!$tweet->retweeted_status) unset($tweet);
}
return $tl;
}
function getReplies(){
return $this->_getData("https://api.twitter.com/1/statuses/mentions.xml");
}
function getFriends(){
return json_decode($this->consumer->sendRequest(
"https://api.twitter.com/1/friends/ids.json",array(),"GET")->getBody())->ids;
}
function getFollowers(){
return json_decode($this->consumer->sendRequest(
"https://api.twitter.com/1/followers/ids.json",array(),"GET")->getBody())->ids;
}
function followUser($screen_name){
return $this->_setData("https://api.twitter.com/1/friendships/create.xml", array('screen_name'=>$screen_name));
}
// 自動フォロー返し
// REST API 消費数: 2, or 3
function autoFollow() {
$followers = $this->getFollowers();
$friends = $this->getFriends();
$followList = array();
foreach ($followers as $follower) {
if (!in_array($follower, $friends)) $followList[] = $follower;
}
if ($followList) {
array_splice($followList, 100);
$ids = implode(',', $followList);
$users = json_decode($this->consumer->sendRequest(
"https://api.twitter.com/1/friendships/lookup.json?user_id={$ids}",array(),"GET")->getBody());
foreach($users as $user) {
if (in_array("following_requested", $user->connections)
|| in_array("following", $user->connections)) {}
elseif (in_array("followed_by", $user->connections)) {
$response = $this->followUser($user->screen_name);
if ($response->error) {
echo "@{$user->screen_name} ({$user->name}) へのフォロー返しは {$response->error} の為失敗しました。\n";
} else {
echo "@{$user->screen_name} ({$user->name}) をフォロー返ししました\n";
//$this->setUpdate(array(status=>"@{$user->screen_name} {$user->name} さん、フォローありがとう!"));
}
}
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment