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:
parent
0eaa26476c
commit
408483f771
@ -253,7 +253,7 @@ abstract class JsonStreamReader
|
||||
// Server sends empty lines as keepalive.
|
||||
return;
|
||||
}
|
||||
$data = json_decode($line, true);
|
||||
$data = json_decode($line);
|
||||
if ($data) {
|
||||
$this->handleJson($data);
|
||||
} else {
|
||||
@ -261,5 +261,5 @@ abstract class JsonStreamReader
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected function handleJson(array $data);
|
||||
abstract protected function handleJson(stdClass $data);
|
||||
}
|
||||
|
@ -28,11 +28,14 @@
|
||||
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
|
||||
|
||||
$shortoptions = 'n:';
|
||||
$longoptions = array('nick=');
|
||||
$longoptions = array('nick=','import');
|
||||
|
||||
$helptext = <<<ENDOFHELP
|
||||
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
|
||||
data as it comes.
|
||||
|
||||
@ -79,54 +82,70 @@ function homeStreamForUser(User $user)
|
||||
}
|
||||
|
||||
$user = User::staticGet('nickname', $nickname);
|
||||
global $myuser;
|
||||
$myuser = $user;
|
||||
|
||||
$stream = homeStreamForUser($user);
|
||||
$stream->hookEvent('raw', function($data) {
|
||||
common_log(LOG_INFO, json_encode($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) {
|
||||
printf("%s favorited %s's notice: %s\n",
|
||||
$data['source']['screen_name'],
|
||||
$data['target']['screen_name'],
|
||||
$data['target_object']['text']);
|
||||
$data->source->screen_name,
|
||||
$data->target->screen_name,
|
||||
$data->target_object->text);
|
||||
});
|
||||
$stream->hookEvent('unfavorite', function($data) {
|
||||
printf("%s unfavorited %s's notice: %s\n",
|
||||
$data['source']['screen_name'],
|
||||
$data['target']['screen_name'],
|
||||
$data['target_object']['text']);
|
||||
$data->source->screen_name,
|
||||
$data->target->screen_name,
|
||||
$data->target_object->text);
|
||||
});
|
||||
$stream->hookEvent('follow', function($data) {
|
||||
printf("%s friended %s\n",
|
||||
$data['source']['screen_name'],
|
||||
$data['target']['screen_name']);
|
||||
$data->source->screen_name,
|
||||
$data->target->screen_name);
|
||||
});
|
||||
$stream->hookEvent('unfollow', function($data) {
|
||||
printf("%s unfriended %s\n",
|
||||
$data['source']['screen_name'],
|
||||
$data['target']['screen_name']);
|
||||
$data->source->screen_name,
|
||||
$data->target->screen_name);
|
||||
});
|
||||
$stream->hookEvent('delete', function($data) {
|
||||
printf("Deleted status notification: %s\n",
|
||||
$data['status']['id']);
|
||||
$data->status->id);
|
||||
});
|
||||
$stream->hookEvent('scrub_geo', function($data) {
|
||||
printf("Req to scrub geo data for user id %s up to status ID %s\n",
|
||||
$data['user_id'],
|
||||
$data['up_to_status_id']);
|
||||
$data->user_id,
|
||||
$data->up_to_status_id);
|
||||
});
|
||||
$stream->hookEvent('status', function($data) {
|
||||
printf("Received status update from %s: %s\n",
|
||||
$data['user']['screen_name'],
|
||||
$data['text']);
|
||||
$data->user->screen_name,
|
||||
$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) {
|
||||
printf("Direct message from %s to %s: %s\n",
|
||||
$data['sender']['screen_name'],
|
||||
$data['recipient']['screen_name'],
|
||||
$data['text']);
|
||||
$data->sender->screen_name,
|
||||
$data->recipient->screen_name,
|
||||
$data->text);
|
||||
});
|
||||
|
||||
class TwitterManager extends IoManager
|
||||
|
@ -71,7 +71,7 @@ abstract class TwitterStreamReader extends JsonStreamReader
|
||||
* Add an event callback to receive notifications when things come in
|
||||
* 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
|
||||
* user to whom the message should be routed.
|
||||
*
|
||||
@ -80,48 +80,49 @@ abstract class TwitterStreamReader extends JsonStreamReader
|
||||
* Messaging:
|
||||
*
|
||||
* 'status': $data contains a status update in standard Twitter JSON format.
|
||||
* $data['user']: sending user in standard Twitter JSON format.
|
||||
* $data['text']... etc
|
||||
* $data->user: sending user in standard Twitter JSON format.
|
||||
* $data->text... etc
|
||||
*
|
||||
* 'direct_message': $data contains a direct message in standard Twitter JSON format.
|
||||
* $data['sender']: sending user in standard Twitter JSON format.
|
||||
* $data['recipient']: receiving user in standard Twitter JSON format.
|
||||
* $data['text']... etc
|
||||
* $data->sender: sending user in standard Twitter JSON format.
|
||||
* $data->recipient: receiving user in standard Twitter JSON format.
|
||||
* $data->text... etc
|
||||
*
|
||||
*
|
||||
* Out of band events:
|
||||
*
|
||||
* 'follow': User has either started following someone, or is being followed.
|
||||
* $data['source']: following user in standard Twitter JSON format.
|
||||
* $data['target']: followed user in standard Twitter JSON format.
|
||||
* $data->source: following user in standard Twitter JSON format.
|
||||
* $data->target: followed user in standard Twitter JSON format.
|
||||
*
|
||||
* 'favorite': Someone has favorited a status update.
|
||||
* $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_object']: the favorited status update 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_object: the favorited status update in standard Twitter JSON format.
|
||||
*
|
||||
* 'unfavorite': Someone has unfavorited a status update.
|
||||
* $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_object']: the unfavorited status update 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_object: the unfavorited status update in standard Twitter JSON format.
|
||||
*
|
||||
*
|
||||
* 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
|
||||
* should follow suit.
|
||||
* $data['id']: ID of status being deleted
|
||||
* $data['user_id']: ID of its owning user
|
||||
* $data->id: ID of status being deleted
|
||||
* $data->user_id: ID of its owning user
|
||||
*
|
||||
* 'scrub_geo': Advisory that a user is clearing geo data from their status
|
||||
* stream; nice clients should follow suit.
|
||||
* $data['user_id']: ID of user
|
||||
* $data['up_to_status_id']: any notice older than this should be scrubbed.
|
||||
* $data->user_id: ID of user
|
||||
* $data->up_to_status_id: any notice older than this should be scrubbed.
|
||||
*
|
||||
* 'limit': Advisory that tracking has hit a resource limit.
|
||||
* $data['track']
|
||||
* $data->track
|
||||
*
|
||||
* '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);
|
||||
}
|
||||
|
||||
abstract protected function routeMessage($data);
|
||||
abstract protected function routeMessage(stdClass $data);
|
||||
|
||||
/**
|
||||
* Send the decoded JSON object out to any event listeners.
|
||||
@ -162,23 +163,26 @@ abstract class TwitterStreamReader extends JsonStreamReader
|
||||
* @param array $data
|
||||
* @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);
|
||||
|
||||
if (isset($data['text'])) {
|
||||
if (isset($data->text)) {
|
||||
$this->fireEvent('status', $data, $context);
|
||||
return;
|
||||
}
|
||||
if (isset($data['event'])) {
|
||||
$this->fireEvent($data['event'], $data, $context);
|
||||
if (isset($data->event)) {
|
||||
$this->fireEvent($data->event, $data, $context);
|
||||
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) {
|
||||
if (isset($data[$key])) {
|
||||
$this->fireEvent($key, $data[$key], $context);
|
||||
if (isset($data->$key)) {
|
||||
$this->fireEvent($key, $data->$key, $context);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -237,13 +241,13 @@ class TwitterSiteStream extends TwitterStreamReader
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
function routeMessage($data)
|
||||
function routeMessage(stdClass $data)
|
||||
{
|
||||
$context = array(
|
||||
'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
|
||||
*/
|
||||
function routeMessage($data)
|
||||
function routeMessage(stdClass $data)
|
||||
{
|
||||
$context = array(
|
||||
'source' => 'userstream'
|
||||
|
Loading…
Reference in New Issue
Block a user