2008-11-19 01:11:28 +00:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
2019-11-01 12:27:36 +00:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2008, 2009 StatusNet, Inc
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
2008-11-19 01:11:28 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-26 01:59:06 +01:00
|
|
|
define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
|
2019-11-01 12:27:36 +00:00
|
|
|
define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2009-08-06 08:03:05 +01: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';
|
2014-08-06 08:29:47 +01:00
|
|
|
require_once dirname(__DIR__) . '/twitter.php';
|
2009-08-06 08:03:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Daemon to sync local friends with Twitter friends
|
|
|
|
*
|
|
|
|
* @category Twitter
|
2009-08-25 23:12:20 +01:00
|
|
|
* @package StatusNet
|
2009-08-25 23:19:04 +01:00
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
2009-08-06 08:03:05 +01:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-25 23:16:46 +01:00
|
|
|
* @link http://status.net/
|
2009-08-06 08:03:05 +01:00
|
|
|
*/
|
|
|
|
class SyncTwitterFriendsDaemon extends ParallelizingDaemon
|
|
|
|
{
|
2009-08-06 23:52:58 +01: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
|
|
|
|
*
|
|
|
|
**/
|
2019-11-01 12:27:36 +00:00
|
|
|
public function __construct(
|
|
|
|
$id = null,
|
|
|
|
$interval = 60,
|
|
|
|
$max_children = 2,
|
|
|
|
$debug = null
|
|
|
|
) {
|
2009-08-06 08:03:05 +01:00
|
|
|
parent::__construct($id, $interval, $max_children, $debug);
|
|
|
|
}
|
2009-05-08 02:22:14 +01:00
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
/**
|
|
|
|
* Name of this daemon
|
|
|
|
*
|
|
|
|
* @return string Name of the daemon.
|
|
|
|
*/
|
2019-11-01 12:27:36 +00:00
|
|
|
public function name()
|
2009-08-06 08:03:05 +01:00
|
|
|
{
|
2009-08-10 08:18:09 +01:00
|
|
|
return ('synctwitterfriends.' . $this->_id);
|
2009-08-06 08:03:05 +01:00
|
|
|
}
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2009-08-06 23:52:58 +01: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
|
|
|
|
*/
|
2019-11-01 12:27:36 +00:00
|
|
|
public function getObjects()
|
2009-08-06 08:03:05 +01:00
|
|
|
{
|
|
|
|
$flinks = array();
|
|
|
|
$flink = new Foreign_link();
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2019-04-26 23:03:31 +01:00
|
|
|
$conn = $flink->getDatabaseConnection();
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
$flink->service = TWITTER_SERVICE;
|
|
|
|
$flink->orderBy('last_friendsync');
|
|
|
|
$flink->limit(25); // sync this many users during this run
|
|
|
|
$flink->find();
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
while ($flink->fetch()) {
|
|
|
|
if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
|
|
|
|
$flinks[] = clone($flink);
|
|
|
|
}
|
2009-04-08 02:29:10 +01:00
|
|
|
}
|
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
$conn->disconnect();
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
global $_DB_DATAOBJECT;
|
|
|
|
unset($_DB_DATAOBJECT['CONNECTIONS']);
|
2008-11-19 20:45:01 +00:00
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
return $flinks;
|
|
|
|
}
|
|
|
|
|
2015-07-18 10:39:34 +01:00
|
|
|
// FIXME: make it so we can force a Foreign_link here without colliding with parent
|
2019-11-01 12:27:36 +00:00
|
|
|
public function childTask($flink)
|
|
|
|
{
|
2009-08-06 08:03:05 +01:00
|
|
|
// Each child ps needs its own DB connection
|
|
|
|
|
|
|
|
// Note: DataObject::getDatabaseConnection() creates
|
2009-11-09 19:01:46 +00:00
|
|
|
// a new connection if there isn't one already
|
2009-08-06 08:03:05 +01:00
|
|
|
$conn = &$flink->getDatabaseConnection();
|
|
|
|
|
|
|
|
$this->subscribeTwitterFriends($flink);
|
2009-05-08 02:22:14 +01:00
|
|
|
|
|
|
|
$flink->last_friendsync = common_sql_now();
|
|
|
|
$flink->update();
|
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
$conn->disconnect();
|
|
|
|
|
2009-11-08 22:28:51 +00:00
|
|
|
// XXX: Couldn't find a less brutal way to blow
|
2009-08-06 08:03:05 +01:00
|
|
|
// away a cached connection
|
|
|
|
global $_DB_DATAOBJECT;
|
|
|
|
unset($_DB_DATAOBJECT['CONNECTIONS']);
|
|
|
|
}
|
|
|
|
|
2019-11-01 12:27:36 +00:00
|
|
|
public function fetchTwitterFriends(Foreign_link $flink)
|
2009-08-06 08:03:05 +01:00
|
|
|
{
|
|
|
|
$friends = array();
|
|
|
|
|
2009-08-28 08:02:27 +01:00
|
|
|
$client = null;
|
2009-08-10 08:00:59 +01:00
|
|
|
|
2009-08-28 08:02:27 +01: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 {
|
2010-09-04 17:58:20 +01:00
|
|
|
common_debug("Skipping Twitter friends for {$flink->user_id} since not OAuth.");
|
|
|
|
return $friends;
|
2009-08-28 08:02:27 +01:00
|
|
|
}
|
2009-08-06 08:03:05 +01:00
|
|
|
|
|
|
|
try {
|
2009-08-10 07:05:43 +01:00
|
|
|
$friends_ids = $client->friendsIds();
|
2009-08-28 08:02:27 +01:00
|
|
|
} catch (Exception $e) {
|
2009-08-06 08:03:05 +01:00
|
|
|
common_log(LOG_WARNING, $this->name() .
|
2009-10-28 19:29:20 +00:00
|
|
|
' - error getting friend ids: ' .
|
|
|
|
$e->getMessage());
|
2009-08-06 08:03:05 +01: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-23 19:19:07 +00:00
|
|
|
}
|
2009-08-06 08:03:05 +01:00
|
|
|
|
|
|
|
for ($i = 1; $i <= $pages; $i++) {
|
2019-11-01 12:27:36 +00:00
|
|
|
try {
|
|
|
|
$more_friends = $client->statusesFriends(null, null, null, $i);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(
|
|
|
|
LOG_WARNING,
|
|
|
|
$this->name() .
|
|
|
|
' - cURL error getting Twitter statuses/friends ' .
|
|
|
|
"page {$i} - " . $e->getCode() . ' - ' .
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2009-08-06 08:03:05 +01:00
|
|
|
|
|
|
|
if (empty($more_friends)) {
|
|
|
|
common_log(LOG_WARNING, $this->name() .
|
2009-11-08 22:28:51 +00:00
|
|
|
" - Couldn't retrieve page $i " .
|
2009-08-06 08:03:05 +01:00
|
|
|
"of Twitter user $flink->foreign_id friends.");
|
|
|
|
continue;
|
|
|
|
} else {
|
2015-02-09 01:11:50 +00:00
|
|
|
if (is_array($more_friends)) {
|
|
|
|
$friends = array_merge($friends, $more_friends);
|
|
|
|
}
|
2009-08-06 08:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $friends;
|
2008-12-23 19:19:07 +00:00
|
|
|
}
|
2008-11-19 01:11:28 +00:00
|
|
|
|
2019-11-01 12:27:36 +00:00
|
|
|
public function subscribeTwitterFriends(Foreign_link $flink)
|
2009-08-06 08:03:05 +01:00
|
|
|
{
|
2015-07-18 10:39:34 +01:00
|
|
|
try {
|
|
|
|
$profile = $flink->getProfile();
|
|
|
|
} catch (NoResultException $e) {
|
|
|
|
common_log(LOG_WARNING, 'Foreign_link has no matching local profile for local ID: '.$flink->user_id);
|
|
|
|
}
|
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2010-03-03 06:09:52 +00:00
|
|
|
common_log(LOG_WARNING, $this->name() .
|
2009-11-08 22:28:51 +00:00
|
|
|
" - Couldn't save $screen_name's friend, $friend_name.");
|
2009-08-06 08:03:05 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-18 10:39:34 +01:00
|
|
|
// Check to see if there's a related local user and try to subscribe
|
|
|
|
try {
|
|
|
|
$friend_flink = Foreign_link::getByForeignID($friend_id, TWITTER_SERVICE);
|
2009-08-06 08:03:05 +01:00
|
|
|
|
|
|
|
// Get associated user and subscribe her
|
2015-07-18 10:39:34 +01:00
|
|
|
$friend_profile = $friend_flink->getProfile();
|
|
|
|
|
|
|
|
Subscription::start($profile, $friend_profile);
|
2019-11-01 12:27:36 +00:00
|
|
|
common_log(
|
|
|
|
LOG_INFO,
|
|
|
|
$this->name() . ' - Subscribed ' .
|
|
|
|
"{$friend_profile->nickname} to {$profile->nickname}."
|
|
|
|
);
|
2015-07-18 10:39:34 +01:00
|
|
|
} catch (NoResultException $e) {
|
|
|
|
// either no foreign link for this friend's foreign ID or no profile found on local ID.
|
|
|
|
} catch (Exception $e) {
|
|
|
|
common_debug($this->name() .
|
|
|
|
' - Tried and failed subscribing ' .
|
|
|
|
"{$friend_profile->nickname} to {$profile->nickname} - " .
|
|
|
|
$e->getMessage());
|
2009-08-06 08:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-08 02:22:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-06 08:03:05 +01:00
|
|
|
$id = null;
|
|
|
|
$debug = null;
|
|
|
|
|
|
|
|
if (have_option('i')) {
|
|
|
|
$id = get_option_value('i');
|
2019-11-01 12:27:36 +00:00
|
|
|
} elseif (have_option('--id')) {
|
2009-08-06 08:03:05 +01:00
|
|
|
$id = get_option_value('--id');
|
2019-11-01 12:27:36 +00:00
|
|
|
} elseif (count($args) > 0) {
|
2009-08-06 08:03:05 +01:00
|
|
|
$id = $args[0];
|
|
|
|
} else {
|
|
|
|
$id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_option('d') || have_option('debug')) {
|
|
|
|
$debug = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);
|
|
|
|
$syncer->runOnce();
|