]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/twitterstreamreader.php
Twitter streaming API reader: Cleanup input handling & split from HTTP headers to...
[quix0rs-gnu-social.git] / plugins / TwitterBridge / twitterstreamreader.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
7  * LICENCE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
22  * @author    Brion Vibber <brion@status.net>
23  * @copyright 2010 StatusNet, Inc.
24  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25  * @link      http://status.net/
26  */
27
28 // A single stream connection
29 abstract class TwitterStreamReader extends JsonStreamReader
30 {
31     protected $callbacks = array();
32
33     function __construct(TwitterOAuthClient $auth, $baseUrl)
34     {
35         $this->baseUrl = $baseUrl;
36         $this->oauth = $auth;
37     }
38
39     public function connect($method)
40     {
41         $url = $this->oAuthUrl($this->baseUrl . '/' . $method);
42         return parent::connect($url);
43     }
44
45     /**
46      * Sign our target URL with OAuth auth stuff.
47      *
48      * @param string $url
49      * @param array $params
50      * @return string 
51      */
52     function oAuthUrl($url, $params=array())
53     {
54         // In an ideal world this would be better encapsulated. :)
55         $request = OAuthRequest::from_consumer_and_token($this->oauth->consumer,
56             $this->oauth->token, 'GET', $url, $params);
57         $request->sign_request($this->oauth->sha1_method,
58             $this->oauth->consumer, $this->oauth->token);
59
60         return $request->to_url();
61     }
62     /**
63      * Add an event callback. Available event names include
64      * 'raw' (all data), 'friends', 'delete', 'scrubgeo', etc
65      *
66      * @param string $event
67      * @param callable $callback
68      */
69     public function hookEvent($event, $callback)
70     {
71         $this->callbacks[$event][] = $callback;
72     }
73
74     /**
75      * Call event handler callbacks for the given event.
76      * 
77      * @param string $event
78      * @param mixed $arg1 ... one or more params to pass on
79      */
80     public function fireEvent($event, $arg1)
81     {
82         if (array_key_exists($event, $this->callbacks)) {
83             $args = array_slice(func_get_args(), 1);
84             foreach ($this->callbacks[$event] as $callback) {
85                 call_user_func_array($callback, $args);
86             }
87         }
88     }
89
90     function handleJson(array $data)
91     {
92         $this->routeMessage($data);
93     }
94
95     abstract function routeMessage($data);
96
97     /**
98      * Send the decoded JSON object out to any event listeners.
99      *
100      * @param array $data
101      * @param int $forUserId
102      */
103     function handleMessage(array $data, $forUserId=null)
104     {
105         $this->fireEvent('raw', $data, $forUserId);
106
107         if (isset($data['id']) && isset($data['text']) && isset($data['user'])) {
108             $this->fireEvent('status', $data);
109         }
110
111         $knownMeta = array('friends', 'delete', 'scrubgeo', 'limit', 'event', 'direct_message');
112         foreach ($knownMeta as $key) {
113             if (isset($data[$key])) {
114                 $this->fireEvent($key, $data[$key], $forUserId);
115             }
116         }
117     }
118 }
119
120 class TwitterSiteStream extends TwitterStreamReader
121 {
122     protected $userIds;
123
124     public function __construct(TwitterOAuthClient $auth, $baseUrl='https://stream.twitter.com')
125     {
126         parent::__construct($auth, $baseUrl);
127     }
128
129     public function connect($method='2b/site.json')
130     {
131         return parent::connect($method);
132     }
133
134     function followUsers($userIds)
135     {
136         $this->userIds = $userIds;
137     }
138
139     /**
140      * Each message in the site stream tells us which user ID it should be
141      * routed to; we'll need that to let the caller know what to do.
142      *
143      * @param array $data
144      */
145     function routeMessage($data)
146     {
147         parent::handleMessage($data['message'], $data['for_user']);
148     }
149 }
150
151 class TwitterUserStream extends TwitterStreamReader
152 {
153     public function __construct(TwitterOAuthClient $auth, $baseUrl='https://userstream.twitter.com')
154     {
155         parent::__construct($auth, $baseUrl);
156     }
157
158     public function connect($method='2/user.json')
159     {
160         return parent::connect($method);
161     }
162
163     /**
164      * Each message in the user stream is just ready to go.
165      *
166      * @param array $data
167      */
168     function routeMessage($data)
169     {
170         parent::handleMessage($data, $this->userId);
171     }
172 }