Fetch more user data in Yammer imports, including the primary email address (preconfirmed, so we can do stuff like tell people to reset their passwords and log in!) and some bio info.

This commit is contained in:
Brion Vibber 2010-09-21 18:15:32 -07:00
parent 37c67c3f9f
commit 1685c8a4fa
4 changed files with 112 additions and 74 deletions

View File

@ -185,8 +185,36 @@ class SN_YammerClient
return $this->apiBase . '/oauth/authorize?oauth_token=' . urlencode($token);
}
/**
* High-level API hit: fetch all messages in the network (up to 20 at a time).
* Return data is the full JSON array returned, including meta and references
* sections.
*
* The matching messages themselves will be in the 'messages' item within.
*
* @param array $options optional set of additional params for the request.
* @return array
*
* @throws Exception on low-level or HTTP error
*/
public function messages($params=array())
{
return $this->api('messages', $params);
}
/**
* High-level API hit: fetch all users in the network (up to 50 at a time).
* Return data is the full JSON array returned, listing user items.
*
* The matching messages themselves will be in the 'users' item within.
*
* @param array $options optional set of additional params for the request.
* @return array of JSON-sourced user data arrays
*
* @throws Exception on low-level or HTTP error
*/
public function users($params=array())
{
return $this->api('users', $params);
}
}

View File

