Fix up Twitter JSON formatting to be consistent between the polling and streaming API interfaces; basic stream tester can now import your notices (ooooh)

This commit is contained in:
Brion Vibber 2010-10-05 14:26:11 -07:00
parent 0eaa26476c
commit 408483f771
3 changed files with 78 additions and 55 deletions

View File

@ -253,7 +253,7 @@ abstract class JsonStreamReader
// Server sends empty lines as keepalive. // Server sends empty lines as keepalive.
return; return;
} }
$data = json_decode($line, true); $data = json_decode($line);
if ($data) { if ($data) {
$this->handleJson($data); $this->handleJson($data);
} else { } else {
@ -261,5 +261,5 @@ abstract class JsonStreamReader
} }
} }
abstract protected function handleJson(array $data); abstract protected function handleJson(stdClass $data);
} }

View File

@ -28,11 +28,14 @@
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
$shortoptions = 'n:'; $shortoptions = 'n:';
$longoptions = array('nick='); $longoptions = array('nick=','import');
$helptext = <<<ENDOFHELP $helptext = <<<ENDOFHELP
USAGE: streamtest.php -n <username> USAGE: streamtest.php -n <username>
-n --nick <username> Local user whose Twitter timeline to watch
--import Experimental: run incoming messages through import
Attempts a User Stream connection to Twitter as the given user, dumping Attempts a User Stream connection to Twitter as the given user, dumping
data as it comes. data as it comes.
@ -79,54 +82,70 @@ function homeStreamForUser(User $user)
} }
$user = User::staticGet('nickname', $nickname); $user = User::staticGet('nickname', $nickname);
global $myuser;
$myuser = $user;
$stream = homeStreamForUser($user); $stream = homeStreamForUser($user);
$stream->hookEvent('raw', function($data) { $stream->hookEvent('raw', function($data) {
common_log(LOG_INFO, json_encode($data)); common_log(LOG_INFO, json_encode($data));
}); });
$stream->hookEvent('friends', function($data) { $stream->hookEvent('friends', function($data) {
printf("Friend list: %s\n", implode(', ', $data)); printf("Friend list: %s\n", implode(', ', $data->friends));
}); });
$stream->hookEvent('favorite', function($data) { $stream->hookEvent('favorite', function($data) {
printf("%s favorited %s's notice: %s\n", printf("%s favorited %s's notice: %s\n",
$data['source']['screen_name'], $data->source->screen_name,
$data['target']['screen_name'], $data->target->screen_name,
$data['target_object']['text']); $data->target_object->text);
}); });
$stream->hookEvent('unfavorite', function($data) { $stream->hookEvent('unfavorite', function($data) {
printf("%s unfavorited %s's notice: %s\n", printf("%s unfavorited %s's notice: %s\n",
$data['source']['screen_name'], $data->source->screen_name,
$data['target']['screen_name'], $data->target->screen_name,
$data['target_object']['text']); $data->target_object->text);
}); });
$stream->hookEvent('follow', function($data) { $stream->hookEvent('follow', function($data) {
printf("%s friended %s\n", printf("%s friended %s\n",
$data['source']['screen_name'], $data->source->screen_name,
$data['target']['screen_name']); $data->target->screen_name);
}); });
$stream->hookEvent('unfollow', function($data) { $stream->hookEvent('unfollow', function($data) {
printf("%s unfriended %s\n", printf("%s unfriended %s\n",
$data['source']['screen_name'], $data->source->screen_name,
$data['target']['screen_name']); $data->target->screen_name);
}); });
$stream->hookEvent('delete', function($data) { $stream->hookEvent('delete', function($data) {
printf("Deleted status notification: %s\n", printf("Deleted status notification: %s\n",
$data['status']['id']); $data->status->id);
}); });
$stream->hookEvent('scrub_geo', function($data) { $stream->hookEvent('scrub_geo', function($data) {
printf("Req to scrub geo data for user id %s up to status ID %s\n", printf("Req to scrub geo data for user id %s up to status ID %s\n",
$data['user_id'], $data->user_id,
$data['up_to_status_id']); $data->up_to_status_id);
}); });
$stream->hookEvent('status', function($data) { $stream->hookEvent('status', function($data) {
printf("Received status update from %s: %s\n", printf("Received status update from %s: %s\n",
$data['user']['screen_name'], $data->user->screen_name,
$data['text']); $data->text);
if (have_option('import')) {
$importer = new TwitterImport();
printf("\timporting...");
$notice = $importer->importStatus($data);
if ($notice) {
global $myuser;
Inbox::insertNotice($myuser->id, $notice->id);
printf(" %s\n", $notice->id);
} else {
printf(" FAIL\n");
}
}
}); });
$stream->hookEvent('direct_message', function($data) { $stream->hookEvent('direct_message', function($data) {
printf("Direct message from %s to %s: %s\n", printf("Direct message from %s to %s: %s\n",
$data['sender']['screen_name'], $data->sender->screen_name,
$data['recipient']['screen_name'], $data->recipient->screen_name,
$data['text']); $data->text);
}); });
class TwitterManager extends IoManager class TwitterManager extends IoManager

