最近、twitterでは直近10postと同じ内容を投稿しようとするとduplicate textエラーが出るという規制が導入されたようなので(Twitter、重複つぶやきをブロック開始)、その対策バージョンです。
例によって同時にソースコードも公開しております。
最近投稿された10件分のpostを記録し、それと同じ内容のpostをしない仕様になっております。よってその性質上、botの発言内容ファイルには11件以上のpost内容が入っている必要があるので、それより少ない場合は増やしてからお使いください。
新たに追加されたlastlog.txtが最新10件分を記録するファイルになっています。本体と同じディレクトリにアップロードした後、パーミッションを666に変更してください。
既にver.1.0を使っている人はlastlog.txtファイルに最近のbotの発言10件分をコピーして格納しておくといいと思います。その場合は文字コードをUTF-8に変更するのをお忘れなく。
また、記録しておくpostの数を適宜調整すれば、同じ内容を短期間の間に繰り返すということを阻止できるかもしれません。$lastmax = 10;のところの数字を変更してください。
<?
/* ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Twitter Post ver.1.1
https://palemag.kazeki.net/blog/
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ */
$username = "アカウント";
$password = "パスワード";
$logfile = "bot.txt";
$lastlog = "lastlog.txt"; // 最後の10post分を記録するファイル
$lastmax = 10; // $lastlogに記録しておくpostの件数
$andromarius = file($logfile);
srand(time());
shuffle($andromarius);
$baalu = file($lastlog);
if(count($baalu) < count($andromarius)){
$lastpost = file_get_contents($lastlog);
$i = 0;
while(preg_match("/$andromarius[$i]/u", $lastpost)){
$i++;
}
}
else{
echo "Error: postさせる文章は".$lastmax."件以上記録してください。";
}
$message = $andromarius[$i];
$fp = fopen($lastlog, "w");
fputs($fp, $message);
for ($j=0;$j<$lastmax-1;$j++) {
fputs($fp, $baalu[$j]);
}
fclose($fp);
$params = "status=". rawurlencode($message);
$url = "http://twitter.com/statuses/update.xml?";
$fp = fopen($url.$params, 'r', false, stream_context_create(array(
"http" => array(
"method" => "POST",
"header" => "Authorization: Basic ". base64_encode($username. ":". $password)
)
)));
$result = "";
while(!feof($fp)){
$result .= fgets($fp);
}
fclose($fp);
?>