2008-11-19 10:11:28 +09:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
/*
|
2009-08-26 07:14:12 +09:00
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
2009-08-26 07:12:20 +09:00
|
|
|
* Copyright (C) 2008, 2009, StatusNet, Inc.
|
2008-11-19 10:11:28 +09:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-12-24 04:19:07 +09:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-11-19 10:11:28 +09:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2008-12-24 04:19:07 +09:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-11-19 10:11:28 +09:00
|
|
|
*/
|
|
|
|
|
2009-08-26 09:59:06 +09:00
|
|
|
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$shortoptions = 'di::';
|
|
|
|
$longoptions = array('id::', 'debug');
|
|
|
|
|
|
|
|
$helptext = <<<END_OF_TRIM_HELP
|
|
|
|
Batch script for synching local friends with Twitter friends.
|
|
|
|
-i --id Identity (default 'generic')
|
|
|
|
-d --debug Debug (lots of log output)
|
|
|
|
|
|
|
|
END_OF_TRIM_HELP;
|
|
|
|
|
|
|
|
require_once INSTALLDIR . '/scripts/commandline.inc';
|
|
|
|
require_once INSTALLDIR . '/lib/parallelizingdaemon.php';
|
2009-08-26 09:59:06 +09:00
|
|
|
require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
|
2009-10-14 13:50:16 +09:00
|
|
|
require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
|
|
|
|
require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
|
2009-08-06 16:03:05 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Daemon to sync local friends with Twitter friends
|
|
|
|
*
|
|
|
|
* @category Twitter
|
2009-08-26 07:12:20 +09:00
|
|
|
* @package StatusNet
|
2009-08-26 07:19:04 +09:00
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
2009-08-06 16:03:05 +09:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-26 07:16:46 +09:00
|
|
|
* @link http://status.net/
|
2009-08-06 16:03:05 +09:00
|
|
|
*/
|
2009-04-08 10:29:10 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
class SyncTwitterFriendsDaemon extends ParallelizingDaemon
|
|
|
|
{
|
2009-08-07 07:52:58 +09:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $id the name/id of this daemon
|
|
|
|
* @param int $interval sleep this long before doing everything again
|
|
|
|
* @param int $max_children maximum number of child processes at a time
|
|
|
|
* @param boolean $debug debug output flag
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
function __construct($id = null, $interval = 60,
|
|
|
|
$max_children = 2, $debug = null)
|
|
|
|
{
|
|
|
|
parent::__construct($id, $interval, $max_children, $debug);
|
|
|
|
}
|
2009-05-08 10:22:14 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
/**
|
|
|
|
* Name of this daemon
|
|
|
|
*
|
|
|
|
* @return string Name of the daemon.
|
|
|
|
*/
|
2009-04-08 10:29:10 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
function name()
|
|
|
|
{
|
2009-08-10 16:18:09 +09:00
|
|
|
return ('synctwitterfriends.' . $this->_id);
|
2009-08-06 16:03:05 +09:00
|
|
|
}
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-07 07:52:58 +09:00
|
|
|
/**
|
|
|
|
* Find all the Twitter foreign links for users who have requested
|
|
|
|
* automatically subscribing to their Twitter friends locally.
|
|
|
|
*
|
|
|
|
* @return array flinks an array of Foreign_link objects
|
|
|
|
*/
|
2009-08-06 16:03:05 +09:00
|
|
|
function getObjects()
|
|
|
|
{
|
|
|
|
$flinks = array();
|
|
|
|
$flink = new Foreign_link();
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$conn = &$flink->getDatabaseConnection();
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$flink->service = TWITTER_SERVICE;
|
|
|
|
$flink->orderBy('last_friendsync');
|
|
|
|
$flink->limit(25); // sync this many users during this run
|
|
|
|
$flink->find();
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
while ($flink->fetch()) {
|
|
|
|
if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
|
|
|
|
$flinks[] = clone($flink);
|
|
|
|
}
|
2009-04-08 10:29:10 +09:00
|
|
|
}
|
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$conn->disconnect();
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
global $_DB_DATAOBJECT;
|
|
|
|
unset($_DB_DATAOBJECT['CONNECTIONS']);
|
2008-11-20 05:45:01 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
return $flinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
function childTask($flink) {
|
2008-11-20 05:45:01 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
// Each child ps needs its own DB connection
|
|
|
|
|
|
|
|
// Note: DataObject::getDatabaseConnection() creates
|
|
|
|
// a new connection if there isn't one already
|
|
|
|
|
|
|
|
$conn = &$flink->getDatabaseConnection();
|
|
|
|
|
|
|
|
$this->subscribeTwitterFriends($flink);
|
2009-05-08 10:22:14 +09:00
|
|
|
|
|
|
|
$flink->last_friendsync = common_sql_now();
|
|
|
|
$flink->update();
|
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$conn->disconnect();
|
|
|
|
|
|
|
|
// XXX: Couldn't find a less brutal way to blow
|
|
|
|
// away a cached connection
|
|
|
|
|
|
|
|
global $_DB_DATAOBJECT;
|
|
|
|
unset($_DB_DATAOBJECT['CONNECTIONS']);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchTwitterFriends($flink)
|
|
|
|
{
|
|
|
|
$friends = array();
|
|
|
|
|
2009-08-28 16:02:27 +09:00
|
|
|
$client = null;
|
2009-08-10 16:00:59 +09:00
|
|
|
|
2009-08-28 16:02:27 +09:00
|
|
|
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.');
|
|
|
|
}
|
2009-08-06 16:03:05 +09:00
|
|
|
|
|
|
|
try {
|
2009-08-10 15:05:43 +09:00
|
|
|
$friends_ids = $client->friendsIds();
|
2009-08-28 16:02:27 +09:00
|
|
|
} catch (Exception $e) {
|
2009-08-06 16:03:05 +09:00
|
|
|
common_log(LOG_WARNING, $this->name() .
|
2009-10-29 04:29:20 +09:00
|
|
|
' - error getting friend ids: ' .
|
|
|
|
$e->getMessage());
|
2009-08-06 16:03:05 +09:00
|
|
|
return $friends;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($friends_ids)) {
|
|
|
|
common_debug($this->name() .
|
|
|
|
" - Twitter user $flink->foreign_id " .
|
|
|
|
'doesn\'t have any friends!');
|
|
|
|
return $friends;
|
|
|
|
}
|
|
|
|
|
|
|
|
common_debug($this->name() . ' - Twitter\'s API says Twitter user id ' .
|
|
|
|
"$flink->foreign_id has " .
|
|
|
|
count($friends_ids) . ' friends.');
|
|
|
|
|
|
|
|
// Calculate how many pages to get...
|
|
|
|
$pages = ceil(count($friends_ids) / 100);
|
|
|
|
|
|
|
|
if ($pages == 0) {
|
|
|
|
common_debug($this->name() . " - $user seems to have no friends.");
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2009-08-06 16:03:05 +09:00
|
|
|
|
|
|
|
for ($i = 1; $i <= $pages; $i++) {
|
|
|
|
|
|
|
|
try {
|
2009-08-10 15:05:43 +09:00
|
|
|
$more_friends = $client->statusesFriends(null, null, null, $i);
|
2009-08-28 16:02:27 +09:00
|
|
|
} catch (Exception $e) {
|
2009-08-06 16:03:05 +09:00
|
|
|
common_log(LOG_WARNING, $this->name() .
|
|
|
|
' - cURL error getting Twitter statuses/friends ' .
|
|
|
|
"page $i - " . $e->getCode() . ' - ' .
|
|
|
|
$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($more_friends)) {
|
|
|
|
common_log(LOG_WARNING, $this->name() .
|
|
|
|
" - Couldn't retrieve page $i " .
|
|
|
|
"of Twitter user $flink->foreign_id friends.");
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$friends = array_merge($friends, $more_friends);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $friends;
|
2008-12-24 04:19:07 +09:00
|
|
|
}
|
2008-11-19 10:11:28 +09:00
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
function subscribeTwitterFriends($flink)
|
|
|
|
{
|
|
|
|
$friends = $this->fetchTwitterFriends($flink);
|
|
|
|
|
|
|
|
if (empty($friends)) {
|
|
|
|
common_debug($this->name() .
|
|
|
|
' - Couldn\'t get friends from Twitter for ' .
|
|
|
|
"Twitter user $flink->foreign_id.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $flink->getUser();
|
|
|
|
|
|
|
|
foreach ($friends as $friend) {
|
|
|
|
|
|
|
|
$friend_name = $friend->screen_name;
|
|
|
|
$friend_id = (int) $friend->id;
|
|
|
|
|
|
|
|
// Update or create the Foreign_user record for each
|
|
|
|
// Twitter friend
|
|
|
|
|
|
|
|
if (!save_twitter_user($friend_id, $friend_name)) {
|
|
|
|
common_log(LOG_WARNING, $this-name() .
|
|
|
|
" - Couldn't save $screen_name's friend, $friend_name.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if there's a related local user
|
|
|
|
|
|
|
|
$friend_flink = Foreign_link::getByForeignID($friend_id,
|
|
|
|
TWITTER_SERVICE);
|
|
|
|
|
|
|
|
if (!empty($friend_flink)) {
|
|
|
|
|
|
|
|
// Get associated user and subscribe her
|
|
|
|
|
|
|
|
$friend_user = User::staticGet('id', $friend_flink->user_id);
|
|
|
|
|
|
|
|
if (!empty($friend_user)) {
|
|
|
|
$result = subs_subscribe_to($user, $friend_user);
|
|
|
|
|
|
|
|
if ($result === true) {
|
|
|
|
common_log(LOG_INFO,
|
|
|
|
$this->name() . ' - Subscribed ' .
|
|
|
|
"$friend_user->nickname to $user->nickname.");
|
|
|
|
} else {
|
|
|
|
common_debug($this->name() .
|
|
|
|
' - Tried subscribing ' .
|
|
|
|
"$friend_user->nickname to $user->nickname - " .
|
|
|
|
$result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-08 10:22:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-08-06 16:03:05 +09:00
|
|
|
$id = null;
|
|
|
|
$debug = null;
|
|
|
|
|
|
|
|
if (have_option('i')) {
|
|
|
|
$id = get_option_value('i');
|
|
|
|
} else if (have_option('--id')) {
|
|
|
|
$id = get_option_value('--id');
|
|
|
|
} else if (count($args) > 0) {
|
|
|
|
$id = $args[0];
|
|
|
|
} else {
|
|
|
|
$id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_option('d') || have_option('debug')) {
|
|
|
|
$debug = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);
|
|
|
|
$syncer->runOnce();
|
2009-05-08 10:22:14 +09:00
|
|
|
|