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);
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)
155 common_log(LOG_WARNING, $this->name() .
156 " - Can't retrieve Foreign_link for foreign ID $fid");
160 common_debug($this->name() . ' - Trying to get timeline for Twitter user ' .
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 friends timeline with OAuth.');
170 common_debug("Skipping friends timeline for $flink->foreign_id since not OAuth.");
175 $lastId = Twitter_synch_status::getLastId($flink->foreign_id, 'home_timeline');
177 common_debug("Got lastId value '{$lastId}' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'");
180 $timeline = $client->statusesHomeTimeline($lastId);
181 } catch (Exception $e) {
182 common_log(LOG_WARNING, $this->name() .
183 ' - Twitter client unable to get friends timeline for user ' .
184 $flink->user_id . ' - code: ' .
185 $e->getCode() . 'msg: ' . $e->getMessage());
188 if (empty($timeline)) {
189 common_log(LOG_WARNING, $this->name() . " - Empty timeline.");
193 common_debug(LOG_INFO, $this->name() . ' - Retrieved ' . sizeof($timeline) . ' statuses from Twitter.');
195 $importer = new TwitterImport();
197 // Reverse to preserve order
199 foreach (array_reverse($timeline) as $status) {
200 $notice = $importer->importStatus($status);
202 if (!empty($notice)) {
203 Inbox::insertNotice($flink->user_id, $notice->id);
207 if (!empty($timeline)) {
208 $lastId = twitter_id($timeline[0]);
209 Twitter_synch_status::setLastId($flink->foreign_id, 'home_timeline', $lastId);
210 common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline 'home_timeline'");
213 // Okay, record the time we synced with Twitter for posterity
214 $flink->last_noticesync = common_sql_now();
222 if (have_option('i')) {
223 $id = get_option_value('i');
224 } else if (have_option('--id')) {
225 $id = get_option_value('--id');
226 } else if (count($args) > 0) {
232 if (have_option('d') || have_option('debug')) {
236 $fetcher = new TwitterStatusFetcher($id, 60, 2, $debug);