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', 70); // in seconds, Twitter API v1.1 says 15 calls every 15 mins
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.php';
40 require_once INSTALLDIR . '/lib/common.php';
41 require_once INSTALLDIR . '/lib/daemon.php';
42 require_once dirname(__DIR__) . '/twitter.php';
45 * Fetch statuses from Twitter
47 * Fetches statuses from Twitter and inserts them as notices
49 * NOTE: an Avatar path MUST be set in config.php for this
50 * script to work, e.g.:
51 * $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
53 * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
54 * this script is run, so the default avatar path is always set wrong in
55 * default.php. Therefore it must be set explicitly in config.php. --Z
59 * @author Zach Copley <zach@status.net>
60 * @author Evan Prodromou <evan@status.net>
61 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
62 * @link http://status.net/
64 class TwitterStatusFetcher extends ParallelizingDaemon
69 * @param string $id the name/id of this daemon
70 * @param int $interval sleep this long before doing everything again
71 * @param int $max_children maximum number of child processes at a time
72 * @param boolean $debug debug output flag
77 function __construct($id = null, $interval = 60,
78 $max_children = 2, $debug = null)
80 parent::__construct($id, $interval, $max_children, $debug);
86 * @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
101 global $_DB_DATAOBJECT;
102 $flink = new Foreign_link();
103 $conn = &$flink->getDatabaseConnection();
105 $flink->service = TWITTER_SERVICE;
106 $flink->orderBy('last_noticesync');
111 while ($flink->fetch()) {
113 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
114 FOREIGN_NOTICE_RECV) {
115 $flinks[] = clone($flink);
116 common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
118 common_log(LOG_INFO, "nothing to sync");
126 unset($_DB_DATAOBJECT['CONNECTIONS']);
131 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
136 $conn = &$flink->getDatabaseConnection();
138 $this->getTimeline($flink, 'home_timeline');
139 $this->getTimeline($flink, 'mentions_timeline');
141 $flink->last_friendsync = common_sql_now();
146 // XXX: Couldn't find a less brutal way to blow
147 // away a cached connection
148 global $_DB_DATAOBJECT;
149 unset($_DB_DATAOBJECT['CONNECTIONS']);
152 function getTimeline($flink, $timelineUri = 'home_timeline')
155 common_log(LOG_ERR, $this->name() .
156 " - Can't retrieve Foreign_link for foreign ID $fid");
160 common_debug($this->name() . ' - Trying to get ' . $timelineUri .
161 ' timeline for Twitter user ' . $flink->foreign_id);
165 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
166 $token = TwitterOAuthClient::unpackToken($flink->credentials);
167 $client = new TwitterOAuthClient($token->key, $token->secret);
168 common_debug($this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
170 common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
171 $flink->foreign_id . " since not OAuth.");
176 $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
178 common_debug("Got lastId value '" . $lastId . "' for foreign id '" .
179 $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
182 $timeline = $client->statusesTimeline($lastId, $timelineUri);
183 } catch (Exception $e) {
184 common_log(LOG_ERR, $this->name() .
185 ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
186 ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
189 if (empty($timeline)) {
190 common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
194 common_log(LOG_INFO, $this->name() .
195 ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
196 ' - for user ' . $flink->user_id);
198 if (!empty($timeline)) {
199 $qm = QueueManager::get();
201 // Reverse to preserve order
202 foreach (array_reverse($timeline) as $status) {
205 'for_user' => $flink->foreign_id,
207 $qm->enqueue($data, 'tweetin');
210 $lastId = twitter_id($timeline[0]);
211 Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
212 common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
216 // Okay, record the time we synced with Twitter for posterity
217 $flink->last_noticesync = common_sql_now();
225 if (have_option('i')) {
226 $id = get_option_value('i');
227 } else if (have_option('--id')) {
228 $id = get_option_value('--id');
229 } else if (count($args) > 0) {
235 if (have_option('d') || have_option('debug')) {
239 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);