twitter BOT作成しようとしてみた

cakephptwitterbot作成に挑戦してみました


使ったのは、twitter datasource。ちょー便利!
cakePHP用のtwitterマッシュアップみたいです。

まずは基本形を指示通りに進めていきます


1,app/models/datasources に twitter_source.php を入れる

2,データベースとテーブル作成


CREATE DATABASE `caketweet`;
USE `caketweet`;
CREATE TABLE IF NOT EXISTS `tweets` (
`id` int(11) NOT NULL auto_increment,
`twitter_username` varchar(255) NOT NULL,
`tweet_content` text NOT NULL,
`created` datetime default NULL,
`updated` datetime default NULL,
PRIMARY KEY (`id`)
);


3,app/config/database.phpを書き換える


<?phpclass DATABASE_CONFIG {

    var $default = array(
        
'driver' => 'mysql',
        
'persistent' => false,
        
'host' => 'localhost',
        
'login' => '自分のuser',
        
'password' => '自分のpass',
        
'database' => 'caketweet',
        
'prefix' => '',
    );

    var $twitter = array(
        
'datasource' => 'twitter',
        
'username' => '自分のID',
        
'password' => '自分のpass',
    ); 


4,app/models/tweet.php


<?phpclass Tweet extends AppModel {
    var 
$name 'Tweet';


5,app/controllers/tweets_controller.php


<?phpclass TweetsController extends AppController {
    
    var 
$name 'Tweets';
    
    function 
index(){
        
$this->set('tweets'$this->paginate());
    }
    function 
search(){
        if(!empty(
$this->data['Tweet']['keyword'])){
            
$this->Twitter ConnectionManager::getDataSource('twitter');
            
$search_results $this->Twitter->search($this->data['Tweet']['keyword'], 'all'5);
            
// let's loop through tweets
            
foreach($search_results['Feed']['Entry'] as $rawtweet){
                
// format to our needs
                
$i explode(' '$rawtweet['Author']['name']);
                
$tweet['Tweet']['twitter_username'] = $i[0];
                
$tweet['Tweet']['tweet_content'] = $rawtweet['content']['value'];
                
$tweet['Tweet']['created'] = date('Y-m-d H:i:s' strtotime($rawtweet['published']));
                
$tweet['Tweet']['updated'] = date('Y-m-d H:i:s' ,strtotime($rawtweet['updated']));
                
// and save
                                
$this->Tweet->create();            
                
$this->Tweet->save($tweet);
            }
            
$this->Session->setFlash(__('Got tweets.'true));
        }    
    }


6,app/views/tweets/index.ctp


<div class="tweets index">
<
h2><?php __('Tweets');?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
    <th><?php echo $paginator->sort('id');?></th>
    <th><?php echo $paginator->sort('twitter_username');?></th>
    <th><?php echo $paginator->sort('tweet_content');?></th>
    <th><?php echo $paginator->sort('created');?></th>
    <th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i 
0;
foreach (
$tweets as $tweet):
    
$class null;
    if (
$i++ % == 0) {
        
$class ' class="altrow"';
    }
?>    <tr<?php echo $class;?>>
        <td>
            <?php echo $tweet['Tweet']['id']; ?>        </td>
        <td>
            <?php echo $tweet['Tweet']['twitter_username']; ?>        </td>
        <td>
            <?php echo $tweet['Tweet']['tweet_content']; ?>        </td>
        <td>
            <?php echo $tweet['Tweet']['created']; ?>        </td>
        <td class="actions">
           <?php echo $html->link(__('Delete'true), array('action'=>'delete'$tweet['Tweet']['id']), nullsprintf(__('Are you sure you want to delete # %s?'true), $tweet['Tweet']['id'])); ?>        </td>
    </tr>
<?php endforeach; ?></table>
</div>
<div class="paging">
    <?php echo $paginator->prev('&#0171;' .__('prev'true), array('escape' => false), null, array('class'=>'disabled''escape' => false));?> |  <?php echo $paginator->numbers();?>    <?php echo $paginator->next(__('next'true).' &#0187;', array('escape' => false), null, array('class'=>'disabled''escape' => false));?></div>
<div class="actions">
    <ul>
        <li><?php echo $html->link(__('Search tweets'true), array('action'=>'search')); ?></li>
    </ul>
</div>


7,app/views/tweets/search.ctp


<div class="tweets form">
<?
php echo $form->create('Tweet', array('action' => 'search'));?>    <fieldset>
         <legend><?php __('Search tweet');?></legend>
    <?php
        
echo $form->input('keyword');
    
?>    </fieldset>
<?php echo $form->end('Search');?></div>
<div class="actions">
    <ul>
        <li><?php echo $html->link(__('List tweets'true), array('action'=>'index'));?></li>
    </ul>
</div> 

!注意!
最新版のcakephp使ってる人は、cake/libs/xml.phpの記述が _killParent (アンダースコアひとつ)になってるので、
twitter_datasource の __killParent を _killParent に修正してください。



アクセス→http://yourhost/caketweet/tweets/search

で検索した、ツイートがデータベースに5件ずつ保存されるはずです!!




BOT作りたいので、これをちょっとずつ変えて行きたいと思います。


app/controllers/tweets_controller.php


<?phpclass TweetsController extends AppController {
    
    var 
$name 'Tweets';
    
    function 
index(){
        
$this->set('tweets'$this->paginate());
    }
    function 
search(){
        if(!empty(
$this->data['Tweet']['keyword'])){
            
$this->Twitter ConnectionManager::getDataSource('twitter');

            //記述する値をツイートしてくれる
            
$this->Twitter->status_update($this->data['Tweet']['keyword']);

            $this->Session->setFlash(__('Got tweets.'true));

        }    
    }
}

ということは、$this->Twitter->status_updateに好きな値を自動的に取得して、
自動的に更新させれば、BOTの完成ということですね!!!



自動的に更新するのはcronを使用するようです。

rssからとってきた情報をつぶやくBOT作ろうと思ってます。

まだ、どんなのにするかは未定