@ -13,50 +13,22 @@ require 'yam-config.php';
$yam = new SN_YammerClient($consumerKey, $consumerSecret, $token, $tokenSecret);
$imp = new YammerImporter($yam);
$data = $yam->messages();
$data = $yam->users();
var_dump($data);
/*
["messages"]=>
["meta"]=> // followed_user_ids, current_user_id, etc
["threaded_extended"]=> // empty!
["references"]=> // lists the users, threads, replied messages, tags
*/
// 1) we're getting messages in descending order, but we'll want to process ascending
// 2) we'll need to pull out all those referenced items too?
// 3) do we need to page over or anything?
// 20 qualifying messages per hit...
// use older_than to grab more
// (better if we can go in reverse though!)
// meta: The older-available element indicates whether messages older than those shown are available to be fetched. See the older_than parameter mentioned above.
foreach ($data['references'] as $item) {
if ($item['type'] == 'user') {
$user = $imp->prepUser($item);
var_dump($user);
} else if ($item['type'] == 'group') {
$group = $imp->prepGroup($item);
var_dump($group);
} else if ($item['type'] == 'tag') {
// could need these if we work from the parsed message text
// otherwise, the #blarf in orig text is fine.
} else if ($item['type'] == 'thread') {
// Shouldn't need thread info; we'll reconstruct conversations
// from the reply-to chains.
} else if ($item['type'] == 'message') {
// If we're processing everything, then we don't need the refs here.
} else {
echo "(skipping unknown ref: " . $item['type'] . ")\n";
}
// @fixme follow paging
foreach ($data as $item) {
$user = $imp->prepUser($item);
var_dump($user);
}
// Process in reverse chron order...
/*
$data = $yam->messages();
var_dump($data);
// @fixme follow paging
$messages = $data['messages'];
array_reverse($messages);
$messages = array_reverse($messages);
foreach ($messages as $message) {
$notice = $imp->prepNotice($message);
var_dump($notice);
}
*/

View File

@ -13,44 +13,26 @@ require 'yam-config.php';
$yam = new SN_YammerClient($consumerKey, $consumerSecret, $token, $tokenSecret);
$imp = new YammerImporter($yam);
// First, import all the users!
// @fixme follow paging -- we only get 50 at a time
$data = $yam->users();
foreach ($data as $item) {
$user = $imp->importUser($item);
echo "Imported Yammer user " . $item['id'] . " as $user->nickname ($user->id)\n";
}
$data = $yam->messages();
/*
["messages"]=>
["meta"]=> // followed_user_ids, current_user_id, etc
["threaded_extended"]=> // empty!
["references"]=> // lists the users, threads, replied messages, tags
*/
// 1) we're getting messages in descending order, but we'll want to process ascending
// 2) we'll need to pull out all those referenced items too?
// 3) do we need to page over or anything?
// 20 qualifying messages per hit...
// use older_than to grab more
// (better if we can go in reverse though!)
// meta: The older-available element indicates whether messages older than those shown are available to be fetched. See the older_than parameter mentioned above.
// @fixme pull the full group list; this'll be a partial list with less data
// and only for groups referenced in the message set.
foreach ($data['references'] as $item) {
if ($item['type'] == 'user') {
$user = $imp->importUser($item);
echo "Imported Yammer user " . $item['id'] . " as $user->nickname ($user->id)\n";
} else if ($item['type'] == 'group') {
if ($item['type'] == 'group') {
$group = $imp->importGroup($item);
echo "Imported Yammer group " . $item['id'] . " as $group->nickname ($group->id)\n";
} else if ($item['type'] == 'tag') {
// could need these if we work from the parsed message text
// otherwise, the #blarf in orig text is fine.
} else if ($item['type'] == 'thread') {
// Shouldn't need thread info; we'll reconstruct conversations
// from the reply-to chains.
} else if ($item['type'] == 'message') {
// If we're processing everything, then we don't need the refs here.
} else {
echo "(skipping unknown ref: " . $item['type'] . ")\n";
}
}
// Process in reverse chron order...
// @fixme follow paging
// @fixme follow paging -- we only get 20 at a time, and start at the most recent!
$messages = $data['messages'];
$messages = array_reverse($messages);
foreach ($messages as $item) {

View File

@ -158,16 +158,60 @@ class YammerImporter
$options['nickname'] = $item['name'];
$options['fullname'] = trim($item['full_name']);
// We don't appear to have full bio avail here!
$options['bio'] = $item['job_title'];
// What about other data like emails?
// Avatar... this will be the "_small" variant.
// Remove that (pre-extension) suffix to get the orig-size image.
$avatar = $item['mugshot_url'];
// Warning: we don't have following state for other users?
// The following info is only available in full data, not in the reference version.
// There can be extensive contact info, but for now we'll only pull the primary email.
if (isset($item['contact'])) {
foreach ($item['contact']['email_addresses'] as $addr) {
if ($addr['type'] == 'primary') {
$options['email'] = $addr['address'];
$options['email_confirmed'] = true;
break;
}
}
}
// There can be multiple external URLs; for now pull the first one as home page.
if (isset($item['external_urls'])) {
foreach ($item['external_urls'] as $url) {
if (common_valid_http_url($url)) {
$options['homepage'] = $url;
break;
}
}
}
// Combine a few bits into the bio...
$bio = array();
if (!empty($item['job_title'])) {
$bio[] = $item['job_title'];
}
if (!empty($item['summary'])) {
$bio[] = $item['summary'];
}
if (!empty($item['expertise'])) {
$bio[] = _m('Expertise:') . ' ' . $item['expertise'];
}
$options['bio'] = implode("\n\n", $bio);
// Pull raw location string, may be lookupable
if (!empty($item['location'])) {
$options['location'] = $item['location'];
}
// Timezone is in format like 'Pacific Time (US & Canada)'
// We need to convert that to a zone id. :P
// @fixme timezone not yet supported at registration time :)
if (!empty($item['timezone'])) {
$tz = $this->timezone($item['timezone']);
if ($tz) {
$options['timezone'] = $tz;
}
}
return array('orig_id' => $origId,
'orig_url' => $origUrl,
@ -325,6 +369,18 @@ class YammerImporter
return common_sql_date(strtotime($ts));
}
private function timezone($tz)
{
// Blaaaaaarf!
$known = array('Pacific Time (US & Canada)' => 'America/Los_Angeles',
'Eastern Time (US & Canada)' => 'America/New_York');
if (array_key_exists($known, $tz)) {
return $known[$tz];
} else {
return false;
}
}
/**
* Download and update given avatar image
*