]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/scripts/fakestream.php
Merge branch 'master' into 0.9.x
[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';
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,
66                                        TWITTER_SERVICE);
67     if (!$flink) {
68         throw new ServerException("No Twitter config for this user.");
69     }
70
71     $token = TwitterOAuthClient::unpackToken($flink->credentials);
72     if (!$token) {
73         throw new ServerException("No Twitter OAuth credentials for this user.");
74     }
75
76     return new TwitterOAuthClient($token->key, $token->secret);
77 }
78
79 /**
80  * Emulate the line-by-line output...
81  *
82  * @param Foreign_link $flink
83  * @param mixed $data
84  */
85 function dumpMessage($flink, $data)
86 {
87     $msg = prepMessage($flink, $data);
88     print json_encode($msg) . "\r\n";
89 }
90
91 function prepMessage($flink, $data)
92 {
93     $msg->for_user = $flink->foreign_id;
94     $msg->message = $data;
95     return $msg;
96 }
97
98 if (have_option('all')) {
99     $users = array();
100
101     $flink = new Foreign_link();
102     $flink->service = TWITTER_SERVICE;
103     $flink->find();
104
105     while ($flink->fetch()) {
106         if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
107             FOREIGN_NOTICE_RECV) {
108             $users[] = $flink->user_id;
109         }
110     }
111 } else {
112     $user = User::staticGet('nickname', $nickname);
113     $users = array($user->id);
114 }
115
116 $output = array();
117 foreach ($users as $id) {
118     $user = User::staticGet('id', $id);
119     if (!$user) {
120         throw new Exception("No user for id $id");
121     }
122     $auth = twitterAuthForUser($user);
123     $flink = Foreign_link::getByUserID($user->id,
124                                        TWITTER_SERVICE);
125
126     $friends->friends = $auth->friendsIds();
127     dumpMessage($flink, $friends);
128
129     $timeline = $auth->statusesHomeTimeline();
130     foreach ($timeline as $status) {
131         $output[] = prepMessage($flink, $status);
132     }
133 }
134
135 usort($output, function($a, $b) {
136     if ($a->message->id < $b->message->id) {
137         return -1;
138     } else if ($a->message->id == $b->message->id) {
139         return 0;
140     } else {
141         return 1;
142     }
143 });
144
145 foreach ($output as $msg) {
146     print json_encode($msg) . "\r\n";
147 }