123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
- define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
- $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 dirname(__DIR__) . '/twitter.php';
- class SyncTwitterFriendsDaemon extends ParallelizingDaemon
- {
-
- public function __construct(
- $id = null,
- $interval = 60,
- $max_children = 2,
- $debug = null
- ) {
- parent::__construct($id, $interval, $max_children, $debug);
- }
-
- public function name()
- {
- return ('synctwitterfriends.' . $this->_id);
- }
-
- public function getObjects()
- {
- $flinks = array();
- $flink = new Foreign_link();
- $conn = $flink->getDatabaseConnection();
- $flink->service = TWITTER_SERVICE;
- $flink->orderBy('last_friendsync');
- $flink->limit(25);
- $flink->find();
- while ($flink->fetch()) {
- if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
- $flinks[] = clone($flink);
- }
- }
- $conn->disconnect();
- global $_DB_DATAOBJECT;
- foreach ($_DB_DATAOBJECT['CONNECTIONS'] as $k => $v) {
- if ($v === $conn) {
- unset($_DB_DATAOBJECT['CONNECTIONS'][$k]);
- }
- }
- return $flinks;
- }
-
- public function childTask($flink)
- {
-
-
-
- $conn = $flink->getDatabaseConnection();
- $this->subscribeTwitterFriends($flink);
- $flink->last_friendsync = common_sql_now();
- $flink->update();
- $conn->disconnect();
-
-
- global $_DB_DATAOBJECT;
- foreach ($_DB_DATAOBJECT['CONNECTIONS'] as $k => $v) {
- if ($v === $conn) {
- unset($_DB_DATAOBJECT['CONNECTIONS'][$k]);
- }
- }
- }
- public function fetchTwitterFriends(Foreign_link $flink)
- {
- $friends = array();
- $client = null;
- 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 {
- common_debug("Skipping Twitter friends for {$flink->user_id} since not OAuth.");
- return $friends;
- }
- try {
- $friends_ids = $client->friendsIds();
- } catch (Exception $e) {
- common_log(LOG_WARNING, $this->name() .
- ' - error getting friend ids: ' .
- $e->getMessage());
- 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.');
-
- $pages = ceil(count($friends_ids) / 100);
- if ($pages == 0) {
- common_debug($this->name() . " - $user seems to have no friends.");
- }
- for ($i = 1; $i <= $pages; $i++) {
- 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()
- );
- }
- if (empty($more_friends)) {
- common_log(LOG_WARNING, $this->name() .
- " - Couldn't retrieve page $i " .
- "of Twitter user $flink->foreign_id friends.");
- continue;
- } else {
- if (is_array($more_friends)) {
- $friends = array_merge($friends, $more_friends);
- }
- }
- }
- return $friends;
- }
- public function subscribeTwitterFriends(Foreign_link $flink)
- {
- try {
- $profile = $flink->getProfile();
- } catch (NoResultException $e) {
- common_log(LOG_WARNING, 'Foreign_link has no matching local profile for local ID: '.$flink->user_id);
- }
- $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;
-
-
- if (!save_twitter_user($friend_id, $friend_name)) {
- common_log(LOG_WARNING, $this->name() .
- " - Couldn't save $screen_name's friend, $friend_name.");
- continue;
- }
-
- try {
- $friend_flink = Foreign_link::getByForeignID($friend_id, TWITTER_SERVICE);
-
- $friend_profile = $friend_flink->getProfile();
- Subscription::start($profile, $friend_profile);
- common_log(
- LOG_INFO,
- $this->name() . ' - Subscribed ' .
- "{$friend_profile->nickname} to {$profile->nickname}."
- );
- } catch (NoResultException $e) {
-
- } catch (Exception $e) {
- common_debug($this->name() .
- ' - Tried and failed subscribing ' .
- "{$friend_profile->nickname} to {$profile->nickname} - " .
- $e->getMessage());
- }
- }
- return true;
- }
- }
- $id = null;
- $debug = null;
- if (have_option('i')) {
- $id = get_option_value('i');
- } elseif (have_option('--id')) {
- $id = get_option_value('--id');
- } elseif (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();
|