public
Description: A library intended to allow easy creation of Bots using the Twitter website
Homepage: http://twitbot.com
Clone URL: git://github.com/NeilCrosby/twitbot.git
twitbot / bot_runner.php
100644 69 lines (54 sloc) 2.164 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
function __autoload($class) {
    $aLocations = array('classes', 'api', 'bot');
    
    foreach( $aLocations as $location ) {
        $file = "$location/$class.php";
        if ( file_exists( $file ) ) {
            include_once( $file );
            return;
        }
    }
 
    // Check to see if we managed to declare the class
    if (!class_exists($class, false)) {
        trigger_error("Unable to load class: $class", E_USER_WARNING);
    }
}
 
 
$aValidTypes = array(
    'echo' => array('class' => 'EchoTwitBot', 'table' => 'tbl_bot_echo'),
    'rss' => array('class' => 'RssTwitBot', 'table' => 'tbl_bot_rss'),
    'question' => array('class' => 'QuestionTwitBot', 'table' => 'tbl_bot_question'),
    'werewolf' => array('class' => 'WerewolfTwitBot', 'table' => 'tbl_bot_werewolf'),
    'friendly' => array('class' => 'FriendlyTwitBot', 'table' => 'tbl_bot_friendly'),
);
 
if ( !isset($_GET['type']) || !array_key_exists( $_GET['type'], $aValidTypes ) ) {
    echo "<p>Invalid type</p>";
    exit();
}
 
// Set up DB uname/pword, error reporting etc.
new Config();
 
$botType = $aValidTypes[$_GET['type']];
$botClass = $botType['class'];
$botTable = $botType['table'];
 
$safeBotTable = SQL::makeSafe($botTable);
$sql = "SELECT *
FROM tbl_bot
INNER JOIN $safeBotTable ON $safeBotTable.uid = tbl_bot.uid";
$aBots = SQL::doReadQuery($sql);
 
foreach ( $aBots as $aBotVars ) {
    $aBotVars['last_data_time'] = strtotime($aBotVars['last_data_time']);
    $lastDataTime = $aBotVars['last_data_time'];
    
    $bot = new $botClass( $aBotVars );
    $bot->run();
    $newLastDataTime = $bot->getLastDataTime();
    
    echo "<p>{$aBotVars['username']} $lastDataTime $newLastDataTime</p>";
    echo '<p>'.date("D M j H:i:s +0000 Y", $newLastDataTime).'</p>';
    
    if ( $newLastDataTime != $lastDataTime ) {
        $safeUid = SQL::makeSafe($aBotVars['uid']);
        $safeNewLastDataTime = SQL::makeSafe(date('Y-m-d H:i:s', $newLastDataTime));
        
        $sql = "UPDATE $safeBotTable
SET last_data_time = '$safeNewLastDataTime'
WHERE uid = '$safeUid'";
        SQL::doWriteQuery($sql);
    }
}
 
?>