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 // Tune number of processes and how often to poll Twitter
24 // XXX: Should these things be in config.php?
25 define('MAXCHILDREN', 2);
26 define('POLL_INTERVAL', 60); // in seconds
28 $shortoptions = 'di::';
29 $longoptions = array('id::', 'debug');
31 $helptext = <<<END_OF_TRIM_HELP
32 Batch script for retrieving Twitter messages from foreign service.
34 -i --id Identity (default 'generic')
35 -d --debug Debug (lots of log output)
39 require_once INSTALLDIR . '/scripts/commandline.inc';
40 require_once INSTALLDIR . '/lib/common.php';
41 require_once INSTALLDIR . '/lib/daemon.php';
42 require_once INSTALLDIR . '/plugins/TwitterBridge/twitter.php';
43 require_once INSTALLDIR . '/plugins/TwitterBridge/twitterbasicauthclient.php';
44 require_once INSTALLDIR . '/plugins/TwitterBridge/twitteroauthclient.php';
47 * Fetcher for statuses from Twitter
49 * Fetches statuses from Twitter and inserts them as notices in local
54 * @author Zach Copley <zach@status.net>
55 * @author Evan Prodromou <evan@status.net>
56 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57 * @link http://status.net/
60 // NOTE: an Avatar path MUST be set in config.php for this
61 // script to work: e.g.: $config['avatar']['path'] = '/statusnet/avatar';
63 class TwitterStatusFetcher extends ParallelizingDaemon
68 * @param string $id the name/id of this daemon
69 * @param int $interval sleep this long before doing everything again
70 * @param int $max_children maximum number of child processes at a time
71 * @param boolean $debug debug output flag
76 function __construct($id = null, $interval = 60,
77 $max_children = 2, $debug = null)
79 parent::__construct($id, $interval, $max_children, $debug);
85 * @return string Name of the daemon.
90 return ('twitterstatusfetcher.'.$this->_id);
94 * Find all the Twitter foreign links for users who have requested
95 * importing of their friends' timelines
97 * @return array flinks an array of Foreign_link objects
100 function getObjects()
102 global $_DB_DATAOBJECT;
104 $flink = new Foreign_link();
105 $conn = &$flink->getDatabaseConnection();
107 $flink->service = TWITTER_SERVICE;
108 $flink->orderBy('last_noticesync');
113 while ($flink->fetch()) {
115 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
116 FOREIGN_NOTICE_RECV) {
117 $flinks[] = clone($flink);
125 unset($_DB_DATAOBJECT['CONNECTIONS']);
130 function childTask($flink) {
132 // Each child ps needs its own DB connection
134 // Note: DataObject::getDatabaseConnection() creates
135 // a new connection if there isn't one already
137 $conn = &$flink->getDatabaseConnection();
139 $this->getTimeline($flink);
141 $flink->last_friendsync = common_sql_now();
146 // XXX: Couldn't find a less brutal way to blow
147 // away a cached connection
149 global $_DB_DATAOBJECT;
150 unset($_DB_DATAOBJECT['CONNECTIONS']);
153 function getTimeline($flink)
156 common_log(LOG_WARNING, $this->name() .
157 " - Can't retrieve Foreign_link for foreign ID $fid");
161 common_debug($this->name() . ' - Trying to get timeline for Twitter user ' .
164 // XXX: Biggest remaining issue - How do we know at which status
165 // to start importing? How many statuses? Right now I'm going
166 // with the default last 20.
170 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
171 $token = TwitterOAuthClient::unpackToken($flink->credentials);
172 $client = new TwitterOAuthClient($token->key, $token->secret);
173 common_debug($this->name() . ' - Grabbing friends timeline with OAuth.');
175 $client = new TwitterBasicAuthClient($flink);
176 common_debug($this->name() . ' - Grabbing friends timeline with basic auth.');
182 $timeline = $client->statusesFriendsTimeline();
183 } catch (Exception $e) {
184 common_log(LOG_WARNING, $this->name() .
185 ' - Twitter client unable to get friends timeline for user ' .
186 $flink->user_id . ' - code: ' .
187 $e->getCode() . 'msg: ' . $e->getMessage());
190 if (empty($timeline)) {
191 common_log(LOG_WARNING, $this->name() . " - Empty timeline.");
195 // Reverse to preserve order
197 foreach (array_reverse($timeline) as $status) {
199 // Hacktastic: filter out stuff coming from this StatusNet
201 $source = mb_strtolower(common_config('integration', 'source'));
203 if (preg_match("/$source/", mb_strtolower($status->source))) {
204 common_debug($this->name() . ' - Skipping import of status ' .
205 $status->id . ' with source ' . $source);
209 $this->saveStatus($status, $flink);
212 // Okay, record the time we synced with Twitter for posterity
214 $flink->last_noticesync = common_sql_now();
218 function saveStatus($status, $flink)
220 $id = $this->ensureProfile($status->user);
222 $profile = Profile::staticGet($id);
224 if (empty($profile)) {
225 common_log(LOG_ERR, $this->name() .
226 ' - Problem saving notice. No associated Profile.');
230 // XXX: change of screen name?
232 $uri = 'http://twitter.com/' . $status->user->screen_name .
233 '/status/' . $status->id;
235 $notice = Notice::staticGet('uri', $uri);
237 // check to see if we've already imported the status
239 if (empty($notice)) {
241 $notice = new Notice();
243 $notice->profile_id = $id;
245 $notice->created = strftime('%Y-%m-%d %H:%M:%S',
246 strtotime($status->created_at));
247 $notice->content = common_shorten_links($status->text); // XXX
248 $notice->rendered = common_render_content($notice->content, $notice);
249 $notice->source = 'twitter';
250 $notice->reply_to = null; // XXX: lookup reply
251 $notice->is_local = Notice::GATEWAY;
253 if (Event::handle('StartNoticeSave', array(&$notice))) {
254 $id = $notice->insert();
255 Event::handle('EndNoticeSave', array($notice));
259 if (!Notice_inbox::pkeyGet(array('notice_id' => $notice->id,
260 'user_id' => $flink->user_id))) {
262 $inbox = new Notice_inbox();
264 $inbox->user_id = $flink->user_id;
265 $inbox->notice_id = $notice->id;
266 $inbox->created = $notice->created;
267 $inbox->source = NOTICE_INBOX_SOURCE_GATEWAY; // From a private source
273 function ensureProfile($user)
275 // check to see if there's already a profile for this user
277 $profileurl = 'http://twitter.com/' . $user->screen_name;
278 $profile = Profile::staticGet('profileurl', $profileurl);
280 if (!empty($profile)) {
281 common_debug($this->name() .
282 " - Profile for $profile->nickname found.");
284 // Check to see if the user's Avatar has changed
286 $this->checkAvatar($user, $profile);
290 common_debug($this->name() . ' - Adding profile and remote profile ' .
291 "for Twitter user: $profileurl.");
293 $profile = new Profile();
294 $profile->query("BEGIN");
296 $profile->nickname = $user->screen_name;
297 $profile->fullname = $user->name;
298 $profile->homepage = $user->url;
299 $profile->bio = $user->description;
300 $profile->location = $user->location;
301 $profile->profileurl = $profileurl;
302 $profile->created = common_sql_now();
304 $id = $profile->insert();
307 common_log_db_error($profile, 'INSERT', __FILE__);
308 $profile->query("ROLLBACK");
312 // check for remote profile
314 $remote_pro = Remote_profile::staticGet('uri', $profileurl);
316 if (empty($remote_pro)) {
318 $remote_pro = new Remote_profile();
320 $remote_pro->id = $id;
321 $remote_pro->uri = $profileurl;
322 $remote_pro->created = common_sql_now();
324 $rid = $remote_pro->insert();
327 common_log_db_error($profile, 'INSERT', __FILE__);
328 $profile->query("ROLLBACK");
333 $profile->query("COMMIT");
335 $this->saveAvatars($user, $id);
341 function checkAvatar($twitter_user, $profile)
345 $path_parts = pathinfo($twitter_user->profile_image_url);
347 $newname = 'Twitter_' . $twitter_user->id . '_' .
348 $path_parts['basename'];
350 $oldname = $profile->getAvatar(48)->filename;
352 if ($newname != $oldname) {
353 common_debug($this->name() . ' - Avatar for Twitter user ' .
354 "$profile->nickname has changed.");
355 common_debug($this->name() . " - old: $oldname new: $newname");
357 $this->updateAvatars($twitter_user, $profile);
360 if ($this->missingAvatarFile($profile)) {
361 common_debug($this->name() . ' - Twitter user ' .
363 ' is missing one or more local avatars.');
364 common_debug($this->name() ." - old: $oldname new: $newname");
366 $this->updateAvatars($twitter_user, $profile);
371 function updateAvatars($twitter_user, $profile) {
375 $path_parts = pathinfo($twitter_user->profile_image_url);
377 $img_root = substr($path_parts['basename'], 0, -11);
378 $ext = $path_parts['extension'];
379 $mediatype = $this->getMediatype($ext);
381 foreach (array('mini', 'normal', 'bigger') as $size) {
382 $url = $path_parts['dirname'] . '/' .
383 $img_root . '_' . $size . ".$ext";
384 $filename = 'Twitter_' . $twitter_user->id . '_' .
385 $img_root . "_$size.$ext";
387 $this->updateAvatar($profile->id, $size, $mediatype, $filename);
388 $this->fetchAvatar($url, $filename);
392 function missingAvatarFile($profile) {
394 foreach (array(24, 48, 73) as $size) {
396 $filename = $profile->getAvatar($size)->filename;
397 $avatarpath = Avatar::path($filename);
399 if (file_exists($avatarpath) == FALSE) {
407 function getMediatype($ext)
411 switch (strtolower($ext)) {
413 $mediatype = 'image/jpg';
416 $mediatype = 'image/gif';
419 $mediatype = 'image/png';
425 function saveAvatars($user, $id)
429 $path_parts = pathinfo($user->profile_image_url);
430 $ext = $path_parts['extension'];
431 $end = strlen('_normal' . $ext);
432 $img_root = substr($path_parts['basename'], 0, -($end+1));
433 $mediatype = $this->getMediatype($ext);
435 foreach (array('mini', 'normal', 'bigger') as $size) {
436 $url = $path_parts['dirname'] . '/' .
437 $img_root . '_' . $size . ".$ext";
438 $filename = 'Twitter_' . $user->id . '_' .
439 $img_root . "_$size.$ext";
441 if ($this->fetchAvatar($url, $filename)) {
442 $this->newAvatar($id, $size, $mediatype, $filename);
444 common_log(LOG_WARNING, $this->id() .
445 " - Problem fetching Avatar: $url");
450 function updateAvatar($profile_id, $size, $mediatype, $filename) {
452 common_debug($this->name() . " - Updating avatar: $size");
454 $profile = Profile::staticGet($profile_id);
456 if (empty($profile)) {
457 common_debug($this->name() . " - Couldn't get profile: $profile_id!");
461 $sizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73);
462 $avatar = $profile->getAvatar($sizes[$size]);
464 // Delete the avatar, if present
470 $this->newAvatar($profile->id, $size, $mediatype, $filename);
473 function newAvatar($profile_id, $size, $mediatype, $filename)
477 $avatar = new Avatar();
478 $avatar->profile_id = $profile_id;
483 $avatar->height = 24;
487 $avatar->height = 48;
491 // Note: Twitter's big avatars are a different size than
492 // StatusNet's (StatusNet's = 96)
495 $avatar->height = 73;
498 $avatar->original = 0; // we don't have the original
499 $avatar->mediatype = $mediatype;
500 $avatar->filename = $filename;
501 $avatar->url = Avatar::url($filename);
503 $avatar->created = common_sql_now();
505 $id = $avatar->insert();
508 common_log_db_error($avatar, 'INSERT', __FILE__);
512 common_debug($this->name() .
513 " - Saved new $size avatar for $profile_id.");
518 function fetchAvatar($url, $filename)
520 $avatarfile = Avatar::path($filename);
522 $out = fopen($avatarfile, 'wb');
524 common_log(LOG_WARNING, $this->name() .
525 " - Couldn't open file $filename");
529 common_debug($this->name() . " - Fetching Twitter avatar: $url");
532 curl_setopt($ch, CURLOPT_URL, $url);
533 curl_setopt($ch, CURLOPT_FILE, $out);
534 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
535 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
536 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
537 $result = curl_exec($ch);
549 if (have_option('i')) {
550 $id = get_option_value('i');
551 } else if (have_option('--id')) {
552 $id = get_option_value('--id');
553 } else if (count($args) > 0) {
559 if (have_option('d') || have_option('debug')) {
563 $fetcher = new TwitterStatusFetcher($id, 60, 2, $debug);