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/
28 // A single stream connection
29 abstract class TwitterStreamReader extends JsonStreamReader
31 protected $callbacks = array();
33 function __construct(TwitterOAuthClient $auth, $baseUrl)
35 $this->baseUrl = $baseUrl;
39 public function connect($method)
41 $url = $this->oAuthUrl($this->baseUrl . '/' . $method);
42 return parent::connect($url);
46 * Sign our target URL with OAuth auth stuff.
49 * @param array $params
52 function oAuthUrl($url, $params=array())
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);
60 return $request->to_url();
63 * Add an event callback. Available event names include
64 * 'raw' (all data), 'friends', 'delete', 'scrubgeo', etc
66 * @param string $event
67 * @param callable $callback
69 public function hookEvent($event, $callback)
71 $this->callbacks[$event][] = $callback;
75 * Call event handler callbacks for the given event.
77 * @param string $event
78 * @param mixed $arg1 ... one or more params to pass on
80 public function fireEvent($event, $arg1)
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);
90 function handleJson(array $data)
92 $this->routeMessage($data);
95 abstract function routeMessage($data);
98 * Send the decoded JSON object out to any event listeners.
101 * @param int $forUserId
103 function handleMessage(array $data, $forUserId=null)
105 $this->fireEvent('raw', $data, $forUserId);
107 if (isset($data['id']) && isset($data['text']) && isset($data['user'])) {
108 $this->fireEvent('status', $data);
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);
120 class TwitterSiteStream extends TwitterStreamReader
124 public function __construct(TwitterOAuthClient $auth, $baseUrl='https://stream.twitter.com')
126 parent::__construct($auth, $baseUrl);
129 public function connect($method='2b/site.json')
131 return parent::connect($method);
134 function followUsers($userIds)
136 $this->userIds = $userIds;
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.
145 function routeMessage($data)
147 parent::handleMessage($data['message'], $data['for_user']);
151 class TwitterUserStream extends TwitterStreamReader
153 public function __construct(TwitterOAuthClient $auth, $baseUrl='https://userstream.twitter.com')
155 parent::__construct($auth, $baseUrl);
158 public function connect($method='2/user.json')
160 return parent::connect($method);
164 * Each message in the user stream is just ready to go.
168 function routeMessage($data)
170 parent::handleMessage($data, $this->userId);