123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- abstract class TwitterStreamReader extends JsonStreamReader
- {
- protected $callbacks = array();
- function __construct(TwitterOAuthClient $auth, $baseUrl)
- {
- $this->baseUrl = $baseUrl;
- $this->oauth = $auth;
- }
- public function connect($method, $params=array())
- {
- $url = $this->oAuthUrl($this->baseUrl . '/' . $method, $params);
- return parent::connect($url);
- }
-
- protected function oAuthUrl($url, $params=array())
- {
-
- $request = OAuthRequest::from_consumer_and_token($this->oauth->consumer,
- $this->oauth->token, 'GET', $url, $params);
- $request->sign_request($this->oauth->sha1_method,
- $this->oauth->consumer, $this->oauth->token);
- return $request->to_url();
- }
-
- public function hookEvent($event, $callback)
- {
- $this->callbacks[$event][] = $callback;
- }
-
- protected function fireEvent($event, $arg1)
- {
- if (array_key_exists($event, $this->callbacks)) {
- $args = array_slice(func_get_args(), 1);
- foreach ($this->callbacks[$event] as $callback) {
- call_user_func_array($callback, $args);
- }
- }
- }
- protected function handleJson(stdClass $data)
- {
- $this->routeMessage($data);
- }
- abstract protected function routeMessage(stdClass $data);
-
- protected function handleMessage(stdClass $data, array $context=array())
- {
- $this->fireEvent('raw', $data, $context);
- if (isset($data->text)) {
- $this->fireEvent('status', $data, $context);
- return;
- }
- if (isset($data->event)) {
- $this->fireEvent($data->event, $data, $context);
- return;
- }
- if (isset($data->friends)) {
- $this->fireEvent('friends', $data, $context);
- }
- $knownMeta = array('delete', 'scrub_geo', 'limit', 'direct_message');
- foreach ($knownMeta as $key) {
- if (isset($data->$key)) {
- $this->fireEvent($key, $data->$key, $context);
- return;
- }
- }
- }
- }
- class TwitterSiteStream extends TwitterStreamReader
- {
- protected $userIds;
- public function __construct(TwitterOAuthClient $auth, $baseUrl='https://sitestream.twitter.com')
- {
- parent::__construct($auth, $baseUrl);
- }
- public function connect($method='2b/site.json')
- {
- $params = array();
- if ($this->userIds) {
- $params['follow'] = implode(',', $this->userIds);
- }
- return parent::connect($method, $params);
- }
-
- function followUsers($userIds)
- {
- $this->userIds = $userIds;
- }
-
- function routeMessage(stdClass $data)
- {
- $context = array(
- 'source' => 'sitestream',
- 'for_user' => $data->for_user
- );
- parent::handleMessage($data->message, $context);
- }
- }
- class TwitterUserStream extends TwitterStreamReader
- {
- public function __construct(TwitterOAuthClient $auth, $baseUrl='https://userstream.twitter.com')
- {
- parent::__construct($auth, $baseUrl);
- }
- public function connect($method='2/user.json')
- {
- return parent::connect($method);
- }
-
- function routeMessage(stdClass $data)
- {
- $context = array(
- 'source' => 'userstream'
- );
- parent::handleMessage($data, $context);
- }
- }
|