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 // 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 $helptext = <<<END_OF_TRIM_HELP
29 Batch script for retrieving Twitter messages from foreign service.
33 require_once INSTALLDIR.'/scripts/commandline.inc';
35 require_once INSTALLDIR . '/lib/common.php';
36 require_once INSTALLDIR . '/lib/daemon.php';
39 * Fetcher for statuses from Twitter
41 * Fetches statuses from Twitter and inserts them as notices in local
46 * @author Zach Copley <zach@controlyourself.ca>
47 * @author Evan Prodromou <evan@controlyourself.ca>
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
49 * @link http://laconi.ca/
52 // NOTE: an Avatar path MUST be set in config.php for this
53 // script to work: e.g.: $config['avatar']['path'] = '/laconica/avatar';
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($twitter_user, $profile)
410 $path_parts = pathinfo($twitter_user->profile_image_url);
412 $newname = 'Twitter_' . $twitter_user->id . '_' .
413 $path_parts['basename'];
415 $oldname = $profile->getAvatar(48)->filename;
417 if ($newname != $oldname) {
419 if (defined('SCRIPT_DEBUG')) {
420 common_debug('Avatar for Twitter user ' .
421 "$profile->nickname has changed.");
422 common_debug("old: $oldname new: $newname");
425 $this->updateAvatars($twitter_user, $profile);
428 if ($this->missingAvatarFile($profile)) {
430 if (defined('SCRIPT_DEBUG')) {
431 common_debug('Twitter user ' . $profile->nickname .
432 ' is missing one or more local avatars.');
433 common_debug("old: $oldname new: $newname");
436 $this->updateAvatars($twitter_user, $profile);
441 function updateAvatars($twitter_user, $profile) {
445 $path_parts = pathinfo($twitter_user->profile_image_url);
447 $img_root = substr($path_parts['basename'], 0, -11);
448 $ext = $path_parts['extension'];
449 $mediatype = $this->getMediatype($ext);
451 foreach (array('mini', 'normal', 'bigger') as $size) {
452 $url = $path_parts['dirname'] . '/' .
453 $img_root . '_' . $size . ".$ext";
454 $filename = 'Twitter_' . $twitter_user->id . '_' .
455 $img_root . "_$size.$ext";
457 $this->updateAvatar($profile->id, $size, $mediatype, $filename);
458 $this->fetchAvatar($url, $filename);
462 function missingAvatarFile($profile) {
464 foreach (array(24, 48, 73) as $size) {
466 $filename = $profile->getAvatar($size)->filename;
467 $avatarpath = Avatar::path($filename);
469 if (file_exists($avatarpath) == FALSE) {
477 function getMediatype($ext)
481 switch (strtolower($ext)) {
483 $mediatype = 'image/jpg';
486 $mediatype = 'image/gif';
489 $mediatype = 'image/png';
495 function saveAvatars($user, $id)
499 $path_parts = pathinfo($user->profile_image_url);
500 $ext = $path_parts['extension'];
501 $end = strlen('_normal' . $ext);
502 $img_root = substr($path_parts['basename'], 0, -($end+1));
503 $mediatype = $this->getMediatype($ext);
505 foreach (array('mini', 'normal', 'bigger') as $size) {
506 $url = $path_parts['dirname'] . '/' .
507 $img_root . '_' . $size . ".$ext";
508 $filename = 'Twitter_' . $user->id . '_' .
509 $img_root . "_$size.$ext";
511 if ($this->fetchAvatar($url, $filename)) {
512 $this->newAvatar($id, $size, $mediatype, $filename);
514 common_log(LOG_WARNING, "Problem fetching Avatar: $url", __FILE__);
519 function updateAvatar($profile_id, $size, $mediatype, $filename) {
521 if (defined('SCRIPT_DEBUG')) {
522 common_debug("Updating avatar: $size");
525 $profile = Profile::staticGet($profile_id);
527 if (empty($profile)) {
528 if (defined('SCRIPT_DEBUG')) {
529 common_debug("Couldn't get profile: $profile_id!");
534 $sizes = array('mini' => 24, 'normal' => 48, 'bigger' => 73);
535 $avatar = $profile->getAvatar($sizes[$size]);
537 // Delete the avatar, if present
542 $this->newAvatar($profile->id, $size, $mediatype, $filename);
545 function newAvatar($profile_id, $size, $mediatype, $filename)
549 $avatar = new Avatar();
550 $avatar->profile_id = $profile_id;
555 $avatar->height = 24;
559 $avatar->height = 48;
563 // Note: Twitter's big avatars are a different size than
564 // Laconica's (Laconica's = 96)
567 $avatar->height = 73;
570 $avatar->original = 0; // we don't have the original
571 $avatar->mediatype = $mediatype;
572 $avatar->filename = $filename;
573 $avatar->url = Avatar::url($filename);
575 if (defined('SCRIPT_DEBUG')) {
576 common_debug("new filename: $avatar->url");
579 $avatar->created = common_sql_now();
581 $id = $avatar->insert();
584 common_log_db_error($avatar, 'INSERT', __FILE__);
588 if (defined('SCRIPT_DEBUG')) {
589 common_debug("Saved new $size avatar for $profile_id.");
595 function fetchAvatar($url, $filename)
597 $avatar_dir = INSTALLDIR . '/avatar/';
599 $avatarfile = $avatar_dir . $filename;
601 $out = fopen($avatarfile, 'wb');
603 common_log(LOG_WARNING, "Couldn't open file $filename", __FILE__);
607 if (defined('SCRIPT_DEBUG')) {
608 common_debug("Fetching avatar: $url");
612 curl_setopt($ch, CURLOPT_URL, $url);
613 curl_setopt($ch, CURLOPT_FILE, $out);
614 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
615 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
616 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
617 $result = curl_exec($ch);
628 $fetcher = new TwitterStatusFetcher();