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';
37 * Daemon to sync local friends with Twitter friends
41 * @author Zach Copley <zach@status.net>
42 * @author Evan Prodromou <evan@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
47 $helptext = <<<END_OF_TWITTER_HELP
48 Batch script for synching local friends with Twitter friends.
52 require_once INSTALLDIR . '/scripts/commandline.inc';
53 require_once INSTALLDIR . '/lib/parallelizingdaemon.php';
55 class SyncTwitterFriendsDaemon extends ParallelizingDaemon
60 * @param string $id the name/id of this daemon
61 * @param int $interval sleep this long before doing everything again
62 * @param int $max_children maximum number of child processes at a time
63 * @param boolean $debug debug output flag
69 function __construct($id = null, $interval = 60,
70 $max_children = 2, $debug = null)
72 parent::__construct($id, $interval, $max_children, $debug);
78 * @return string Name of the daemon.
83 return ('synctwitterfriends.' . $this->_id);
87 * Find all the Twitter foreign links for users who have requested
88 * automatically subscribing to their Twitter friends locally.
90 * @return array flinks an array of Foreign_link objects
95 $flink = new Foreign_link();
97 $conn = &$flink->getDatabaseConnection();
99 $flink->service = TWITTER_SERVICE;
100 $flink->orderBy('last_friendsync');
101 $flink->limit(25); // sync this many users during this run
104 while ($flink->fetch()) {
105 if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
106 $flinks[] = clone($flink);
112 global $_DB_DATAOBJECT;
113 unset($_DB_DATAOBJECT['CONNECTIONS']);
118 function childTask($flink) {
120 // Each child ps needs its own DB connection
122 // Note: DataObject::getDatabaseConnection() creates
123 // a new connection if there isn't one already
125 $conn = &$flink->getDatabaseConnection();
127 $this->subscribeTwitterFriends($flink);
129 $flink->last_friendsync = common_sql_now();
134 // XXX: Couldn't find a less brutal way to blow
135 // away a cached connection
137 global $_DB_DATAOBJECT;
138 unset($_DB_DATAOBJECT['CONNECTIONS']);
141 function fetchTwitterFriends($flink)
147 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
148 $token = TwitterOAuthClient::unpackToken($flink->credentials);
149 $client = new TwitterOAuthClient($token->key, $token->secret);
150 common_debug($this->name() . '- Grabbing friends IDs with OAuth.');
152 $client = new TwitterBasicAuthClient($flink);
153 common_debug($this->name() . '- Grabbing friends IDs with basic auth.');
157 $friends_ids = $client->friendsIds();
158 } catch (Exception $e) {
159 common_log(LOG_WARNING, $this->name() .
160 ' - cURL error getting friend ids ' .
161 $e->getCode() . ' - ' . $e->getMessage());
165 if (empty($friends_ids)) {
166 common_debug($this->name() .
167 " - Twitter user $flink->foreign_id " .
168 'doesn\'t have any friends!');
172 common_debug($this->name() . ' - Twitter\'s API says Twitter user id ' .
173 "$flink->foreign_id has " .
174 count($friends_ids) . ' friends.');
176 // Calculate how many pages to get...
177 $pages = ceil(count($friends_ids) / 100);
180 common_debug($this->name() . " - $user seems to have no friends.");
183 for ($i = 1; $i <= $pages; $i++) {
186 $more_friends = $client->statusesFriends(null, null, null, $i);
187 } catch (Exception $e) {
188 common_log(LOG_WARNING, $this->name() .
189 ' - cURL error getting Twitter statuses/friends ' .
190 "page $i - " . $e->getCode() . ' - ' .
194 if (empty($more_friends)) {
195 common_log(LOG_WARNING, $this->name() .
196 " - Couldn't retrieve page $i " .
197 "of Twitter user $flink->foreign_id friends.");
200 $friends = array_merge($friends, $more_friends);
207 function subscribeTwitterFriends($flink)
209 $friends = $this->fetchTwitterFriends($flink);
211 if (empty($friends)) {
212 common_debug($this->name() .
213 ' - Couldn\'t get friends from Twitter for ' .
214 "Twitter user $flink->foreign_id.");
218 $user = $flink->getUser();
220 foreach ($friends as $friend) {
222 $friend_name = $friend->screen_name;
223 $friend_id = (int) $friend->id;
225 // Update or create the Foreign_user record for each
228 if (!save_twitter_user($friend_id, $friend_name)) {
229 common_log(LOG_WARNING, $this-name() .
230 " - Couldn't save $screen_name's friend, $friend_name.");
234 // Check to see if there's a related local user
236 $friend_flink = Foreign_link::getByForeignID($friend_id,
239 if (!empty($friend_flink)) {
241 // Get associated user and subscribe her
243 $friend_user = User::staticGet('id', $friend_flink->user_id);
245 if (!empty($friend_user)) {
246 $result = subs_subscribe_to($user, $friend_user);
248 if ($result === true) {
250 $this->name() . ' - Subscribed ' .
251 "$friend_user->nickname to $user->nickname.");
253 common_debug($this->name() .
254 ' - Tried subscribing ' .
255 "$friend_user->nickname to $user->nickname - " .
270 if (have_option('i')) {
271 $id = get_option_value('i');
272 } else if (have_option('--id')) {
273 $id = get_option_value('--id');
274 } else if (count($args) > 0) {
280 if (have_option('d') || have_option('debug')) {
284 $syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);