3 * StatusNet, the distributed open-source microblogging tool
7 * LICENCE: 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/>.
22 * @author Brion Vibber <brion@status.net>
23 * @copyright 2010 StatusNet, Inc.
24 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25 * @link http://status.net/
28 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
31 $longoptions = array('nick=','import','all');
33 $helptext = <<<ENDOFHELP
34 USAGE: fakestream.php -n <username>
36 -n --nick=<username> Local user whose Twitter timeline to watch
37 --import Experimental: run incoming messages through import
38 --all Experimental: run multiuser; requires nick be the app owner
40 Attempts a User Stream connection to Twitter as the given user, dumping
45 require_once INSTALLDIR.'/scripts/commandline.inc';
47 if (have_option('n')) {
48 $nickname = get_option_value('n');
49 } else if (have_option('nick')) {
50 $nickname = get_option_value('nickname');
51 } else if (have_option('all')) {
61 * @return TwitterOAuthClient
63 function twitterAuthForUser(User $user)
65 $flink = Foreign_link::getByUserID($user->id,
68 throw new ServerException("No Twitter config for this user.");
71 $token = TwitterOAuthClient::unpackToken($flink->credentials);
73 throw new ServerException("No Twitter OAuth credentials for this user.");
76 return new TwitterOAuthClient($token->key, $token->secret);
80 * Emulate the line-by-line output...
82 * @param Foreign_link $flink
85 function dumpMessage($flink, $data)
87 $msg = prepMessage($flink, $data);
88 print json_encode($msg) . "\r\n";
91 function prepMessage($flink, $data)
93 $msg->for_user = $flink->foreign_id;
94 $msg->message = $data;
98 if (have_option('all')) {
101 $flink = new Foreign_link();
102 $flink->service = TWITTER_SERVICE;
105 while ($flink->fetch()) {
106 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
107 FOREIGN_NOTICE_RECV) {
108 $users[] = $flink->user_id;
112 $user = User::staticGet('nickname', $nickname);
113 $users = array($user->id);
117 foreach ($users as $id) {
118 $user = User::staticGet('id', $id);
120 throw new Exception("No user for id $id");
122 $auth = twitterAuthForUser($user);
123 $flink = Foreign_link::getByUserID($user->id,
126 $friends->friends = $auth->friendsIds();
127 dumpMessage($flink, $friends);
129 $timeline = $auth->statusesHomeTimeline();
130 foreach ($timeline as $status) {
131 $output[] = prepMessage($flink, $status);
135 usort($output, function($a, $b) {
136 if ($a->message->id < $b->message->id) {
138 } else if ($a->message->id == $b->message->id) {
145 foreach ($output as $msg) {
146 print json_encode($msg) . "\r\n";