-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtweeter.php
executable file
·70 lines (59 loc) · 1.78 KB
/
tweeter.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
#!/usr/bin/php
<?php
require_once(__DIR__ . '/amqp.inc');
include(__DIR__ . '/config.php');
$exchange = 'tweet_queue';
$queue = 'tweets';
$consumer_tag = 'consumer';
$response="";
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$ch = $conn->channel();
$ch->queue_declare($queue, false, true, false, false);
$ch->exchange_declare($exchange, 'direct', false, true, false);
$ch->queue_bind($queue, $exchange);
function post_tweet($tweet_text) {
global $response;
require_once('tmhOAuth.php');
$consumer_key = "xxxx";
$consumer_secret = "xxxx";
$connection = new tmhOAuth(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'user_token' => "xxxx",
'user_secret' => "xxxx",
));
$connection->request('POST', $connection->url('1.1/statuses/update'), array('status' => $tweet_text));
$response = $connection->response['response'];
print ("Response: $response\n");
return $connection->response['code'];
}
function process_message($msg) {
global $response;
$tweet = $msg->body;
echo "$tweet\n";
print "Posting...\n";
$result = post_tweet($tweet);
# print "Response code: " . $result . "\n";
if ($result == 200) {
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
} else {
# mail('[email protected]', '[auto-tweeter FAILED] $tweet', "$result - $response");
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}
$hour = date('H');
if ($hour > 21) {
sleep (60 * 60 * 12); # 12 hours
} else {
sleep (60 * 60); # 1 hour
}
}
$ch->basic_consume($queue, $consumer_tag, false, false, false, false, 'process_message');
function shutdown($ch, $conn){
$ch->close();
$conn->close();
}
register_shutdown_function('shutdown', $ch, $conn);
while(count($ch->callbacks)) {
$ch->wait();
}
?>