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, TWITTER_SERVICE);
66 $token = TwitterOAuthClient::unpackToken($flink->credentials);
68 throw new ServerException("No Twitter OAuth credentials for this user.");
71 return new TwitterOAuthClient($token->key, $token->secret);
75 * Emulate the line-by-line output...
77 * @param Foreign_link $flink
80 function dumpMessage($flink, $data)
82 $msg = prepMessage($flink, $data);
83 print json_encode($msg) . "\r\n";
86 function prepMessage($flink, $data)
88 $msg->for_user = $flink->foreign_id;
89 $msg->message = $data;
93 if (have_option('all')) {
96 $flink = new Foreign_link();
97 $flink->service = TWITTER_SERVICE;
100 while ($flink->fetch()) {
101 if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
102 FOREIGN_NOTICE_RECV) {
103 $users[] = $flink->user_id;
107 $user = User::getKV('nickname', $nickname);
108 $users = array($user->id);
112 foreach ($users as $id) {
113 $user = User::getKV('id', $id);
115 throw new Exception("No user for id $id");
117 $auth = twitterAuthForUser($user);
118 $flink = Foreign_link::getByUserID($user->id,
121 $friends->friends = $auth->friendsIds();
122 dumpMessage($flink, $friends);
124 $timeline = $auth->statusesHomeTimeline();
125 foreach ($timeline as $status) {
126 $output[] = prepMessage($flink, $status);
130 usort($output, function($a, $b) {
131 if ($a->message->id < $b->message->id) {
133 } else if ($a->message->id == $b->message->id) {
140 foreach ($output as $msg) {
141 print json_encode($msg) . "\r\n";