Skip to content

Commit

Permalink
adding curl timeout, updaing module with amend to Twitter API
Browse files Browse the repository at this point in the history
  • Loading branch information
shapeshed committed Aug 23, 2009
1 parent f50ebde commit 2838a4c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.markdown
Expand Up @@ -65,12 +65,12 @@ Returns the 20 most recent statuses posted from the authenticating user. It's al
{exp:twitee:user_timeline}
{/exp:twitee:user_timeline}

#### Replies ####
#### Mentions ####

Returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
Returns the 20 most recent mentions (status containing @username) for the authenticating user.

{exp:twitee:replies}
{/exp:twitee:replies}
{exp:twitee:mentions}
{/exp:twitee:mentions}

#### Favorites ####

Expand Down
3 changes: 3 additions & 0 deletions language/english/lang.twitee.php
Expand Up @@ -66,6 +66,9 @@
"twitee_error_503" =>
"Oh dear - The Twitter servers are up, but are overloaded with requests. (Error 503)",

"twitee_down" =>
"Sorry, Twitter seems to be down.",

//----------------------------------------

// END
Expand Down
4 changes: 2 additions & 2 deletions modules/twitee/mcp.twitee.php
Expand Up @@ -9,7 +9,7 @@
*
* Fetches data from Twitter for display in templates
*
* @version 1.15
* @version 1.3
* @author George Ornbo <george@shapeshed.com>
* @license http://opensource.org/licenses/bsd-license.php
*/
Expand All @@ -27,7 +27,7 @@ class Twitee_CP {
* Version number of the module
* @var string
*/
var $version = '1.15';
var $version = '1.3';

/**
* Settings used in this module
Expand Down
155 changes: 130 additions & 25 deletions modules/twitee/mod.twitee.php
Expand Up @@ -7,7 +7,7 @@
*
* This class is derived from {@link http://code.google.com/p/arc90-service-twitter/ Arc90_Service_Twitter}
*
* @version 1.15
* @version 1.3
* @author George Ornbo <george@shapeshed.com>
* @license {@link http://opensource.org/licenses/bsd-license.php BSD License}
*/
Expand All @@ -16,7 +16,7 @@
* @see twitee_response
*/
require_once PATH_MOD.'twitee/mod_twitee_response.php';

define("SECOND", 1);
define("MINUTE", 60 * SECOND);
define("HOUR", 60 * MINUTE);
Expand All @@ -35,13 +35,23 @@ class Twitee{
const API_URL = 'http://twitter.com';

const PATH_STATUS_PUBLIC = '/statuses/public_timeline';
const PATH_STATUS_HOME = '/statuses/home_timeline';
const PATH_STATUS_FRIENDS = '/statuses/friends_timeline';
const PATH_STATUS_USER = '/statuses/user_timeline';
const PATH_STATUS_REPLIES = '/statuses/replies';
const PATH_MENTIONS = '/statuses/mentions';
const PATH_RETWEETED_BY_ME = '/statuses/retweeted_by_me';
const PATH_RETWEETED_TO_ME = '/statuses/retweeted_to_me';
const PATH_RETWEETS_OF_ME = '/statuses/retweets_of_me';
const PATH_USER_FRIENDS = '/statuses/friends';
const PATH_USER_FOLLOWERS = '/statuses/followers';
const PATH_FAV_FAVORITES = '/favorites';

/**
* Module version
* @var string
*/
var $version = "1.3";

/**
* Data sent back to calling function
* @var string
Expand Down Expand Up @@ -87,6 +97,13 @@ class Twitee{
* @var string
*/
public $cache_folder = "twitter_cache/";

/**
* Sets the number of seconds after which the module will timeout
* @see __construct
* @var integer
*/
public $timeout = "";

/**
* List of possible HTTP error response codes from the Twitter API
Expand Down Expand Up @@ -149,6 +166,7 @@ public function __construct($username =null, $password =null)
$this->refresh = ( ! $TMPL->fetch_param('refresh')) ? '300' : $TMPL->fetch_param('refresh') * 60;
$this->limit = ( ! $TMPL->fetch_param('limit')) ? '10' : $TMPL->fetch_param('limit');
$this->site_id = ( ! $TMPL->fetch_param('site_id')) ? $PREFS->ini('site_id') : $TMPL->fetch_param('site_id');
$this->timeout = ( ! $TMPL->fetch_param('timeout')) ? '1' : $TMPL->fetch_param('timeout');

/* Added by Leevi Graham (http://leevigraham.com), Technical Director - Newism (http://newism.com.au) */
$this->convert_urls = ( ! $TMPL->fetch_param('convert_urls')) ? "y" : $TMPL->fetch_param('convert_urls');
Expand Down Expand Up @@ -194,6 +212,17 @@ public function public_timeline()
return $this->_getData("public_timeline", self::PATH_STATUS_PUBLIC, false, "status" );
}

// /**
// * Returns Twitter Home Timeline
// *
// * @see _getData
// * @return string Returns parsed data from Twitter API ready for display in templates
// */
// public function home_timeline()
// {
// return $this->_getData("home_timeline", self::PATH_STATUS_HOME, true, "status" );
// }

/**
* Returns Twitter Friends Timeline
*
Expand Down Expand Up @@ -222,11 +251,44 @@ public function user_timeline()
* @see _getData
* @return string Returns parsed data from Twitter API ready for display in templates
*/
function replies()
function mentions()
{
return $this->_getData($this->account_id."_replies", self::PATH_STATUS_REPLIES, true, "status");
return $this->_getData($this->account_id."_mentions", self::PATH_MENTIONS, true, "status");
}

// /**
// * Returns Twitter Retweets by the authenticated user
// *
// * @see _getData
// * @return string Returns parsed data from Twitter API ready for display in templates
// */
// function retweeted_by_me()
// {
// return $this->_getData($this->account_id."_retweeted_by_me", self::PATH_RETWEETED_BY_ME, true, "status");
// }

// /**
// * Returns Twitter Retweets to the authenticated user
// *
// * @see _getData
// * @return string Returns parsed data from Twitter API ready for display in templates
// */
// function retweeted_to_me()
// {
// return $this->_getData($this->account_id."_retweeted_to_me", self::PATH_RETWEETED_TO_ME, true, "status");
// }
//
// /**
// * Returns Twitter Retweets of the authenticated user
// *
// * @see _getData
// * @return string Returns parsed data from Twitter API ready for display in templates
// */
// function retweets_of_me()
// {
// return $this->_getData($this->account_id."_retweets_of_me", self::PATH_RETWEETS_OF_ME, true, "status");
// }

/**
* Returns Twitter Friends for the authenticated user
*
Expand Down Expand Up @@ -283,27 +345,63 @@ protected function _getData($filename, $path, $auth, $parser)

else
{
$response = $this->_makeRequest($path, $this->format, $auth);

if (!in_array($response->_metadata['http_code'], $this->errors))
$response = $this->_makeRequest($path, $this->format, $auth);

/*********************************
* Handle the response
**********************************/
if($response)
{
$this->_updateCache($response->_data, $filename);
$xml = new SimpleXMLElement($response->_data);
/*********************************
* HTTP response ok? Update the cache and prepare for parsing
**********************************/
if (!in_array($response->_metadata['http_code'], $this->errors))
{
$this->_updateCache($response->_data, $filename);
$xml = new SimpleXMLElement($response->_data);
}
else
{
/*********************************
* HTTP response not 200 so handle error
**********************************/
return $this->_handleError($response);
}
}
/*********************************
* No response so attempt to serve up cache file
**********************************/
else
{
return $this->_handleError($response);
$cache_file = PATH_CACHE . $this->cache_folder . $filename .'.'. $this->format;

if (file_exists($cache_file))
{
$xml = simplexml_load_file($cache_file);
}
/*********************************
* No cache file so time for error message
**********************************/
else
{
global $LANG;
$LANG->fetch_language_file('twitee');
echo $LANG->line('twitee_down');
}
}

}
switch($parser)
if (!empty($xml))
{
case 'status':
return $this->_parse_status($xml);
break;
case 'basic_user':
return $this->_parse_basic_user($xml);
break;
switch($parser)
{
case 'status':
return $this->_parse_status($xml);
break;
case 'basic_user':
return $this->_parse_basic_user($xml);
break;
}
}

}
Expand All @@ -318,12 +416,12 @@ protected function _getData($filename, $path, $auth, $parser)
* @see function updateCache()
* @return string Returns raw data from the Twitter API request
*/
protected function _makeRequest($url, $format = 'xml', $auth = false, $data = '')
protected function _makeRequest($url, $format = 'xml', $auth = false, $data = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_URL . $url .'.'. $format);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$this->timeout);
if($auth)
{
curl_setopt($ch, CURLOPT_USERPWD, "{$this->_authUsername}:{$this->_authPassword}");
Expand All @@ -332,8 +430,16 @@ protected function _makeRequest($url, $format = 'xml', $auth = false, $data = ''
$data = curl_exec($ch);
$metadata = curl_getinfo($ch);
curl_close($ch);

return $this->_lastResponse = new Twitee_response($data, $metadata, $format);

if(!empty($data))
{
return $this->_lastResponse = new Twitee_response($data, $metadata, $format);
}
else
{
return false;
}

}

/**
Expand Down Expand Up @@ -626,7 +732,7 @@ function twitterStatusUrlConverter($status, $which = "link", $relExternal = TRUE
// The target
$rel = $relExternal ? " rel=\"extenal\" " : "";

if($this->convert_links == "y")
if($this->convert_urls == "y")
{
// convert link to url
$status = preg_replace("/((http:\/\/|https:\/\/)[^ )
Expand All @@ -648,8 +754,7 @@ function twitterStatusUrlConverter($status, $which = "link", $relExternal = TRUE
// return the status
return $status;
}



}

?>
2 changes: 1 addition & 1 deletion modules/twitee/mod_twitee_response.php
Expand Up @@ -7,7 +7,7 @@
*
* This class is derived from {@link http://code.google.com/p/arc90-service-twitter/ Arc90_Service_Twitter}
*
* @version 1.15
* @version 1.3
* @author George Ornbo <george@shapeshed.com>
* @license {@link http://opensource.org/licenses/bsd-license.php BSD License}
*/
Expand Down

0 comments on commit 2838a4c

Please sign in to comment.