View File

@ -71,7 +71,7 @@ abstract class TwitterStreamReader extends JsonStreamReader
* Add an event callback to receive notifications when things come in * Add an event callback to receive notifications when things come in
* over the wire. * over the wire.
* *
* Callbacks should be in the form: function(array $data, array $context) * Callbacks should be in the form: function(object $data, array $context)
* where $context may list additional data on some streams, such as the * where $context may list additional data on some streams, such as the
* user to whom the message should be routed. * user to whom the message should be routed.
* *
@ -80,48 +80,49 @@ abstract class TwitterStreamReader extends JsonStreamReader
* Messaging: * Messaging:
* *
* 'status': $data contains a status update in standard Twitter JSON format. * 'status': $data contains a status update in standard Twitter JSON format.
* $data['user']: sending user in standard Twitter JSON format. * $data->user: sending user in standard Twitter JSON format.
* $data['text']... etc * $data->text... etc
* *
* 'direct_message': $data contains a direct message in standard Twitter JSON format. * 'direct_message': $data contains a direct message in standard Twitter JSON format.
* $data['sender']: sending user in standard Twitter JSON format. * $data->sender: sending user in standard Twitter JSON format.
* $data['recipient']: receiving user in standard Twitter JSON format. * $data->recipient: receiving user in standard Twitter JSON format.
* $data['text']... etc * $data->text... etc
* *
* *
* Out of band events: * Out of band events:
* *
* 'follow': User has either started following someone, or is being followed. * 'follow': User has either started following someone, or is being followed.
* $data['source']: following user in standard Twitter JSON format. * $data->source: following user in standard Twitter JSON format.
* $data['target']: followed user in standard Twitter JSON format. * $data->target: followed user in standard Twitter JSON format.
* *
* 'favorite': Someone has favorited a status update. * 'favorite': Someone has favorited a status update.
* $data['source']: user doing the favoriting, in standard Twitter JSON format. * $data->source: user doing the favoriting, in standard Twitter JSON format.
* $data['target']: user whose status was favorited, in standard Twitter JSON format. * $data->target: user whose status was favorited, in standard Twitter JSON format.
* $data['target_object']: the favorited status update in standard Twitter JSON format. * $data->target_object: the favorited status update in standard Twitter JSON format.
* *
* 'unfavorite': Someone has unfavorited a status update. * 'unfavorite': Someone has unfavorited a status update.
* $data['source']: user doing the unfavoriting, in standard Twitter JSON format. * $data->source: user doing the unfavoriting, in standard Twitter JSON format.
* $data['target']: user whose status was unfavorited, in standard Twitter JSON format. * $data->target: user whose status was unfavorited, in standard Twitter JSON format.
* $data['target_object']: the unfavorited status update in standard Twitter JSON format. * $data->target_object: the unfavorited status update in standard Twitter JSON format.
* *
* *
* Meta information: * Meta information:
* *
* 'friends': $data is a list of user IDs of the current user's friends. * 'friends':
* $data->friends: array of user IDs of the current user's friends.
* *
* 'delete': Advisory that a Twitter status has been deleted; nice clients * 'delete': Advisory that a Twitter status has been deleted; nice clients
* should follow suit. * should follow suit.
* $data['id']: ID of status being deleted * $data->id: ID of status being deleted
* $data['user_id']: ID of its owning user * $data->user_id: ID of its owning user
* *
* 'scrub_geo': Advisory that a user is clearing geo data from their status * 'scrub_geo': Advisory that a user is clearing geo data from their status
* stream; nice clients should follow suit. * stream; nice clients should follow suit.
* $data['user_id']: ID of user * $data->user_id: ID of user
* $data['up_to_status_id']: any notice older than this should be scrubbed. * $data->up_to_status_id: any notice older than this should be scrubbed.
* *
* 'limit': Advisory that tracking has hit a resource limit. * 'limit': Advisory that tracking has hit a resource limit.
* $data['track'] * $data->track
* *
* 'raw': receives the full JSON data for all message types. * 'raw': receives the full JSON data for all message types.
* *
@ -149,12 +150,12 @@ abstract class TwitterStreamReader extends JsonStreamReader
} }
} }
protected function handleJson(array $data) protected function handleJson(stdClass $data)
{ {
$this->routeMessage($data); $this->routeMessage($data);
} }
abstract protected function routeMessage($data); abstract protected function routeMessage(stdClass $data);
/** /**
* Send the decoded JSON object out to any event listeners. * Send the decoded JSON object out to any event listeners.
@ -162,23 +163,26 @@ abstract class TwitterStreamReader extends JsonStreamReader
* @param array $data * @param array $data
* @param array $context optional additional context data to pass on * @param array $context optional additional context data to pass on
*/ */
protected function handleMessage(array $data, array $context=array()) protected function handleMessage(stdClass $data, array $context=array())
{ {
$this->fireEvent('raw', $data, $context); $this->fireEvent('raw', $data, $context);
if (isset($data['text'])) { if (isset($data->text)) {
$this->fireEvent('status', $data, $context); $this->fireEvent('status', $data, $context);
return; return;
} }
if (isset($data['event'])) { if (isset($data->event)) {
$this->fireEvent($data['event'], $data, $context); $this->fireEvent($data->event, $data, $context);
return; return;
} }
if (isset($data->friends)) {
$this->fireEvent('friends', $data, $context);
}
$knownMeta = array('friends', 'delete', 'scrubgeo', 'limit', 'direct_message'); $knownMeta = array('delete', 'scrub_geo', 'limit', 'direct_message');
foreach ($knownMeta as $key) { foreach ($knownMeta as $key) {
if (isset($data[$key])) { if (isset($data->$key)) {
$this->fireEvent($key, $data[$key], $context); $this->fireEvent($key, $data->$key, $context);
return; return;
} }
} }
@ -237,13 +241,13 @@ class TwitterSiteStream extends TwitterStreamReader
* *
* @param array $data * @param array $data
*/ */
function routeMessage($data) function routeMessage(stdClass $data)
{ {
$context = array( $context = array(
'source' => 'sitestream', 'source' => 'sitestream',
'for_user' => $data['for_user'] 'for_user' => $data->for_user
); );
parent::handleMessage($data['message'], $context); parent::handleMessage($data->message, $context);
} }
} }
@ -271,7 +275,7 @@ class TwitterUserStream extends TwitterStreamReader
* *
* @param array $data * @param array $data
*/ */
function routeMessage($data) function routeMessage(stdClass $data)
{ {
$context = array( $context = array(
'source' => 'userstream' 'source' => 'userstream'