]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/scripts/fakestream.php
cdb56d4910a0bde42481bb3b80524d259cee2f0b
[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 {
52     show_help($helptext);
53     exit(0);
54 }
55
56 /**
57  *
58  * @param User $user 
59  * @return TwitterOAuthClient
60  */
61 function twitterAuthForUser(User $user)
62 {
63     $flink = Foreign_link::getByUserID($user->id,
64                                        TWITTER_SERVICE);
65     if (!$flink) {
66         throw new ServerException("No Twitter config for this user.");
67     }
68
69     $token = TwitterOAuthClient::unpackToken($flink->credentials);
70     if (!$token) {
71         throw new ServerException("No Twitter OAuth credentials for this user.");
72     }
73
74     return new TwitterOAuthClient($token->key, $token->secret);
75 }
76
77 /**
78  * Emulate the line-by-line output...
79  *
80  * @param Foreign_link $flink
81  * @param mixed $data
82  */
83 function dumpMessage($flink, $data)
84 {
85     $msg->for_user = $flink->foreign_id;
86     $msg->message = $data;
87     print json_encode($msg) . "\r\n";
88 }
89
90 if (have_option('all')) {
91     throw new Exception('--all not yet implemented');
92 }
93
94 $user = User::staticGet('nickname', $nickname);
95 $auth = twitterAuthForUser($user);
96 $flink = Foreign_link::getByUserID($user->id,
97                                    TWITTER_SERVICE);
98
99 $friends->friends = $auth->friendsIds();
100 dumpMessage($flink, $friends);
101
102 $timeline = $auth->statusesHomeTimeline();
103 foreach ($timeline as $status) {
104     dumpMessage($flink, $status);
105 }
106