Make SyncTwitterFriends and TwitterStatusFetcher daemons use both HTTP Basic Auth as well as OAuth

This commit is contained in:
Zach Copley 2009-08-28 07:02:27 +00:00
parent 36b6ef8d05
commit 36c104fb34
6 changed files with 35 additions and 15 deletions

View File

@ -22,7 +22,7 @@
* @category Action * @category Action
* @package StatusNet * @package StatusNet
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2008 StatusNet, Inc. * @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */

View File

@ -88,7 +88,7 @@ class TwitterBasicAuthClient
$params = array('status' => $status, $params = array('status' => $status,
'source' => common_config('integration', 'source'), 'source' => common_config('integration', 'source'),
'in_reply_to_status_id' => $in_reply_to_status_id); 'in_reply_to_status_id' => $in_reply_to_status_id);
$response = $this->httpRequest($url, $params, true); $response = $this->httpRequest($url, $params);
$status = json_decode($response); $status = json_decode($response);
return $status; return $status;
} }
@ -117,7 +117,7 @@ class TwitterBasicAuthClient
$url .= "?$qry"; $url .= "?$qry";
} }
$response = $this->httpRequest($url, null, true); $response = $this->httpRequest($url);
$statuses = json_decode($response); $statuses = json_decode($response);
return $statuses; return $statuses;
} }
@ -190,7 +190,7 @@ class TwitterBasicAuthClient
* *
* @return mixed the request * @return mixed the request
*/ */
function httpRequest($url, $params = null, $auth = false) function httpRequest($url, $params = null, $auth = true)
{ {
$options = array( $options = array(
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,

View File

@ -22,7 +22,7 @@
* @category Integration * @category Integration
* @package StatusNet * @package StatusNet
* @author Zach Copley <zach@status.net> * @author Zach Copley <zach@status.net>
* @copyright 2008 StatusNet, Inc. * @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */

View File

@ -19,6 +19,8 @@
*/ */
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
define('LACONICA', true); // compatibility
$shortoptions = 'di::'; $shortoptions = 'di::';
$longoptions = array('id::', 'debug'); $longoptions = array('id::', 'debug');
@ -142,13 +144,20 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
{ {
$friends = array(); $friends = array();
$token = TwitterOAuthClient::unpackToken($flink->credentials); $client = null;
$client = new TwitterOAuthClient($token->key, $token->secret); if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
$token = TwitterOAuthClient::unpackToken($flink->credentials);
$client = new TwitterOAuthClient($token->key, $token->secret);
common_debug($this->name() . '- Grabbing friends IDs with OAuth.');
} else {
$client = new TwitterBasicAuthClient($flink);
common_debug($this->name() . '- Grabbing friends IDs with basic auth.');
}
try { try {
$friends_ids = $client->friendsIds(); $friends_ids = $client->friendsIds();
} catch (OAuthCurlException $e) { } catch (Exception $e) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_WARNING, $this->name() .
' - cURL error getting friend ids ' . ' - cURL error getting friend ids ' .
$e->getCode() . ' - ' . $e->getMessage()); $e->getCode() . ' - ' . $e->getMessage());
@ -177,7 +186,7 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon
try { try {
$more_friends = $client->statusesFriends(null, null, null, $i); $more_friends = $client->statusesFriends(null, null, null, $i);
} catch (OAuthCurlException $e) { } catch (Exception $e) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_WARNING, $this->name() .
' - cURL error getting Twitter statuses/friends ' . ' - cURL error getting Twitter statuses/friends ' .
"page $i - " . $e->getCode() . ' - ' . "page $i - " . $e->getCode() . ' - ' .

View File

@ -19,6 +19,8 @@
*/ */
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
define('LACONICA', true); // compatibility
$shortoptions = 'i::'; $shortoptions = 'i::';
$longoptions = array('id::'); $longoptions = array('id::');

View File

@ -19,6 +19,8 @@
*/ */
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
define('LACONICA', true); // compatibility
// Tune number of processes and how often to poll Twitter // Tune number of processes and how often to poll Twitter
// XXX: Should these things be in config.php? // XXX: Should these things be in config.php?
@ -148,9 +150,9 @@ class TwitterStatusFetcher extends ParallelizingDaemon
function getTimeline($flink) function getTimeline($flink)
{ {
if (empty($flink)) { if (empty($flink)) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_WARNING, $this->name() .
" - Can't retrieve Foreign_link for foreign ID $fid"); " - Can't retrieve Foreign_link for foreign ID $fid");
return; return;
} }
@ -161,17 +163,24 @@ class TwitterStatusFetcher extends ParallelizingDaemon
// to start importing? How many statuses? Right now I'm going // to start importing? How many statuses? Right now I'm going
// with the default last 20. // with the default last 20.
$token = TwitterOAuthClient::unpackToken($flink->credentials); $client = null;
$client = new TwitterOAuthClient($token->key, $token->secret); if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
$token = TwitterOAuthClient::unpackToken($flink->credentials);
$client = new TwitterOAuthClient($token->key, $token->secret);
common_debug($this->name() . ' - Grabbing friends timeline with OAuth.');
} else {
$client = new TwitterBasicAuthClient($flink);
common_debug($this->name() . ' - Grabbing friends timeline with basic auth.');
}
$timeline = null; $timeline = null;
try { try {
$timeline = $client->statusesFriendsTimeline(); $timeline = $client->statusesFriendsTimeline();
} catch (OAuthClientCurlException $e) { } catch (Exception $e) {
common_log(LOG_WARNING, $this->name() . common_log(LOG_WARNING, $this->name() .
' - OAuth client unable to get friends timeline for user ' . ' - Twitter client unable to get friends timeline for user ' .
$flink->user_id . ' - code: ' . $flink->user_id . ' - code: ' .
$e->getCode() . 'msg: ' . $e->getMessage()); $e->getCode() . 'msg: ' . $e->getMessage());
} }