summaryrefslogtreecommitdiff
path: root/plugins/TwitterBridge
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-10-05 14:26:11 -0700
committerBrion Vibber <brion@pobox.com>2010-10-05 14:26:11 -0700
commit408483f7718a2c81c37eb5f387130381a4a188c8 (patch)
tree23716a5be014afbc49b4ce1fadbace7900740e2f /plugins/TwitterBridge
parent0eaa26476cac557484e295666b574340236452ff (diff)
Fix up Twitter JSON formatting to be consistent between the polling and streaming API interfaces; basic stream tester can now import your notices (ooooh)
Diffstat (limited to 'plugins/TwitterBridge')
-rw-r--r--plugins/TwitterBridge/jsonstreamreader.php4
-rw-r--r--plugins/TwitterBridge/scripts/streamtest.php59
-rw-r--r--plugins/TwitterBridge/twitterstreamreader.php70
3 files changed, 78 insertions, 55 deletions
diff --git a/plugins/TwitterBridge/jsonstreamreader.php b/plugins/TwitterBridge/jsonstreamreader.php
index 427037129..f6572c9ee 100644
--- a/plugins/TwitterBridge/jsonstreamreader.php
+++ b/plugins/TwitterBridge/jsonstreamreader.php
@@ -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);
}
diff --git a/plugins/TwitterBridge/scripts/streamtest.php b/plugins/TwitterBridge/scripts/streamtest.php
index eddec39a7..1cec451c8 100644
--- a/plugins/TwitterBridge/scripts/streamtest.php
+++ b/plugins/TwitterBridge/scripts/streamtest.php
@@ -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
diff --git a/plugins/TwitterBridge/twitterstreamreader.php b/plugins/TwitterBridge/twitterstreamreader.php
index 793e239d0..3a3c8f5e6 100644
--- a/plugins/TwitterBridge/twitterstreamreader.php
+++ b/plugins/TwitterBridge/twitterstreamreader.php
@@ -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'