-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtwitterUsersDaemon.php
89 lines (70 loc) · 3.03 KB
/
twitterUsersDaemon.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
88
89
<?php
require_once __DIR__ . '/vendor/autoload.php';
$daemon = new Firehed\ProcessControl\Daemon;
declare (ticks=1);
$daemon->setUser('root')
->setPidFileLocation('/var/run/tj-twitter-users.pid')
->setStdoutFileLocation(sys_get_temp_dir() . '/tj-twitter-users.log')
->setStdErrFileLocation(sys_get_temp_dir() . '/tj-twitter-users-err.log')
->setProcessName('tj-twitter-users')
->autoRun();
define('TWITTER_CONSUMER_KEY', 'app key'); // changeme
define('TWITTER_CONSUMER_SECRET', 'app secret'); // changeme
class FilterTrackConsumer extends OauthPhirehose
{
private $_track = [];
private $_trackUpdate = 0;
public function enqueueStatus($status)
{
$data = json_decode($status, true);
if (is_array($data) && isset($data['user']['screen_name'])) {
Gear::doBackground('tweet_process', serialize($data)); // this is sending to background worker (gearman, rabbitmq)
// see tweet_process.php
echo date('d.m H:i:s') . " +\n";
} else {
echo date('d.m H:i:s') . $status . "\n";
}
}
public function checkFilterPredicates()
{
if (empty($this->_track) || $this->_trackUpdate < time() - 300) {
$this->_track = $this->getTrackIds();
$this->_trackUpdate = time();
echo date('d.m H:i:s') . " . " . count($this->_track) . "\n";
}
$size = memory_get_usage();
$unit = array('b','kb','mb','gb','tb','pb');
echo @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i] . "\n";
$this->setFollow($this->_track);
}
public function getTrackIds()
{
try {
// Тут из базы берём id чуваков, которых отслеживаем
$tweople = \DB\Twitter\TweopleList::select('twitter_tweople_lists.tweople_id')
->distinct()
->inner_join('twitter_lists', ['twitter_tweople_lists.list_id', '=', 'twitter_lists.id'])
->where('twitter_lists.is_active', 1)
->find_array();
$result = [];
if (count($tweople)) {
foreach ($tweople as $tw) {
$result[] = intval($tw['tweople_id']);
}
$result = array_unique($result);
if (count($result) > 5000) {
\Log::addError('TWTR: There is 5000 people max');
$result = array_slice($result, 0, 5000);
}
}
unset($tweople);
return $result;
} catch (Exception $e) {
\Log::addException($e);
}
return [];
}
}
$sc = new FilterTrackConsumer('user token', 'user secert key', Phirehose::METHOD_FILTER);
$sc->setFollow($sc->getTrackIds());
$sc->consume();