4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008-2010, 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/twitteroauthclient.php';
46 * Fetch statuses from Twitter
48 * Fetches statuses from Twitter and inserts them as notices
50 * NOTE: an Avatar path MUST be set in config.php for this
51 * script to work, e.g.:
52 * $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
54 * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
55 * this script is run, so the default avatar path is always set wrong in
56 * default.php. Therefore it must be set explicitly in config.php. --Z
60 * @author Zach Copley <zach@status.net>
61 * @author Evan Prodromou <evan@status.net>
62 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
63 * @link http://status.net/
65 class TwitterStatusFetcher extends ParallelizingDaemon
70 * @param string $id the name/id of this daemon
71 * @param int $interval sleep this long before doing everything again
72 * @param int $max_children maximum number of child processes at a time
73 * @param boolean $debug debug output flag
78 function __construct($id = null, $interval = 60,
79 $max_children = 2, $debug = null)
81 parent::__construct($id, $interval, $max_children, $debug);
87 * @return string Name of the daemon.
91 return ('twitterstatusfetcher.'.$this->_id);
95 * Find all the Twitter foreign links for users who have requested
96 * importing of their friends' timelines
98 * @return array flinks an array of Foreign_link objects
100 function getObjects()
102 global $_DB_DATAOBJECT;
103 $flink = new Foreign_link();
104 $conn = &$flink->getDatabaseConnection();
106 $flink->service = TWITTER_SERVICE;
107 $flink->orderBy('last_noticesync');
112 while ($flink->fetch()) {
114 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
115 FOREIGN_NOTICE_RECV) {
116 $flinks[] = clone($flink);
117 common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
119 common_log(LOG_INFO, "nothing to sync");
127 unset($_DB_DATAOBJECT['CONNECTIONS']);
132 function childTask($flink) {
133 // Each child ps needs its own DB connection
135 // Note: DataObject::getDatabaseConnection() creates
136 // a new connection if there isn't one already
137 $conn = &$flink->getDatabaseConnection();
139 $this->getTimeline($flink, 'home_timeline');
140 $this->getTimeline($flink, 'mentions_timeline');
142 $flink->last_friendsync = common_sql_now();
147 // XXX: Couldn't find a less brutal way to blow
148 // away a cached connection
149 global $_DB_DATAOBJECT;
150 unset($_DB_DATAOBJECT['CONNECTIONS']);
153 function getTimeline($flink, $timelineUri = 'home_timeline')
156 common_log(LOG_ERR, $this->name() .
157 " - Can't retrieve Foreign_link for foreign ID $fid");
161 common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
162 ' timeline for Twitter user ' . $flink->foreign_id);
166 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
167 $token = TwitterOAuthClient::unpackToken($flink->credentials);
168 $client = new TwitterOAuthClient($token->key, $token->secret);
169 common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
171 common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
172 $flink->foreign_id . " since not OAuth.");
177 $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
179 common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
180 $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
183 $timeline = $client->statusesTimeline($lastId, $timelineUri);
184 } catch (Exception $e) {
185 common_log(LOG_ERR, $this->name() .
186 ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
187 ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
190 if (empty($timeline)) {
191 common_log(LOG_WARNING, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
195 common_log(LOG_INFO, $this->name() .
196 ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
197 ' - for user ' . $flink->user_id);
199 if (!empty($timeline)) {
200 $qm = QueueManager::get();
202 // Reverse to preserve order
203 foreach (array_reverse($timeline) as $status) {
206 'for_user' => $flink->foreign_id,
208 $qm->enqueue($data, 'tweetin');
211 $lastId = twitter_id($timeline[0]);
212 Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
213 common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
217 // Okay, record the time we synced with Twitter for posterity
218 $flink->last_noticesync = common_sql_now();
226 if (have_option('i')) {
227 $id = get_option_value('i');
228 } else if (have_option('--id')) {
229 $id = get_option_value('--id');
230 } else if (count($args) > 0) {
236 if (have_option('d') || have_option('debug')) {
240 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);