Twitterのbot作成によく使われるEasyBotterをカスタマイズしてみます。
phpでTwitter botが作れるEasyBotter
pha氏が作成・公開しているEasyBotterをカスタマイズしてみます。
[link url=”http://pha22.net/twitterbot/”]
日本語の人だけフォローする
autoFollow()をオンにしておくと、フォローしてくれた人を自動でフォローバックしてくれて便利なのですが、異国の方々が一気に増えることがあります。
そこでプロフィールに日本語がある人だけをフォローするカスタマイズを行いました。
まずは autoFollow() を書き換える
こんな感じに差し替えます。
[php title=”EasyBotter.php” start-line=”227″ mark=”4-30″] return $results;
}
//自動フォロー返しする(プロフィールに日本語がある人だけ)
function autoFollow(){
$followers = $this->getFollowers();
$friends = $this->getFriends();
$followlist = array_diff($followers[“ids”], $friends[“ids”]);
if($followlist){
$j =0;
for ($i = 0; $i < count($followlist); $i++ ) {
$j++;
$user_list[] = $followlist[$i];
if ($j >= 100 or $i == count($followlist)) {
$profiles = $this->getProfiles($user_list);
foreach ($profiles as $user) {
$text = $user[‘name’] . $user[‘description’] . $user[‘status’][‘text’];
if (preg_match(‘/[ぁ-んァ-ヶ]/u’, $text)) {
$response = $this->followUser($user[‘id’]);
if(empty($response[“errors”])){
echo $response[“name”].”(@“.$response[“screen_name”].”)をフォローしました”;
}
}
}
$j = 0;
unset($user_list);
}
}
}
}
//============================================================
//上の5つの関数から呼び出す関数
//============================================================[/php]
未フォロワーリストから100件ずつプロフィールを読み込んで、「ひらがな」か「カタカナ」がある場合のみフォローしに行っています。
100件ずつなのはAPIの制限です。
プロフィールを取得する関数を作成する
こんな感じで getFollowers() の下辺りに追加します。
[php title=”EasyBotter.php” start-line=”570″ mark=”10-22″] function getFollowers()
{
$url = “https://api.twitter.com/1.1/followers/ids.json”;
$response = $this->_getData($url);
if($response[“errors”]){
echo $response[“errors”][0][“message”];
}
return $response;
}
function getProfiles($user_id = NULL)
{
$url = “https://api.twitter.com/1.1/users/lookup.json?”;
if ($user_id) {
$url .= ‘user_id=’ . implode(‘,’,$user_id) .”&”;
}
$url .= “include_entities=true”;
$response = $this->_getData($url);
if(isset($response[“errors”])){
echo $response[“errors”][0][“message”];
}
return $response;
}
function checkApi($resources = “statuses”){
$url = “https://api.twitter.com/1.1/application/rate_limit_status.json”;
if ($resources) {
$url .= ‘?resources=’ . $resources;
}[/php]
完成
これでプロフィールに日本語がある人だけをフォローするようになりました。
日本語といっても漢字だけの人は中国の方と判断が難しいので「ひらがな」か「カタカナ」が1文字でも含まれている人です。
記号としてひらがなを使っている異国の方はフォロー返しをしてしまいます。
言語設定が日本語の人だけフォローする
しばらくしてから気づきましたが、言語設定が「日本語」の人という条件にすれば良い気がしてきました。
こんな感じになります。
[php title=”EasyBotter.php” start-line=”230″ mark=”1,14″] //自動フォロー返しする(日本語設定の人だけ)
function autoFollow(){
$followers = $this->getFollowers();
$friends = $this->getFriends();
$followlist = array_diff($followers[“ids”], $friends[“ids”]);
if($followlist){
$j =0;
for ($i = 0; $i < count($followlist); $i++ ) {
$j++;
$user_list[] = $followlist[$i];
if ($j >= 100 or $i == count($followlist)) {
$profiles = $this->getProfiles($user_list);
foreach ($profiles as $user) {
if ($user[‘lang’] == ‘ja’) {
$response = $this->followUser($user[‘id’]);
if(empty($response[“errors”])){
echo $response[“name”].”(@“.$response[“screen_name”].”)をフォローしました”;
}
}
}
$j = 0;
unset($user_list);
}
}
}
}[/php]
この方がすっきりして良いですね。
誰でもやりたそうなカスタマイズなのに全然情報が無かったですね。
では、この辺で。(^-^)o
コメント