-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
executable file
·87 lines (73 loc) · 2.54 KB
/
bot.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
require_once __DIR__ . '/bootstrap.php';
use Makotokw\TwientBot\Bot;
try {
$flags = new donatj\Flags();
$flags->string('executed-at', '');
$flags->string('timezone', '');
$flags->parse();
$command = $flags->arg(0);
switch ($command) {
case 'tweet':
case 'post':
$screenName = $flags->arg(1);
if (empty($screenName)) {
throw new Exception("Usage: bot.php tweet screen-name");
}
$bot = createBot($screenName);
$option = $flags->longs();
if ($option['executed-at'] != '') {
$executedOn = explode(',', $option['executed-at']);
$timeZone = ($option['timezone'] != '') ? new DateTimeZone($option['timezone']) : null;
$now = new DateTime('now', $timeZone);
$nowHour = $now->format('H');
if (!in_array($nowHour, $executedOn) && !in_array(intval($nowHour), $executedOn)) {
exit;
}
}
$s = $bot->randomPost();
if (is_array($s)) {
echo sprintf('https://twitter.com/%s/status/%s', $s['user']['screen_name'], $s['id_str']) . PHP_EOL;
}
break;
case 'cache:dump':
$screenName = $flags->arg(1);
if (empty($screenName)) {
throw new Exception("Usage: bot.php cache:dump screen-name");
}
$bot = createBot($screenName);
$bot->dumpCache();
break;
case 'cache:clear':
$screenName = $flags->arg(1);
if (empty($screenName)) {
throw new Exception("Usage: bot.php cache:clear screen-name");
}
$bot = createBot($screenName);
$bot->clearCache();
break;
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
/**
* @param string $screenName
* @return Bot
* @throws Exception
*/
function createBot($screenName)
{
$upperScreenName = strtoupper($screenName);
$userToken = getenv($upperScreenName . '_TOKEN');
$userSecret = getenv($upperScreenName . '_SECRET');
if (empty($userToken) || empty($userSecret)) {
throw new Exception("undefined {$upperScreenName}_TOKEN or {$upperScreenName}_SECRET");
}
$bot = new Bot(APP_CONSUMER_KEY, APP_CONSUMER_SECRET);
$bot->setDataDir(__DIR__ . '/data/');
if (defined('REDIS_URL')) {
$bot->setRedisUrl(REDIS_URL);
}
$bot->setUser($screenName, $userToken, $userSecret);
return $bot;
}