Twitter-API: Viewing direct msgs in xml and json now work

darcs-hash:20080924234857-7b5ce-2f0c60901ee582bdd76ae9596253c2efcc6dc50d.gz
This commit is contained in:
Zach Copley 2008-09-24 19:48:57 -04:00
parent 654f9b8c97
commit d687f16525
2 changed files with 183 additions and 62 deletions

View File

@ -40,7 +40,46 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
function direct_messages($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
$user = $apidata['user'];
$count = $this->arg('count');
$since = $this->arg('since');
$since_id = $this->arg('since_id');
$page = $this->arg('page');
if (!$page) {
$page = 1;
}
if (!$count) {
$count = 20;
}
$message = new Message();
$message->to_profile = $user->id;
$message->orderBy('created DESC, id DESC');
$message->limit((($page-1)*20), $count);
$message->find();
switch($apidata['content-type']) {
case 'xml':
$this->show_xml_direct_messages($message);
break;
case 'rss':
//$this->show_rss_timeline($notice, $title, $id, $link, $subtitle);
break;
case 'atom':
//$this->show_atom_timeline($notice, $title, $id, $link, $subtitle);
break;
case 'json':
$this->show_json_direct_messages($message);
break;
default:
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
@ -63,4 +102,50 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
exit();
}
function show_xml_direct_messages($message) {
$this->init_document('xml');
common_element_start('direct-messages', array('type' => 'array'));
if (is_array($messages)) {
foreach ($message as $m) {
$twitter_dm = $this->twitter_dm_array($m);
$this->show_twitter_xml_dm($twitter_dm);
}
} else {
while ($message->fetch()) {
$twitter_dm = $this->twitter_dm_array($message);
$this->show_twitter_xml_dm($twitter_dm);
}
}
common_element_end('direct-messages');
$this->end_document('xml');
}
function show_json_direct_messages($message) {
$this->init_document('json');
$dmsgs = array();
if (is_array($message)) {
foreach ($message as $m) {
$twitter_dm = $this->twitter_dm_array($m);
array_push($dmsgs, $twitter_dm);
}
} else {
while ($message->fetch()) {
$twitter_dm = $this->twitter_dm_array($message);
array_push($dmsgs, $twitter_dm);
}
}
$this->show_twitter_json_dmsgs($dmsgs);
$this->end_document('json');
}
}

View File

@ -98,6 +98,26 @@ class TwitterapiAction extends Action {
return $entry;
}
function twitter_dm_array($message) {
$twitter_dm = array();
$from_profile = $message->getFrom();
$to_profile = $message->getTo();
$twitter_dm['id'] = $message->id;
$twitter_dm['sender_id'] = $message->from_profile;
$twitter_dm['text'] = $message->content;
$twitter_dm['recipient_id'] = $message->to_profile;
$twitter_dm['created_at'] = $this->date_twitter($message->created);
$twitter_dm['sender_screen_name'] = $from_profile->nickname;
$twitter_dm['recipient_screen_name'] = $to_profile->nickname;
$twitter_dm['sender'] = $this->twitter_user_array($from_profile, false);
$twitter_dm['recipient'] = $this->twitter_user_array($to_profile, false);
return $twitter_dm;
}
function show_twitter_xml_status($twitter_status) {
common_element_start('status');
foreach($twitter_status as $element => $value) {
@ -110,8 +130,8 @@ class TwitterapiAction extends Action {
common_element_end('status');
}
function show_twitter_xml_user($twitter_user) {
common_element_start('user');
function show_twitter_xml_user($twitter_user, $role='user') {
common_element_start($role);
foreach($twitter_user as $element => $value) {
if ($element == 'status') {
$this->show_twitter_xml_status($twitter_user['status']);
@ -119,7 +139,7 @@ class TwitterapiAction extends Action {
common_element($element, NULL, $value);
}
}
common_element_end('user');
common_element_end($role);
}
function show_twitter_rss_item($entry) {
@ -151,6 +171,10 @@ class TwitterapiAction extends Action {
print(json_encode($twitter_users));
}
function show_twitter_json_dmsgs($twitter_dms) {
print(json_encode($twitter_dms));
}
function show_single_xml_status($notice) {
$this->init_document('xml');
$twitter_status = $this->twitter_status_array($notice);
@ -167,6 +191,18 @@ class TwitterapiAction extends Action {
exit();
}
function show_twitter_xml_dm($twitter_dm) {
common_element_start('direct_message');
foreach($twitter_dm as $element => $value) {
if ($element == 'sender' || $element == 'recipient') {
$this->show_twitter_xml_user($value, $element);
} else {
common_element($element, NULL, $value);
}
}
common_element_end('direct_message');
}
// Anyone know what date format this is?
// Twitter's dates look like this: "Mon Jul 14 23:52:38 +0000 2008" -- Zach
function date_twitter($dt) {
@ -214,7 +250,7 @@ class TwitterapiAction extends Action {
case 'json':
header('Content-Type: application/json; charset=utf-8');
// Check for JSON-P callback
// Check for JSONP callback
$callback = $this->arg('callback');
if ($callback) {
print $callback . '(';
@ -243,7 +279,7 @@ class TwitterapiAction extends Action {
break;
case 'json':
// Check for JSON-P callback
// Check for JSONP callback
$callback = $this->arg('callback');
if ($callback) {
print ')';