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 // FIXME: make it so we can force a Foreign_link here without colliding with parent
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(Foreign_link $flink, $timelineUri = 'home_timeline')
155 common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
156 ' timeline for Twitter user ' . $flink->foreign_id);
160 if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
161 $token = TwitterOAuthClient::unpackToken($flink->credentials);
162 $client = new TwitterOAuthClient($token->key, $token->secret);
163 common_debug($this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
165 common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
166 $flink->foreign_id . " since not OAuth.");
171 $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
173 common_debug("Got lastId value '" . $lastId . "' for foreign id '" .
174 $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
177 $timeline = $client->statusesTimeline($lastId, $timelineUri);
178 } catch (Exception $e) {
179 common_log(LOG_ERR, $this->name() .
180 ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
181 ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
184 if (empty($timeline)) {
185 common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
189 common_log(LOG_INFO, $this->name() .
190 ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
191 ' - for user ' . $flink->user_id);
193 if (!empty($timeline)) {
194 $qm = QueueManager::get();
196 // Reverse to preserve order
197 foreach (array_reverse($timeline) as $status) {
200 'for_user' => $flink->foreign_id,
202 $qm->enqueue($data, 'tweetin');
205 $lastId = twitter_id($timeline[0]);
206 Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
207 common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
211 // Okay, record the time we synced with Twitter for posterity
212 $flink->last_noticesync = common_sql_now();
220 if (have_option('i')) {
221 $id = get_option_value('i');
222 } else if (have_option('--id')) {
223 $id = get_option_value('--id');
224 } else if (count($args) > 0) {
230 if (have_option('d') || have_option('debug')) {
234 $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);