twitterの自動フォローbotを「ミュート」の人を排除するように改良

2016年6月1日

PHP Tips テクノロジー プログラミング

1年ほど運用してきたtwitter-botですが、おかげさまでフォロワー数が1万ユーザーを越えましたが、最近気がついたんですが、かなりの数がアフェリエイト用アカっぽいんですよね。 今現在のtwitterアカウントはブログ紹介用のアカウントで使っていますが、毎日20人ぐらいのフォローがあるんですが、その大半がエロアカウントや、読めない外国人のフォローです。

ほんとにウザいアフェリアカ

これまでは100%フォロー返しでやっていましたが、この間、思い立って、自分のタイムラインに入ってきてほしくないユーザーを「ミュート」設定してみました。 そりゃあもう、かなりの数だったので、腕が腱鞘炎になる勢いです。 そして、今後はbotでの自動フォローについても、ミュートしている人はフォローしないようにすることで、タイムラインが綺麗になるのと、不用なユーザーをフォローせずに必要な人がフォローできるようになるので、以前コードを改良して下記コードを生成しました。

事前準備

twitter-Oauthライブラリが必要なので事前にgit-cloneしておきましょう。 git clone https://github.com/abraham/twitteroauth.git これの同一階層に、下記ソースを設置します。 あと、twitter-Oauthの認証キーは以前のブログを参考にセットしておいてください。 そしてjsonで下記のようにkey.jsonファイルを作成しておきましょう。 Twitterのbotを作る #1「下準備編:投稿だけなら簡単にできる」 { "%name%":{ "consumer_key":"***", "consumer_secret":"***", "access_token":"***", "access_token_secret":"***" } }

コード

<?php /** * Twitter-bot-controller * [Env] * PHP 5.6 * * [Library] * Twitter/Oauth @ git clone https://github.com/abraham/twitteroauth.git * * [Mode] * mode @ postUnfollow * debug @ true * max @ max-prox (default:50) * * [Howto] * Ex) php twitter.php mode=postUnfollow user=*** debug=true max=100 **/ date_default_timezone_set('Asia/Tokyo'); //CLIの場合argvをREQUESTに変換する。 if(!isset($_SERVER['SCRIPT_URI']) && isset($argv)){ for($i=0,$c=count($argv);$i<$c;$i++){ if(!$argv[$i]){continue;} //各クエリの分解 $q = explode("=",$argv[$i]); if(count($q)<2){continue;} if($q[0]!=''){ //requestに格納 $key = $q[0]; $val = join("=",array_slice($q,1)); $_REQUEST[$key]=$val; } } } //ライブラリ処理 require 'twitteroauth/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; $twitter = new Twitter(); if($_REQUEST["mode"]=="postUnfollow"){ $twitter->postUnfollow(); } class Twitter{ var $connection; var $max_count = 50; function __construct(){ //keyデータ処理 $key_json = json_decode(file_get_contents("./key.json"),true); //keyの対象user処理 $key = array(); //twitterアカウント※指定なしの場合のデフォルト if(!isset($_REQUEST['user']) || !$_REQUEST['user']){ die("ERROR : not-user".PHP_EOL); } $user = $_REQUEST['user'] ; //Error if(!$key_json[$user]){die("not-user-query[".$user."]"."\n");} //load-key $key = $key_json[$user]; $connection = new TwitterOAuth($key['consumer_key'],$key['consumer_secret'],$key['access_token'],$key['access_token_secret']); $this->connection = $connection; } //未フォローの人を友達にする function postUnfollow(){ $unfollows = $this->getNonFriends(); echo "count : ".count($unfollows).PHP_EOL; $cnt = 0; for($i=count($unfollows)-1;$i>=0;$i--){ if($cnt > $this->max_count){break;} if(!isset($_REQUEST["debug"])){ $res = $this->connection->post('friendships/create', array("id"=>$unfollows[$i])); echo "post : ".$unfollows[$i]." / ".PHP_EOL; //print_r($res); $cnt++; } else{ echo "post-debug : ".$unfollows[$i].PHP_EOL; } } } // フォローされているが友達でない人の一覧を取得 function getNonFriends(){ $follow = $this->getFollowers(); $mutes = $this->getMutes(); $friend = $this->getFriends(); //友達済み、ミュートチェック $res = array(); for($i=0;$i<count($follow);$i++){ if(!$follow[$i]){continue;} //friend if(in_array($follow[$i],$friend)){continue;} //mutes if(in_array($follow[$i],$mutes)){continue;} //data-add $res[] = $follow[$i]; } return $res; } // Followers(フォローしてくれている人の一覧) function getFollowers($cursor=-1){ return $this->getIDS('followers/ids',$cursor); } // Mutes(ミュートしている人の一覧) function getMutes($cursor=-1){ return $this->getIDS('mutes/users/ids',$cursor); } // Friends(フォローしている人の一覧) function getFriends($cursor=-1){ return $this->getIDS('friends/ids',$cursor); } //TwitterDatabaseからデータの取得処理 function getIDS($mode="",$cursor=-1){ if($mode==""){return $res;} $data = $this->connection->get($mode, array('cursor' => $cursor)); $res = $data->ids; while($data->next_cursor){ $data = $this->connection->get($mode, array('cursor' => $data->next_cursor)); $res = array_merge($res ,$data->ids); } return $res; } // TwitterのUID->info function getInfo($uid){ if(!$uid){return null;} $userinfo = $this->connection->get('users/show', ['id'=> $uid]); return array( "name"=>$userinfo->name, "description"=>$userinfo->description ); } }

使い方

下記コマンドを実行すると、フォローしてくれている人かつミュートしていない人のみ自動でフォローしていきます。 $ php twitter.php mode=postUnfollow name=%name[jsonにセットした任意名]% とりあえず制限をつけていて、1回で最大50ユーザーのみにしています。 何度もコマンドを叩くことで何度もできますが、あまり大量にやり過ぎると、keyがロックされてしまうので、気をつけてください。

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。