4 * Laconica - a distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, Control Yourself, 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@controlyourself.ca>
42 * @author Evan Prodromou <evan@controlyourself.ca>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://laconi.ca/
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
57 function __construct($id = null, $interval = 60,
58 $max_children = 2, $debug = null)
60 parent::__construct($id, $interval, $max_children, $debug);
66 * @return string Name of the daemon.
71 return ('synctwitterfriendsdaemon.' . $this->_id);
77 $flink = new Foreign_link();
79 $conn = &$flink->getDatabaseConnection();
81 $flink->service = TWITTER_SERVICE;
82 $flink->orderBy('last_friendsync');
83 $flink->limit(25); // sync this many users during this run
86 while ($flink->fetch()) {
87 if (($flink->friendsync & FOREIGN_FRIEND_RECV) == FOREIGN_FRIEND_RECV) {
88 $flinks[] = clone($flink);
94 global $_DB_DATAOBJECT;
95 unset($_DB_DATAOBJECT['CONNECTIONS']);
100 function childTask($flink) {
102 // Each child ps needs its own DB connection
104 // Note: DataObject::getDatabaseConnection() creates
105 // a new connection if there isn't one already
107 $conn = &$flink->getDatabaseConnection();
109 $this->subscribeTwitterFriends($flink);
111 $flink->last_friendsync = common_sql_now();
116 // XXX: Couldn't find a less brutal way to blow
117 // away a cached connection
119 global $_DB_DATAOBJECT;
120 unset($_DB_DATAOBJECT['CONNECTIONS']);
123 function fetchTwitterFriends($flink)
127 $client = new TwitterOAuthClient($flink->token, $flink->credentials);
130 $friends_ids = $client->friends_ids();
131 } catch (OAuthCurlException $e) {
132 common_log(LOG_WARNING, $this->name() .
133 ' - cURL error getting friend ids ' .
134 $e->getCode() . ' - ' . $e->getMessage());
138 if (empty($friends_ids)) {
139 common_debug($this->name() .
140 " - Twitter user $flink->foreign_id " .
141 'doesn\'t have any friends!');
145 common_debug($this->name() . ' - Twitter\'s API says Twitter user id ' .
146 "$flink->foreign_id has " .
147 count($friends_ids) . ' friends.');
149 // Calculate how many pages to get...
150 $pages = ceil(count($friends_ids) / 100);
153 common_debug($this->name() . " - $user seems to have no friends.");
156 for ($i = 1; $i <= $pages; $i++) {
159 $more_friends = $client->statuses_friends(null, null, null, $i);
160 } catch (OAuthCurlException $e) {
161 common_log(LOG_WARNING, $this->name() .
162 ' - cURL error getting Twitter statuses/friends ' .
163 "page $i - " . $e->getCode() . ' - ' .
167 if (empty($more_friends)) {
168 common_log(LOG_WARNING, $this->name() .
169 " - Couldn't retrieve page $i " .
170 "of Twitter user $flink->foreign_id friends.");
173 $friends = array_merge($friends, $more_friends);
180 function subscribeTwitterFriends($flink)
182 $friends = $this->fetchTwitterFriends($flink);
184 if (empty($friends)) {
185 common_debug($this->name() .
186 ' - Couldn\'t get friends from Twitter for ' .
187 "Twitter user $flink->foreign_id.");
191 $user = $flink->getUser();
193 foreach ($friends as $friend) {
195 $friend_name = $friend->screen_name;
196 $friend_id = (int) $friend->id;
198 // Update or create the Foreign_user record for each
201 if (!save_twitter_user($friend_id, $friend_name)) {
202 common_log(LOG_WARNING, $this-name() .
203 " - Couldn't save $screen_name's friend, $friend_name.");
207 // Check to see if there's a related local user
209 $friend_flink = Foreign_link::getByForeignID($friend_id,
212 if (!empty($friend_flink)) {
214 // Get associated user and subscribe her
216 $friend_user = User::staticGet('id', $friend_flink->user_id);
218 if (!empty($friend_user)) {
219 $result = subs_subscribe_to($user, $friend_user);
221 if ($result === true) {
223 $this->name() . ' - Subscribed ' .
224 "$friend_user->nickname to $user->nickname.");
226 common_debug($this->name() .
227 ' - Tried subscribing ' .
228 "$friend_user->nickname to $user->nickname - " .
245 if (have_option('i')) {
246 $id = get_option_value('i');
247 } else if (have_option('--id')) {
248 $id = get_option_value('--id');
249 } else if (count($args) > 0) {
255 if (have_option('d') || have_option('debug')) {
259 $syncer = new SyncTwitterFriendsDaemon($id, 60, 2, $debug);