Twitter-compatible API - code cleanup

darcs-hash:20080716220223-ca946-e3eed117cded61eb9c2d2805fd07758f883fb85b.gz
This commit is contained in:
zach 2008-07-16 18:02:23 -04:00
parent fba25b0d98
commit ac2d811a46
4 changed files with 156 additions and 162 deletions

View File

@ -38,12 +38,12 @@ class ApiAction extends Action {
$cmdext = explode('.', $argument); $cmdext = explode('.', $argument);
$this->api_arg = $cmdext[0]; $this->api_arg = $cmdext[0];
$this->api_method = $method; $this->api_method = $method;
$this->content_type = $cmdext[1]; $this->content_type = strtolower($cmdext[1]);
} else { } else {
#content type will be an extension on the method #content type will be an extension on the method
$cmdext = explode('.', $method); $cmdext = explode('.', $method);
$this->api_method = $cmdext[0]; $this->api_method = $cmdext[0];
$this->content_type = $cmdext[1]; $this->content_type = strtolower($cmdext[1]);
} }
# common_debug("apiaction = $this->api_action, method = $this->api_method, argument = $this->api_arg, ctype = $this->content_type"); # common_debug("apiaction = $this->api_action, method = $this->api_method, argument = $this->api_arg, ctype = $this->content_type");

View File

