Tweet Deleter

twitterの自分の発言を定期的に削除していくbotの作り方です。略してデリったー。

twitter apiが9月から完全にベーシック認証を廃止し、oauthに完全移行したため作り直しを余儀なくされたので備忘録も含めての投稿。

oauthの概要、oauthを使うための環境作り、appの登録からconsumer keyやconsumer secretの取得などは調べればいくらでも参考サイトがでてくるので割愛。

まずは得たconsumer key, secretのペアからaccess key, tokenを取得する方法。今回はブラウザアプリではなく、クライアントアプリなのでコマンドラインから実行できるphpスクリプトを作成。

以下のスクリプトをコマンドラインから実行。URLが表示されるのでブラウザでURLにアクセス、PINコードを入手、再度スクリプトの入力受付に入力、access key, tokenの取得となります。


require_once 'HTTP/OAuth/Consumer.php';

define('CONSUMER_KEY',    'XXXXXXXXXXXXXXXXXX');
define('CONSUMER_SECRET', 'XXXXXXXXXXXXXXXXXX');

try
{
  $http_request = new HTTP_Request2();
  $http_request->setConfig('ssl_verify_peer', false);

  $consumer_request = new HTTP_OAuth_Consumer_Request();
  $consumer_request->accept($http_request);

  $consumer = new HTTP_OAuth_Consumer(CONSUMER_KEY, CONSUMER_SECRET);
  $consumer->accept($consumer_request);

  $consumer->getRequestToken('http://twitter.com/oauth/request_token');
  $url = $consumer->getAuthorizeUrl('http://twitter.com/oauth/authorize');

  if (!$stdin = fopen('php://stdin', 'r'))
  {
    throw new Exception('stdin open failed');
  }

  echo 'Visit the following URL in your browser to authenticate twitter: ';
  echo $url."\n";
  echo 'Enter Twitter OAuth PIN: ';
  $pin = trim(fgets($stdin, 64));
  $consumer->getAccessToken('http://twitter.com/oauth/access_token', $pin);

  echo 'access_token: '.$consumer->getToken()."\n";
  echo 'access_token_secret: '.$consumer->getTokenSecret()."\n";

}
catch (Exception $e)
{
  echo $e->getMessage().' in '.$e->getFile().' line '.$e->getLine()."\n";
  exit(1);
 }

後はbot本体のコード。かなり単純明快。

自分のTLの1ページから5ページを巡回して、存在するtweetを削除。ただし1ページ目に関しては15以上のtweetのみ削除。定期実行の間隔で発言する量にもよるけど、5ページも巡回する必要は実はなかったり。あとapiの制限を受けないように1ページ毎に60秒のsleep付き。


require_once('twitteroauth.php');

$consumer_key = "XXXXXXXXXXXXXXXXXX";
$consumer_secret = "XXXXXXXXXXXXXXXXXX";
$access_token = "XXXXXXXXXXXXXXXXXX";
$access_token_secret = "XXXXXXXXXXXXXXXXXX";

$to = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);

$pagenum = 1;
while ($pagenum <= 5)
{
  $results = getpage($pagenum);
  $xml = new SimpleXMLElement($results);

  for ($i = 0; $i < count($xml->status); $i++)
  {
    if ($pagenum == 1 && $i < 15)
      continue;
    delete_tweet($xml->status[$i]->id);
  }

  sleep(60);
  $pagenum++;
}

function delete_tweet($id)
{
  global $to;

  return $to->OAuthRequest("http://twitter.com/statuses/destroy/$id.xml",
                           'POST', array());
} 

function getpage($pagenum)
{
  global $to;

  return $to->OAuthRequest("http://twitter.com/statuses/user_timeline.xml?page=$pagenum",
                           'GET', array());
} 

後はこのプログラムがサーバ上で定期的に実行されるようにcronなりatなりに登録するだけです。僕は20分毎に起動するように登録してあります。

This entry was posted in diary. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. hgd
    Posted 09/03/2010 at 4:21 PM | Permalink

    僕のもやってもらえませんか?
    ファッキンgone days

  2. hc100
    Posted 09/03/2010 at 4:43 PM | Permalink

    いいですよ♪

  3. hgd
    Posted 09/06/2010 at 10:47 PM | Permalink

    やったー

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • http://twitter.com/hc100
  • www.flickr.com
    This is a Flickr badge showing public items from hardcore100 tagged with iphoneography. Make your own badge here.

  • PROFILE