3 * StatusNet, the distributed open-source microblogging tool
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.
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.
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/>.
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/
29 * Base class for reading Twitter's User Streams and Site Streams
30 * real-time streaming APIs.
32 * Caller can hook event callbacks for various types of messages;
33 * the data from the stream and some context info will be passed
34 * on to the callbacks.
36 abstract class TwitterStreamReader extends JsonStreamReader
38 protected $callbacks = array();
40 function __construct(TwitterOAuthClient $auth, $baseUrl)
42 $this->baseUrl = $baseUrl;
46 public function connect($method, $params=array())
48 $url = $this->oAuthUrl($this->baseUrl . '/' . $method, $params);
49 return parent::connect($url);
53 * Sign our target URL with OAuth auth stuff.
56 * @param array $params
59 protected function oAuthUrl($url, $params=array())
61 // In an ideal world this would be better encapsulated. :)
62 $request = OAuthRequest::from_consumer_and_token($this->oauth->consumer,
63 $this->oauth->token, 'GET', $url, $params);
64 $request->sign_request($this->oauth->sha1_method,
65 $this->oauth->consumer, $this->oauth->token);
67 return $request->to_url();
71 * Add an event callback to receive notifications when things come in
74 * Callbacks should be in the form: function(object $data, array $context)
75 * where $context may list additional data on some streams, such as the
76 * user to whom the message should be routed.
82 * 'status': $data contains a status update in standard Twitter JSON format.
83 * $data->user: sending user in standard Twitter JSON format.
86 * 'direct_message': $data contains a direct message in standard Twitter JSON format.
87 * $data->sender: sending user in standard Twitter JSON format.
88 * $data->recipient: receiving user in standard Twitter JSON format.
94 * 'follow': User has either started following someone, or is being followed.
95 * $data->source: following user in standard Twitter JSON format.
96 * $data->target: followed user in standard Twitter JSON format.
98 * 'favorite': Someone has favorited a status update.
99 * $data->source: user doing the favoriting, in standard Twitter JSON format.
100 * $data->target: user whose status was favorited, in standard Twitter JSON format.
101 * $data->target_object: the favorited status update in standard Twitter JSON format.
103 * 'unfavorite': Someone has unfavorited a status update.
104 * $data->source: user doing the unfavoriting, in standard Twitter JSON format.
105 * $data->target: user whose status was unfavorited, in standard Twitter JSON format.
106 * $data->target_object: the unfavorited status update in standard Twitter JSON format.
112 * $data->friends: array of user IDs of the current user's friends.
114 * 'delete': Advisory that a Twitter status has been deleted; nice clients
115 * should follow suit.
116 * $data->id: ID of status being deleted
117 * $data->user_id: ID of its owning user
119 * 'scrub_geo': Advisory that a user is clearing geo data from their status
120 * stream; nice clients should follow suit.
121 * $data->user_id: ID of user
122 * $data->up_to_status_id: any notice older than this should be scrubbed.
124 * 'limit': Advisory that tracking has hit a resource limit.
127 * 'raw': receives the full JSON data for all message types.
129 * @param string $event
130 * @param callable $callback
132 public function hookEvent($event, $callback)
134 $this->callbacks[$event][] = $callback;
138 * Call event handler callbacks for the given event.
140 * @param string $event
141 * @param mixed $arg1 ... one or more params to pass on
143 protected function fireEvent($event, $arg1)
145 if (array_key_exists($event, $this->callbacks)) {
146 $args = array_slice(func_get_args(), 1);
147 foreach ($this->callbacks[$event] as $callback) {
148 call_user_func_array($callback, $args);
153 protected function handleJson(stdClass $data)
155 $this->routeMessage($data);
158 abstract protected function routeMessage(stdClass $data);
161 * Send the decoded JSON object out to any event listeners.
164 * @param array $context optional additional context data to pass on
166 protected function handleMessage(stdClass $data, array $context=array())
168 $this->fireEvent('raw', $data, $context);
170 if (isset($data->text)) {
171 $this->fireEvent('status', $data, $context);
174 if (isset($data->event)) {
175 $this->fireEvent($data->event, $data, $context);
178 if (isset($data->friends)) {
179 $this->fireEvent('friends', $data, $context);
182 $knownMeta = array('delete', 'scrub_geo', 'limit', 'direct_message');
183 foreach ($knownMeta as $key) {
184 if (isset($data->$key)) {
185 $this->fireEvent($key, $data->$key, $context);
193 * Multiuser stream listener for Twitter Site Streams API
194 * http://dev.twitter.com/pages/site_streams
196 * The site streams API allows listening to updates for multiple users.
197 * Pass in the user IDs to listen to in via followUser() -- note they
198 * must each have a valid OAuth token for the application ID we're
201 * You'll need to be connecting with the auth keys for the user who
202 * owns the application registration.
204 * The user each message is destined for will be passed to event handlers
205 * in $context['for_user_id'].
207 class TwitterSiteStream extends TwitterStreamReader
211 public function __construct(TwitterOAuthClient $auth, $baseUrl='http://betastream.twitter.com')
213 parent::__construct($auth, $baseUrl);
216 public function connect($method='2b/site.json')
219 if ($this->userIds) {
220 $params['follow'] = implode(',', $this->userIds);
222 return parent::connect($method, $params);
226 * Set the users whose home streams should be pulled.
227 * They all must have valid oauth tokens for this application.
229 * Must be called before connect().
231 * @param array $userIds
233 function followUsers($userIds)
235 $this->userIds = $userIds;
239 * Each message in the site stream tells us which user ID it should be
240 * routed to; we'll need that to let the caller know what to do.
244 function routeMessage(stdClass $data)
247 'source' => 'sitestream',
248 'for_user' => $data->for_user
250 parent::handleMessage($data->message, $context);
255 * Stream listener for Twitter User Streams API
256 * http://dev.twitter.com/pages/user_streams
258 * This will pull the home stream and additional events just for the user
259 * we've authenticated as.
261 class TwitterUserStream extends TwitterStreamReader
263 public function __construct(TwitterOAuthClient $auth, $baseUrl='https://userstream.twitter.com')
265 parent::__construct($auth, $baseUrl);
268 public function connect($method='2/user.json')
270 return parent::connect($method);
274 * Each message in the user stream is just ready to go.
278 function routeMessage(stdClass $data)
281 'source' => 'userstream'
283 parent::handleMessage($data, $context);