4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
23 $shortoptions = 'di::';
24 $longoptions = array('id::', 'debug');
26 $helptext = <<<END_OF_TRIM_HELP
27 Batch script for synching local friends with Twitter friends.
28 -i --id Identity (default 'generic')
29 -d --debug Debug (lots of log output)
33 require_once INSTALLDIR . '/scripts/commandline.inc';
34 require_once INSTALLDIR . '/lib/parallelizingdaemon.php';
35 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
36 require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
37 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
40 * Daemon to sync local friends with Twitter friends
44 * @author Zach Copley <zach@status.net>
45 * @author Evan Prodromou <evan@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
50 class SyncTwitterFriendsDaemon extends ParallelizingDaemon
55 * @param string $id the name/id of this daemon
56 * @param int $interval sleep this long before doing everything again
57 * @param int $max_children maximum number of child processes at a time
58 * @param boolean $debug debug output flag
64 function __construct($id = null, $interval = 60,
65 $max_children = 2, $debug = null)
67 parent::__construct($id, $interval, $max_children, $debug);
73 * @return string Name of the daemon.
78 return ('synctwitterfriends.' . $this->_id);
82 * Find all the Twitter foreign links for users who have requested
83 * automatically subscribing to their Twitter friends locally.
85 * @return array flinks an array of Foreign_link objects
90 $flink = new Foreign_link();
92 $conn = &$flink->getDatabaseConnection();
94 $flink->service = TWITTER_SERVICE;
95 $flink->orderBy('last_friendsync');
96 $flink->limit(25); // sync this many users during this run
99 while ($flink->fetch()) {
100 if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
101 $flinks[] = clone($flink);
107 global $_DB_DATAOBJECT;
108 unset($_DB_DATAOBJECT['CONNECTIONS']);
113 function childTask($flink) {
115 // Each child ps needs its own DB connection
117 // Note: DataObject::getDatabaseConnection() creates
118 // a new connection if there isn't one already
120 $conn = &$flink->getDatabaseConnection();
122 $this->subscribeTwitterFriends($flink);
124 $flink->last_friendsync = common_sql_now();
129 // XXX: Couldn't find a less brutal way to blow
130 // away a cached connection
132 global $_DB_DATAOBJECT;
133 unset($_DB_DATAOBJECT['CONNECTIONS']);
136 function fetchTwitterFriends($flink)
142 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
143 $token = TwitterOAuthClient::unpackToken($flink->credentials);
144 $client = new TwitterOAuthClient($token->key, $token->secret);
145 common_debug($this->name() . '- Grabbing friends IDs with OAuth.');
147 $client = new TwitterBasicAuthClient($flink);
148 common_debug($this->name() . '- Grabbing friends IDs with basic auth.');
152 $friends_ids = $client->friendsIds();
153 } catch (Exception $e) {
154 common_log(LOG_WARNING, $this->name() .
155 ' - error getting friend ids: ' .
160 if (empty($friends_ids)) {
161 common_debug($this->name() .
162 " - Twitter user $flink->foreign_id " .
163 'doesn\'t have any friends!');
167 common_debug($this->name() . ' - Twitter\'s API says Twitter user id ' .
168 "$flink->foreign_id has " .
169 count($friends_ids) . ' friends.');
171 // Calculate how many pages to get...
172 $pages = ceil(count($friends_ids) / 100);
175 common_debug($this->name() . " - $user seems to have no friends.");
178 for ($i = 1; $i <= $pages; $i++) {
181 $more_friends = $client->statusesFriends(null, null, null, $i);
182 } catch (Exception $e) {
183 common_log(LOG_WARNING, $this->name() .
184 ' - cURL error getting Twitter statuses/friends ' .
185 "page $i - " . $e->getCode() . ' - ' .
189 if (empty($more_friends)) {
190 common_log(LOG_WARNING, $this->name() .
191 " - Couldn't retrieve page $i " .
192 "of Twitter user $flink->foreign_id friends.");
195 $friends = array_merge($friends, $more_friends);
202 function subscribeTwitterFriends($flink)
204 $friends = $this->fetchTwitterFriends($flink);
206 if (empty($friends)) {
207 common_debug($this->name() .
208 ' - Couldn\'t get friends from Twitter for ' .
209 "Twitter user $flink->foreign_id.");
213 $user = $flink->getUser();
215 foreach ($friends as $friend) {
217 $friend_name = $friend->screen_name;
218 $friend_id = (int) $friend->id;
220 // Update or create the Foreign_user record for each
223 if (!save_twitter_user($friend_id, $friend_name)) {
224 common_log(LOG_WARNING, $this->name() .
225 " - Couldn't save $screen_name's friend, $friend_name.");
229 // Check to see if there's a related local user
231 $friend_flink = Foreign_link::getByForeignID($friend_id,
234 if (!empty($friend_flink)) {
236 // Get associated user and subscribe her
238 $friend_user = User::staticGet('id', $friend_flink->user_id);
240 if (!empty($friend_user)) {
241 $result = subs_subscribe_to($user, $friend_user);
243 if ($result === true) {
245 $this->name() . ' - Subscribed ' .
246 "$friend_user->nickname to $user->nickname.");
248 common_debug($this->name() .
249 ' - Tried subscribing ' .
250 "$friend_user->nickname to $user->nickname - " .
265 if (have_option('i')) {
266 $id = get_option_value('i');
267 } else if (have_option('--id')) {
268 $id = get_option_value('--id');
269 } else if (count($args) > 0) {
275 if (have_option('d') || have_option('debug')) {
279 $syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);