]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/scripts/fakestream.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / TwitterBridge / scripts / fakestream.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
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/
26  */
27
28 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
29
30 $shortoptions = 'n:';
31 $longoptions = array('nick=','import','all');
32
33 $helptext = <<<ENDOFHELP
34 USAGE: fakestream.php -n <username>
35
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
39
40 Attempts a User Stream connection to Twitter as the given user, dumping
41 data as it comes.
42
43 ENDOFHELP;
44
45 require_once INSTALLDIR.'/scripts/commandline.inc.php';
46
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')) {
52     $nickname = null;
53 } else {
54     show_help($helptext);
55     exit(0);
56 }
57
58 /**
59  *
60  * @param User $user 
61  * @return TwitterOAuthClient
62  */
63 function twitterAuthForUser(User $user)
64 {
65     $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
66     $token = TwitterOAuthClient::unpackToken($flink->credentials);
67     if (!$token) {
68         throw new ServerException("No Twitter OAuth credentials for this user.");
69     }
70
71     return new TwitterOAuthClient($token->key, $token->secret);
72 }
73
74 /**
75  * Emulate the line-by-line output...
76  *
77  * @param Foreign_link $flink
78  * @param mixed $data
79  */
80 function dumpMessage($flink, $data)
81 {
82     $msg = prepMessage($flink, $data);
83     print json_encode($msg) . "\r\n";
84 }
85
86 function prepMessage($flink, $data)
87 {
88     $msg->for_user = $flink->foreign_id;
89     $msg->message = $data;
90     return $msg;
91 }
92
93 if (have_option('all')) {
94     $users = array();
95
96     $flink = new Foreign_link();
97     $flink->service = TWITTER_SERVICE;
98     $flink->find();
99
100     while ($flink->fetch()) {
101         if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
102             FOREIGN_NOTICE_RECV) {
103             $users[] = $flink->user_id;
104         }
105     }
106 } else {
107     $user = User::getKV('nickname', $nickname);
108     $users = array($user->id);
109 }
110
111 $output = array();
112 foreach ($users as $id) {
113     $user = User::getKV('id', $id);
114     if (!$user) {
115         throw new Exception("No user for id $id");
116     }
117     $auth = twitterAuthForUser($user);
118     $flink = Foreign_link::getByUserID($user->id,
119                                        TWITTER_SERVICE);
120
121     $friends->friends = $auth->friendsIds();
122     dumpMessage($flink, $friends);
123
124     $timeline = $auth->statusesHomeTimeline();
125     foreach ($timeline as $status) {
126         $output[] = prepMessage($flink, $status);
127     }
128 }
129
130 usort($output, function($a, $b) {
131     if ($a->message->id < $b->message->id) {
132         return -1;
133     } else if ($a->message->id == $b->message->id) {
134         return 0;
135     } else {
136         return 1;
137     }
138 });
139
140 foreach ($output as $msg) {
141     print json_encode($msg) . "\r\n";
142 }