Repository navigation
Expand file tree
/
Copy pathconsumeTwitter.php
More file actions
119 lines (95 loc) · 3.54 KB
/
Copy pathconsumeTwitter.php
File metadata and controls
119 lines (95 loc) · 3.54 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* Twitter Stream Sucker -> MQ Injector
* As wanted by Twitter's API doc, the deamon connecting to the stream service shall not process anything,
* just consume for later use, so let's do as they say !
* @author JaXX.org
*/
require_once('php-amqplib/amqp.inc');
require_once('phirehose/lib/Phirehose.php');
// define('AMQP_DEBUG', true);
$conf = parse_ini_file('conf.php',TRUE);
class consumeTwitter extends Phirehose
{
/**
* Phirehose needs us to extend the class and reimplement enqueueStatus
*
* @param string $status
*/
static private $mqconnection;
static private $mqexchange;
static private $mqchannel;
static private $ex;
static private $qu;
public function __construct($username, $password, $method = Phirehose::METHOD_SAMPLE, $format = self::FORMAT_JSON)
{
global $conf;
self::$ex=$conf['consumer']['amqp_exchange_raw'];
self::$qu=$conf['consumer']['amqp_queue_raw'];
// Let's connect to AMQP too
self::$mqconnection = new AMQPConnection($conf['amqp']['host'],$conf['amqp']['port'],$conf['amqp']['user'],$conf['amqp']['pass'],$conf['amqp']['vhost']);
self::$mqchannel = self::$mqconnection->channel();
$args=array();
foreach($conf['consumer']['amqp_queue_args'] as $amqpargs){
// echo $amqpargs."\n";
$_args=split(',',$amqpargs);
$args=array_merge($args,array($_args[0]=> array($_args[1],$_args[2])));
}
print_r($args); // die();
// queue_declare ( $queue="", $passive=false, $durable=false, $exclusive=false, $auto_delete=true, $nowait=false, $arguments=NULL, $ticket=NULL )
self::$mqchannel->queue_declare(
self::$qu, // QueueName string
false,
true,
false,
false,
false,
$args,
NULL);
self::$mqchannel->exchange_declare(self::$ex, 'topic', false, true, false);
self::$mqchannel->queue_bind(self::$qu, self::$ex);
$msg = new AMQPMessage("Connected to AMQP Broker, jusqu'ici, tout va bien", array('content_type' => 'text/plain'));
self::$mqchannel->basic_publish($msg, self::$ex);
parent::__construct($username, $password, $method = Phirehose::METHOD_SAMPLE, $format = self::FORMAT_JSON);
}
public function enqueueStatus($status)
{
/*
* In this simple example, we will just display to STDOUT rather than enqueue.
* NOTE: You should NOT be processing tweets at this point in a real application, instead they should be being
* enqueued and processed asyncronously from the collection process.
*/
$data = json_decode($status, true);
if (is_array($data) && isset($data['user']['screen_name'])) {
print $data['user']['screen_name'] . ': ' . urldecode($data['text']) . "\n";
}
// print_r($data);
$msg = new AMQPMessage($status, array('content_type' => 'application/json'));
self::$mqchannel->basic_publish($msg, self::$ex);
}
public function __destruct()
{
self::$mqconnection->close();
self::$mqchannel->close();
}
}
// Start streaming
//echo $conf['consumer']['tw_username'].$conf['consumer']['tw_password']; exit();
$str = new consumeTwitter($conf['consumer']['tw_username'], $conf['consumer']['tw_password'], Phirehose::METHOD_FILTER);
if($conf['consumer']['stream_type'] == "LocationsByCircle"){
$locations=array();
foreach($conf['consumer']['locationbycircle'] as $confloc){
$locations[]=split(',',$confloc);
}
print_r($locations);
$str->setLocationsByCircle($locations);
}
if($conf['consumer']['stream_type'] == "LocationsByRect"){
$locations=array();
foreach($conf['consumer']['locationbyrect'] as $confloc){
$locations[]=split(',',$confloc);
}
print_r($locations);
$str->setLocations($locations);
}
$str->consume();