@ -28,148 +28,147 @@ require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapistatusesAction extends TwitterapiAction { class TwitapistatusesAction extends TwitterapiAction {
/* /*
* Returns the 20 most recent statuses from non-protected users who have set a custom * Returns the MAX_PUBSTATUSES most recent statuses from non-protected users who
* user icon. Does not require authentication. * have set a custom avatar. Does not require authentication.
* *
* URL: http://identi.ca/api/statuses/public_timeline.format * URL: http://server/api/statuses/public_timeline.format
* *
* Formats: xml, json, rss, atom * Formats: xml, json, rss, atom
*/ */
function public_timeline($args, $apidata) { function public_timeline($args, $apidata) {
parent::handle($args); parent::handle($args);
// Number of public statuses to return by default -- Twitter sends 20
$MAX_PUBSTATUSES = 20;
$notice = DB_DataObject::factory('notice'); $notice = DB_DataObject::factory('notice');
// FIXME: To really live up to the spec we need to build a list
// of notices by users who have custom avatars, so fix this SQL -- Zach
# FIXME: bad performance # FIXME: bad performance
$notice->whereAdd('EXISTS (SELECT user.id from user where user.id = notice.profile_id)'); $notice->whereAdd('EXISTS (SELECT user.id from user where user.id = notice.profile_id)');
$notice->orderBy('created DESC, notice.id DESC'); $notice->orderBy('created DESC, notice.id DESC');
$notice->limit(20); $notice->limit($MAX_PUBSTATUSES);
$cnt = $notice->find(); $cnt = $notice->find();
if ($apidata['content-type'] == 'xml') {
header('Content-Type: application/xml; charset=utf-8');
common_start_xml();
// XXX: To really live up to the spec we need to build a list
// of notices by users who have custom avatars -- Zach
if ($cnt > 0) {
common_element_start('statuses', array('type' => 'array'));
for ($i = 0; $i < 20; $i++) {
if ($notice->fetch()) {
$twitter_status = $this->twitter_status_array($notice);
$this->render_twitter_xml_status($twitter_status);
} else {
// shouldn't happen!
break;
}
}
common_element_end('statuses');
}
common_end_xml(); if ($cnt > 0) {
} elseif ($apidata['content-type'] == 'rss') {
header("Content-Type: application/rss+xml; charset=utf-8"); switch($apidata['content-type']) {
case 'xml':
$this->init_twitter_rss(); $this->show_xml_public_timeline($notice);
break;
$sitename = common_config('site', 'name'); case 'rss':
$siteserver = common_config('site', 'server'); $this->show_rss_public_timeline($notice);
break;
common_element_start('channel'); case 'atom':
common_element('title', NULL, "$sitename public timeline"); $this->show_atom_public_timeline($notice);
common_element('link', NULL, "http://$siteserver"); break;
common_element('description', NULL, "$sitename updates from everyone!"); case 'json':
common_element('language', NULL, 'en-us'); $this->show_json_public_timeline($notice);
common_element('ttl', NULL, '40'); break;
default:
if ($cnt > 0) { common_user_error("API method not found!", $code = 404);
for ($i = 0; $i < 20; $i++) { break;
if ($notice->fetch()) {
$entry = $this->twitter_rss_entry_array($notice);
$this->show_twitter_rss_item($entry);
} else {
// shouldn't happen!
break;
}
}
} }
common_element_end('channel');
$this->end_twitter_rss();
} elseif ($apidata['content-type'] == 'atom') {
header('Content-Type: application/atom+xml; charset=utf-8');
$this->init_twitter_atom();
$sitename = common_config('site', 'name'); } else {
$siteserver = common_config('site', 'server'); common_server_error('Couldn\'t find any statuses.', $code = 503);
common_element('title', NULL, "$sitename public timeline");
common_element('id', NULL, "tag:$siteserver:Statuses");
common_element('link', array('href' => "http://$siteserver", 'rel' => 'alternate', 'type' => 'text/html'), NULL);
common_element('subtitle', NULL, "$sitename updates from everyone!");
if ($cnt > 0) {
for ($i = 0; $i < 20; $i++) {
if ($notice->fetch()) {
$entry = $this->twitter_rss_entry_array($notice);
$this->show_twitter_atom_entry($entry);
} else {
// shouldn't happen!
break;
}
}
}
$this->end_twitter_atom();
} elseif ($apidata['content-type'] == 'json') {
header('Content-Type: application/json; charset=utf-8');
$statuses = array();
if ($cnt > 0) {
for ($i = 0; $i < 20; $i++) {
if ($notice->fetch()) {
$twitter_status = $this->twitter_status_array($notice);
array_push($statuses, $twitter_status);
} else {
// shouldn't happen!
break;
}
}
}
$this->render_twitter_json_statuses($statuses);
} }
exit(); exit();
} }
function show_xml_public_timeline($notice) {
header('Content-Type: application/xml; charset=utf-8');
common_start_xml();
common_element_start('statuses', array('type' => 'array'));
while ($notice->fetch()) {
$twitter_status = $this->twitter_status_array($notice);
$this->show_twitter_xml_status($twitter_status);
}
common_element_end('statuses');
common_end_xml();
}
function show_rss_public_timeline($notice) {
header("Content-Type: application/rss+xml; charset=utf-8");
$this->init_twitter_rss();
$sitename = common_config('site', 'name');
$siteserver = common_config('site', 'server');
common_element_start('channel');
common_element('title', NULL, "$sitename public timeline");
common_element('link', NULL, "http://$siteserver");
common_element('description', NULL, "$sitename updates from everyone!");
common_element('language', NULL, 'en-us');
common_element('ttl', NULL, '40');
while ($notice->fetch()) {
$entry = $this->twitter_rss_entry_array($notice);
$this->show_twitter_rss_item($entry);
}
common_element_end('channel');
$this->end_twitter_rss();
}
function show_atom_public_timeline($notice) {
header('Content-Type: application/atom+xml; charset=utf-8');
$this->init_twitter_atom();
$sitename = common_config('site', 'name');
$siteserver = common_config('site', 'server');
common_element('title', NULL, "$sitename public timeline");
common_element('id', NULL, "tag:$siteserver:Statuses");
common_element('link', array('href' => "http://$siteserver", 'rel' => 'alternate', 'type' => 'text/html'), NULL);
common_element('subtitle', NULL, "$sitename updates from everyone!");
while ($notice->fetch()) {
$entry = $this->twitter_rss_entry_array($notice);
$this->show_twitter_atom_entry($entry);
}
$this->end_twitter_atom();
}
function show_json_public_timeline($notice) {
header('Content-Type: application/json; charset=utf-8');
$statuses = array();
while ($notice->fetch()) {
$twitter_status = $this->twitter_status_array($notice);
array_push($statuses, $twitter_status);
}
$this->show_twitter_json_statuses($statuses);
}
/* /*
Returns the 20 most recent statuses posted by the authenticating user and that user's friends. Returns the 20 most recent statuses posted by the authenticating user and that user's friends.
This is the equivalent of /home on the Web. This is the equivalent of /home on the Web.
URL: http://identi.ca/api/statuses/friends_timeline.format URL: http://server/api/statuses/friends_timeline.format
Parameters: Parameters:
* since. Optional. Narrows the returned results to just those statuses created after the specified * since. Optional. Narrows the returned results to just those statuses created after the specified
HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in
your HTTP request. your HTTP request.
Ex: http://identi.ca/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT Ex: http://server/api/statuses/friends_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
* since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
the specified ID. Ex: http://identi.ca/api/statuses/friends_timeline.xml?since_id=12345 the specified ID. Ex: http://server/api/statuses/friends_timeline.xml?since_id=12345
* count. Optional. Specifies the number of statuses to retrieve. May not be greater than 200. * count. Optional. Specifies the number of statuses to retrieve. May not be greater than 200.
Ex: http://identi.ca/api/statuses/friends_timeline.xml?count=5 Ex: http://server/api/statuses/friends_timeline.xml?count=5
* page. Optional. Ex: http://identi.ca/api/statuses/friends_timeline.rss?page=3 * page. Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
Formats: xml, json, rss, atom Formats: xml, json, rss, atom
*/ */
@ -193,25 +192,25 @@ class TwitapistatusesAction extends TwitterapiAction {
request another user's timeline via the id parameter below. This is the equivalent of the Web request another user's timeline via the id parameter below. This is the equivalent of the Web
/archive page for your own user, or the profile page for a third party. /archive page for your own user, or the profile page for a third party.
URL: http://identi.ca/api/statuses/user_timeline.format URL: http://server/api/statuses/user_timeline.format
Formats: xml, json, rss, atom Formats: xml, json, rss, atom
Parameters: Parameters:
* id. Optional. Specifies the ID or screen name of the user for whom to return the * id. Optional. Specifies the ID or screen name of the user for whom to return the
friends_timeline. Ex: http://identi.ca/api/statuses/user_timeline/12345.xml or friends_timeline. Ex: http://server/api/statuses/user_timeline/12345.xml or
http://identi.ca/api/statuses/user_timeline/bob.json. http://server/api/statuses/user_timeline/bob.json.
* count. Optional. Specifies the number of * count. Optional. Specifies the number of
statuses to retrieve. May not be greater than 200. Ex: statuses to retrieve. May not be greater than 200. Ex:
http://identi.ca/api/statuses/user_timeline.xml?count=5 http://server/api/statuses/user_timeline.xml?count=5
* since. Optional. Narrows the returned * since. Optional. Narrows the returned
results to just those statuses created after the specified HTTP-formatted date. The same results to just those statuses created after the specified HTTP-formatted date. The same
behavior is available by setting an If-Modified-Since header in your HTTP request. Ex: behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
http://identi.ca/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT http://server/api/statuses/user_timeline.rss?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
* since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than)
the specified ID. Ex: http://identi.ca/api/statuses/user_timeline.xml?since_id=12345 * page. the specified ID. Ex: http://server/api/statuses/user_timeline.xml?since_id=12345 * page.
Optional. Ex: http://identi.ca/api/statuses/friends_timeline.rss?page=3 Optional. Ex: http://server/api/statuses/friends_timeline.rss?page=3
*/ */
function user_timeline($args, $apidata) { function user_timeline($args, $apidata) {
parent::handle($args); parent::handle($args);
@ -230,14 +229,14 @@ class TwitapistatusesAction extends TwitterapiAction {
/* /*
Returns a single status, specified by the id parameter below. The status's author will be returned inline. Returns a single status, specified by the id parameter below. The status's author will be returned inline.
URL: http://identi.ca/api/statuses/show/id.format URL: http://server/api/statuses/show/id.format
Formats: xml, json Formats: xml, json
Parameters: Parameters:
* id. Required. The numerical ID of the status you're trying to retrieve. * id. Required. The numerical ID of the status you're trying to retrieve.
Ex: http://identi.ca/api/statuses/show/123.xml Ex: http://server/api/statuses/show/123.xml
*/ */
function show($args, $apidata) { function show($args, $apidata) {
parent::handle($args); parent::handle($args);
@ -254,7 +253,7 @@ class TwitapistatusesAction extends TwitterapiAction {
/* /*
Updates the authenticating user's status. Requires the status parameter specified below. Request must be a POST. Updates the authenticating user's status. Requires the status parameter specified below. Request must be a POST.
URL: http://identi.ca/api/statuses/update.format URL: http://server/api/statuses/update.format
Formats: xml, json. Returns the posted status in requested format when successful. Formats: xml, json. Returns the posted status in requested format when successful.
@ -271,18 +270,18 @@ class TwitapistatusesAction extends TwitterapiAction {
/* /*
Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user. Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
URL: http://identi.ca/api/statuses/replies.format URL: http://server/api/statuses/replies.format
Formats: xml, json, rss, atom Formats: xml, json, rss, atom
Parameters: Parameters:
* page. Optional. Retrieves the 20 next most recent replies. Ex: http://identi.ca/api/statuses/replies.xml?page=3 * page. Optional. Retrieves the 20 next most recent replies. Ex: http://server/api/statuses/replies.xml?page=3
* since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The * since. Optional. Narrows the returned results to just those replies created after the specified HTTP-formatted date. The
same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex: same behavior is available by setting an If-Modified-Since header in your HTTP request. Ex:
http://identi.ca/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT http://server/api/statuses/replies.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
* since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified * since_id. Optional. Returns only statuses with an ID greater than (that is, more recent than) the specified
ID. Ex: http://identi.ca/api/statuses/replies.xml?since_id=12345 ID. Ex: http://server/api/statuses/replies.xml?since_id=12345
*/ */
function replies($args, $apidata) { function replies($args, $apidata) {
parent::handle($args); parent::handle($args);
@ -294,15 +293,15 @@ class TwitapistatusesAction extends TwitterapiAction {
Destroys the status specified by the required ID parameter. The authenticating user must be Destroys the status specified by the required ID parameter. The authenticating user must be
the author of the specified status. the author of the specified status.
URL: http://identi.ca/api/statuses/destroy/id.format URL: http://server/api/statuses/destroy/id.format
Formats: xml, json Formats: xml, json
Parameters: Parameters:
* id. Required. The ID of the status to destroy. Ex: * id. Required. The ID of the status to destroy. Ex:
http://identi.ca/api/statuses/destroy/12345.json or http://server/api/statuses/destroy/12345.json or
http://identi.ca/api/statuses/destroy/23456.xml http://server/api/statuses/destroy/23456.xml
*/ */
function destroy($args, $apidata) { function destroy($args, $apidata) {
@ -316,22 +315,22 @@ class TwitapistatusesAction extends TwitterapiAction {
Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline. Returns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.
It's also possible to request another user's recent friends list via the id parameter below. It's also possible to request another user's recent friends list via the id parameter below.
URL: http://identi.ca/api/statuses/friends.format URL: http://server/api/statuses/friends.format
Formats: xml, json Formats: xml, json
Parameters: Parameters:
* id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex: * id. Optional. The ID or screen name of the user for whom to request a list of friends. Ex:
http://identi.ca/api/statuses/friends/12345.json http://server/api/statuses/friends/12345.json
or or
http://identi.ca/api/statuses/friends/bob.xml http://server/api/statuses/friends/bob.xml
* page. Optional. Retrieves the next 100 friends. Ex: http://identi.ca/api/statuses/friends.xml?page=2 * page. Optional. Retrieves the next 100 friends. Ex: http://server/api/statuses/friends.xml?page=2
* lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex: * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. Ex:
http://identi.ca/api/statuses/friends.xml?lite=true http://server/api/statuses/friends.xml?lite=true
* since. Optional. Narrows the returned results to just those friendships created after the specified * since. Optional. Narrows the returned results to just those friendships created after the specified
HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP HTTP-formatted date. The same behavior is available by setting an If-Modified-Since header in your HTTP
request. Ex: http://identi.ca/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT request. Ex: http://server/api/statuses/friends.xml?since=Tue%2C+27+Mar+2007+22%3A55%3A48+GMT
*/ */
function friends($args, $apidata) { function friends($args, $apidata) {
parent::handle($args); parent::handle($args);
@ -342,18 +341,18 @@ class TwitapistatusesAction extends TwitterapiAction {
Returns the authenticating user's followers, each with current status inline. They are ordered by the Returns the authenticating user's followers, each with current status inline. They are ordered by the
order in which they joined Twitter (this is going to be changed). order in which they joined Twitter (this is going to be changed).
URL: http://identi.ca/api/statuses/followers.format URL: http://server/api/statuses/followers.format
Formats: xml, json Formats: xml, json
Parameters: Parameters:
* id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex: * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex:
http://identi.ca/api/statuses/followers/12345.json http://server/api/statuses/followers/12345.json
or or
http://identi.ca/api/statuses/followers/bob.xml http://server/api/statuses/followers/bob.xml
* page. Optional. Retrieves the next 100 followers. Ex: http://identi.ca/api/statuses/followers.xml?page=2 * page. Optional. Retrieves the next 100 followers. Ex: http://server/api/statuses/followers.xml?page=2
* lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true. * lite. Optional. Prevents the inline inclusion of current status. Must be set to a value of true.
Ex: http://identi.ca/api/statuses/followers.xml?lite=true Ex: http://server/api/statuses/followers.xml?lite=true
*/ */
function followers($args, $apidata) { function followers($args, $apidata) {
parent::handle($args); parent::handle($args);
@ -362,7 +361,7 @@ class TwitapistatusesAction extends TwitterapiAction {
/* /*
Returns a list of the users currently featured on the site with their current statuses inline. Returns a list of the users currently featured on the site with their current statuses inline.
URL: http://identi.ca/api/statuses/featured.format URL: http://server/api/statuses/featured.format
Formats: xml, json Formats: xml, json
*/ */

View File

@ -68,27 +68,26 @@ class TwitterapiAction extends Action {
function twitter_rss_entry_array($notice) { function twitter_rss_entry_array($notice) {
$profile = $notice->getProfile(); $profile = $notice->getProfile();
$server = common_config('site', 'server'); $server = common_config('site', 'server');
$entry = array(); $entry = array();
$entry['content'] = $profile->nickname . ': ' . $notice->content; $entry['content'] = $profile->nickname . ': ' . $notice->content;
$entry['title'] = $entry['content']; $entry['title'] = $entry['content'];
$entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));; $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id));;
$entry['published'] = $this->date_iso8601($notice->created); $entry['published'] = common_date_iso8601($notice->created);
$entry['id'] = "tag:$server,$entry[published]:$entry[link]"; $entry['id'] = "tag:$server,$entry[published]:$entry[link]";
$entry['updated'] = $entry['published']; $entry['updated'] = $entry['published'];
# RSS Item specific # RSS Item specific
$entry['description'] = $entry['content']; $entry['description'] = $entry['content'];
$entry['pubDate'] = $this->date_rfc2822($notice->created); $entry['pubDate'] = common_date_rfc2822($notice->created);
$entry['guid'] = $entry['link']; $entry['guid'] = $entry['link'];
return $entry; return $entry;
} }
function render_twitter_xml_status($twitter_status) { function show_twitter_xml_status($twitter_status) {
common_element_start('status'); common_element_start('status');
common_element('created_at', NULL, $twitter_status['created_at']); common_element('created_at', NULL, $twitter_status['created_at']);
common_element('id', NULL, $twitter_status['id']); common_element('id', NULL, $twitter_status['id']);
@ -99,12 +98,12 @@ class TwitterapiAction extends Action {
common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']); common_element('in_reply_to_user_id', NULL, $twitter_status['in_reply_to_user_id']);
common_element('favorited', Null, $twitter_status['favorited']); common_element('favorited', Null, $twitter_status['favorited']);
$this->render_twitter_xml_user($twitter_status['user']); $this->show_twitter_xml_user($twitter_status['user']);
common_element_end('status'); common_element_end('status');
} }
function render_twitter_xml_user($twitter_user) { function show_twitter_xml_user($twitter_user) {
common_element_start('user'); common_element_start('user');
common_element('id', NULL, $twitter_user['id']); common_element('id', NULL, $twitter_user['id']);
common_element('name', NULL, $twitter_user['name']); common_element('name', NULL, $twitter_user['name']);
@ -139,7 +138,7 @@ class TwitterapiAction extends Action {
common_element_end('entry'); common_element_end('entry');
} }
function render_twitter_json_statuses($twitter_statuses) { function show_twitter_json_statuses($twitter_statuses) {
print(json_encode($twitter_statuses)); print(json_encode($twitter_statuses));
} }
@ -150,16 +149,6 @@ class TwitterapiAction extends Action {
return date("D M d G:i:s O Y", $t); return date("D M d G:i:s O Y", $t);
} }
function date_rfc2822($dt) {
$t = strtotime($dt);
return date("r", $t);
}
function date_iso8601($dt) {
$t = strtotime($dt);
return date("c", $t);
}
function replier_by_reply($reply_id) { function replier_by_reply($reply_id) {
$notice = Notice::staticGet($reply_id); $notice = Notice::staticGet($reply_id);
@ -216,11 +205,7 @@ class TwitterapiAction extends Action {
common_element_end('rss'); common_element_end('rss');
common_end_xml(); common_end_xml();
} }
function get_twitter_channel() {
}
function init_twitter_atom() { function init_twitter_atom() {
common_start_xml(); common_start_xml();
common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US')); common_element_start('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US'));

View File

@ -861,6 +861,16 @@ function common_date_w3dtf($dt) {
return date(DATE_W3C, $t); return date(DATE_W3C, $t);
} }
function common_date_rfc2822($dt) {
$t = strtotime($dt);
return date("r", $t);
}
function common_date_iso8601($dt) {
$t = strtotime($dt);
return date("c", $t);
}
function common_redirect($url, $code=307) { function common_redirect($url, $code=307) {
static $status = array(301 => "Moved Permanently", static $status = array(301 => "Moved Permanently",
302 => "Found", 302 => "Found",