4 * Laconica - a distributed open-source microblogging tool
5 * Copyright (C) 2008, Controlez-Vous, 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 // Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23 print "This script must be run from the command line\n";
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28 define('LACONICA', true);
30 // Tune number of processes and how often to poll Twitter
31 // XXX: Should these things be in config.php?
32 define('MAXCHILDREN', 2);
33 define('POLL_INTERVAL', 60); // in seconds
35 // Uncomment this to get useful logging
36 define('SCRIPT_DEBUG', true);
38 require_once INSTALLDIR . '/lib/common.php';
39 require_once INSTALLDIR . '/lib/daemon.php';
42 * Fetcher for statuses from Twitter
44 * Fetches statuses from Twitter and inserts them as notices in local
49 * @author Zach Copley <zach@controlyourself.ca>
50 * @author Evan Prodromou <evan@controlyourself.ca>
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://laconi.ca/
55 class TwitterStatusFetcher extends Daemon
57 private $_children = array();
62 * @return string Name of the daemon.
67 return ('twitterstatusfetcher.generic');
80 $flinks = $this->refreshFlinks();
82 foreach ($flinks as $f) {
84 // We have to disconnect from the DB before forking so
85 // each sub-process will open its own connection and
86 // avoid stomping on the others
88 $conn = &$f->getDatabaseConnection();
94 die ("Couldn't fork!");
100 if (defined('SCRIPT_DEBUG')) {
101 common_debug("Parent: forked new status ".
102 " fetcher process " . $pid);
105 $this->_children[] = $pid;
110 $this->getTimeline($f);
114 // Remove child from ps list as it finishes
115 while (($c = pcntl_wait($status, WNOHANG OR WUNTRACED)) > 0) {
117 if (defined('SCRIPT_DEBUG')) {
118 common_debug("Child $c finished.");
121 $this->removePs($this->_children, $c);
124 // Wait! We have too many damn kids.
125 if (sizeof($this->_children) > MAXCHILDREN) {
127 if (defined('SCRIPT_DEBUG')) {
128 common_debug('Too many children. Waiting...');
131 if (($c = pcntl_wait($status, WUNTRACED)) > 0) {
133 if (defined('SCRIPT_DEBUG')) {
134 common_debug("Finished waiting for $c");
137 $this->removePs($this->_children, $c);
142 // Remove all children from the process list before restarting
143 while (($c = pcntl_wait($status, WUNTRACED)) > 0) {
145 if (defined('SCRIPT_DEBUG')) {
146 common_debug("Child $c finished.");
149 $this->removePs($this->_children, $c);
152 // Rest for a bit before we fetch more statuses
154 if (defined('SCRIPT_DEBUG')) {
155 common_debug('Waiting ' . POLL_INTERVAL .
156 ' secs before hitting Twitter again.');
159 if (POLL_INTERVAL > 0) {
160 sleep(POLL_INTERVAL);
167 * Refresh the foreign links for this user
172 function refreshFlinks()
174 $flink = new Foreign_link();
176 $flink->service = 1; // Twitter
178 $flink->orderBy('last_noticesync');
180 $cnt = $flink->find();
182 if (defined('SCRIPT_DEBUG')) {
183 common_debug('Updating Twitter friends subscriptions' .
189 while ($flink->fetch()) {
191 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
192 FOREIGN_NOTICE_RECV) {
193 $flinks[] = clone($flink);
206 * @param array &$plist unknown.
207 * @param string $ps unknown.
213 function removePs(&$plist, $ps)
215 for ($i = 0; $i < sizeof($plist); $i++) {
216 if ($plist[$i] == $ps) {
218 $plist = array_values($plist);
224 function getTimeline($flink)
227 common_log(LOG_WARNING,
228 "Can't retrieve Foreign_link for foreign ID $fid");
232 $fuser = $flink->getForeignUser();
235 common_log(LOG_WARNING, "Unmatched user for ID " .
240 if (defined('SCRIPT_DEBUG')) {
241 common_debug('Trying to get timeline for Twitter user ' .
242 "$fuser->nickname ($flink->foreign_id).");
245 // XXX: Biggest remaining issue - How do we know at which status
246 // to start importing? How many statuses? Right now I'm going
247 // with the default last 20.
249 $url = 'http://twitter.com/statuses/friends_timeline.json';
251 $timeline_json = get_twitter_data($url, $fuser->nickname,
252 $flink->credentials);
254 $timeline = json_decode($timeline_json);
256 if (empty($timeline)) {
257 common_log(LOG_WARNING, "Empty timeline.");
261 // Reverse to preserve order
262 foreach (array_reverse($timeline) as $status) {
264 // Hacktastic: filter out stuff coming from this Laconica
265 $source = mb_strtolower(common_config('integration', 'source'));
267 if (preg_match("/$source/", mb_strtolower($status->source))) {
268 if (defined('SCRIPT_DEBUG')) {
269 common_debug('Skipping import of status ' . $status->id .
270 ' with source ' . $source);
275 $this->saveStatus($status, $flink);
278 // Okay, record the time we synced with Twitter for posterity
279 $flink->last_noticesync = common_sql_now();
283 function saveStatus($status, $flink)
285 $id = $this->ensureProfile($status->user);
286 $profile = Profile::staticGet($id);
290 'Problem saving notice. No associated Profile.');
294 // XXX: change of screen name?
296 $uri = 'http://twitter.com/' . $status->user->screen_name .
297 '/status/' . $status->id;
299 $notice = Notice::staticGet('uri', $uri);
301 // check to see if we've already imported the status
305 $notice = new Notice();
307 $notice->profile_id = $id;
309 $notice->created = strftime('%Y-%m-%d %H:%M:%S',
310 strtotime($status->created_at));
311 $notice->content = common_shorten_links($status->text); // XXX
312 $notice->rendered = common_render_content($notice->content, $notice);
313 $notice->source = 'twitter';
314 $notice->reply_to = null; // XXX lookup reply
315 $notice->is_local = NOTICE_GATEWAY;
317 if (Event::handle('StartNoticeSave', array(&$notice))) {
318 $id = $notice->insert();
319 Event::handle('EndNoticeSave', array($notice));
323 if (!Notice_inbox::pkeyGet(array('notice_id' => $notice->id,
324 'user_id' => $flink->user_id))) {
326 $inbox = new Notice_inbox();
328 $inbox->user_id = $flink->user_id;
329 $inbox->notice_id = $notice->id;
330 $inbox->created = $notice->created;
331 $inbox->source = NOTICE_INBOX_SOURCE_GATEWAY; // From a private source
337 function ensureProfile($user)
339 // check to see if there's already a profile for this user
340 $profileurl = 'http://twitter.com/' . $user->screen_name;
341 $profile = Profile::staticGet('profileurl', $profileurl);
344 if (defined('SCRIPT_DEBUG')) {
345 common_debug("Profile for $profile->nickname found.");
348 // Check to see if the user's Avatar has changed
349 $this->checkAvatar($user, $profile);
354 if (defined('SCRIPT_DEBUG')) {
355 common_debug('Adding profile and remote profile ' .
356 "for Twitter user: $profileurl");
359 $profile = new Profile();
360 $profile->query("BEGIN");
362 $profile->nickname = $user->screen_name;
363 $profile->fullname = $user->name;
364 $profile->homepage = $user->url;
365 $profile->bio = $user->description;
366 $profile->location = $user->location;
367 $profile->profileurl = $profileurl;
368 $profile->created = common_sql_now();
370 $id = $profile->insert();
373 common_log_db_error($profile, 'INSERT', __FILE__);
374 $profile->query("ROLLBACK");
378 // check for remote profile
379 $remote_pro = Remote_profile::staticGet('uri', $profileurl);
383 $remote_pro = new Remote_profile();
385 $remote_pro->id = $id;
386 $remote_pro->uri = $profileurl;
387 $remote_pro->created = common_sql_now();
389 $rid = $remote_pro->insert();
392 common_log_db_error($profile, 'INSERT', __FILE__);
393 $profile->query("ROLLBACK");
398 $profile->query("COMMIT");
400 $this->saveAvatars($user, $id);
406 function checkAvatar($user, $profile)
410 $path_parts = pathinfo($user->profile_image_url);
411 $newname = 'Twitter_' . $user->id . '_' .
412 $path_parts['basename'];
414 $oldname = $profile->getAvatar(48)->filename;
416 if ($newname != $oldname) {
418 if (defined('SCRIPT_DEBUG')) {
419 common_debug('Avatar for Twitter user ' .
420 "$profile->nickname has changed.");
421 common_debug("old: $oldname new: $newname");
424 $img_root = substr($path_parts['basename'], 0, -11);
425 $ext = $path_parts['extension'];
426 $mediatype = $this->getMediatype($ext);
428 foreach (array('mini', 'normal', 'bigger') as $size) {
429 $url = $path_parts['dirname'] . '/' .
430 $img_root . '_' . $size . ".$ext";
431 $filename = 'Twitter_' . $user->id . '_' .
432 $img_root . "_$size.$ext";
434 if ($this->fetchAvatar($url, $filename)) {
435 $this->updateAvatar($profile->id, $size, $mediatype, $filename);
441 function getMediatype($ext)
445 switch (strtolower($ext)) {
447 $mediatype = 'image/jpg';
450 $mediatype = 'image/gif';
453 $mediatype = 'image/png';
459 function saveAvatars($user, $id)
463 $path_parts = pathinfo($user->profile_image_url);
464 $ext = $path_parts['extension'];
465 $end = strlen('_normal' . $ext);
466 $img_root = substr($path_parts['basename'], 0, -($end+1));
467 $mediatype = $this->getMediatype($ext);
469 foreach (array('mini', 'normal', 'bigger') as $size) {
470 $url = $path_parts['dirname'] . '/' .
471 $img_root . '_' . $size . ".$ext";
472 $filename = 'Twitter_' . $user->id . '_' .
473 $img_root . "_$size.$ext";
475 if ($this->fetchAvatar($url, $filename)) {
476 $this->newAvatar($id, $size, $mediatype, $filename);
478 common_log(LOG_WARNING, "Problem fetching Avatar: $url", __FILE__);
483 function updateAvatar($profile_id, $size, $mediatype, $filename) {
485 if (defined('SCRIPT_DEBUG')) {
486 common_debug("Updating avatar: $size");
489 $profile = Profile::staticGet($profile_id);
492 if (defined('SCRIPT_DEBUG')) {
493 common_debug("Couldn't get profile: $profile_id!");
498 $sizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73);
499 $avatar = $profile->getAvatar($sizes[$size]);
502 if (defined('SCRIPT_DEBUG')) {
503 common_debug("Deleting $size avatar for $profile->nickname.");
505 @unlink(INSTALLDIR . '/avatar/' . $avatar->filename);
509 $this->newAvatar($profile->id, $size, $mediatype, $filename);
512 function newAvatar($profile_id, $size, $mediatype, $filename)
516 $avatar = new Avatar();
517 $avatar->profile_id = $profile_id;
522 $avatar->height = 24;
526 $avatar->height = 48;
530 // Note: Twitter's big avatars are a different size than
531 // Laconica's (Laconica's = 96)
534 $avatar->height = 73;
537 $avatar->original = 0; // we don't have the original
538 $avatar->mediatype = $mediatype;
539 $avatar->filename = $filename;
540 $avatar->url = Avatar::url($filename);
542 if (defined('SCRIPT_DEBUG')) {
543 common_debug("new filename: $avatar->url");
546 $avatar->created = common_sql_now();
548 $id = $avatar->insert();
551 common_log_db_error($avatar, 'INSERT', __FILE__);
555 if (defined('SCRIPT_DEBUG')) {
556 common_debug("Saved new $size avatar for $profile_id.");
562 function fetchAvatar($url, $filename)
564 $avatar_dir = INSTALLDIR . '/avatar/';
566 $avatarfile = $avatar_dir . $filename;
568 $out = fopen($avatarfile, 'wb');
570 common_log(LOG_WARNING, "Couldn't open file $filename", __FILE__);
574 if (defined('SCRIPT_DEBUG')) {
575 common_debug("Fetching avatar: $url");
579 curl_setopt($ch, CURLOPT_URL, $url);
580 curl_setopt($ch, CURLOPT_FILE, $out);
581 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
582 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
583 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
584 $result = curl_exec($ch);
593 ini_set("max_execution_time", "0");
594 ini_set("max_input_time", "0");
596 mb_internal_encoding('UTF-8');
599 $fetcher = new TwitterStatusFetcher();