]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TwitterBridge/scripts/streamtest.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / TwitterBridge / scripts / streamtest.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','apiroot=');
32
33 $helptext = <<<ENDOFHELP
34 USAGE: streamtest.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      --apiroot=<url>   Provide alternate streaming API root URL
40
41 Attempts a User Stream connection to Twitter as the given user, dumping
42 data as it comes.
43
44 ENDOFHELP;
45
46 require_once INSTALLDIR.'/scripts/commandline.inc.php';
47 require_once dirname(dirname(__FILE__)) . '/lib/jsonstreamreader.php';
48 require_once dirname(dirname(__FILE__)) . '/lib/twitterstreamreader.php';
49
50 if (have_option('n')) {
51     $nickname = get_option_value('n');
52 } else if (have_option('nick')) {
53     $nickname = get_option_value('nickname');
54 } else {
55     show_help($helptext);
56     exit(0);
57 }
58
59 /**
60  *
61  * @param User $user 
62  * @return TwitterOAuthClient
63  */
64 function twitterAuthForUser(User $user)
65 {
66     $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
67     $token = TwitterOAuthClient::unpackToken($flink->credentials);
68     if (!$token) {
69         throw new ServerException("No Twitter OAuth credentials for this user.");
70     }
71
72     return new TwitterOAuthClient($token->key, $token->secret);
73 }
74
75 function homeStreamForUser(User $user)
76 {
77     $auth = twitterAuthForUser($user);
78     return new TwitterUserStream($auth);
79 }
80
81 function siteStreamForOwner(User $user)
82 {
83     // The user we auth as must be the owner of the application.
84     $auth = twitterAuthForUser($user);
85
86     if (have_option('apiroot')) {
87         $stream = new TwitterSiteStream($auth, get_option_value('apiroot'));
88     } else {
89         $stream = new TwitterSiteStream($auth);
90     }
91
92     // Pull Twitter user IDs for all users we want to pull data for
93     $userIds = array();
94
95     $flink = new Foreign_link();
96     $flink->service = TWITTER_SERVICE;
97     $flink->find();
98
99     while ($flink->fetch()) {
100         if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
101             FOREIGN_NOTICE_RECV) {
102             $userIds[] = $flink->foreign_id;
103         }
104     }
105
106     $stream->followUsers($userIds);
107     return $stream;
108 }
109
110
111 $user = User::getKV('nickname', $nickname);
112 global $myuser;
113 $myuser = $user;
114
115 if (have_option('all')) {
116     $stream = siteStreamForOwner($user);
117 } else {
118     $stream = homeStreamForUser($user);
119 }
120
121
122 $stream->hookEvent('raw', function($data, $context) {
123     common_log(LOG_INFO, json_encode($data) . ' for ' . json_encode($context));
124 });
125 $stream->hookEvent('friends', function($data, $context) {
126     printf("Friend list: %s\n", implode(', ', $data->friends));
127 });
128 $stream->hookEvent('favorite', function($data, $context) {
129     printf("%s favorited %s's notice: %s\n",
130             $data->source->screen_name,
131             $data->target->screen_name,
132             $data->target_object->text);
133 });
134 $stream->hookEvent('unfavorite', function($data, $context) {
135     printf("%s unfavorited %s's notice: %s\n",
136             $data->source->screen_name,
137             $data->target->screen_name,
138             $data->target_object->text);
139 });
140 $stream->hookEvent('follow', function($data, $context) {
141     printf("%s friended %s\n",
142             $data->source->screen_name,
143             $data->target->screen_name);
144 });
145 $stream->hookEvent('unfollow', function($data, $context) {
146     printf("%s unfriended %s\n",
147             $data->source->screen_name,
148             $data->target->screen_name);
149 });
150 $stream->hookEvent('delete', function($data, $context) {
151     printf("Deleted status notification: %s\n",
152             $data->status->id);
153 });
154 $stream->hookEvent('scrub_geo', function($data, $context) {
155     printf("Req to scrub geo data for user id %s up to status ID %s\n",
156             $data->user_id,
157             $data->up_to_status_id);
158 });
159 $stream->hookEvent('status', function($data, $context) {
160     printf("Received status update from %s: %s\n",
161             $data->user->screen_name,
162             $data->text);
163
164     if (have_option('import')) {
165         $importer = new TwitterImport();
166         printf("\timporting...");
167         $notice = $importer->importStatus($data);
168         if (!$notice instanceof Notice) {
169             printf(" FAIL\n");
170         }
171     }
172 });
173 $stream->hookEvent('direct_message', function($data) {
174     printf("Direct message from %s to %s: %s\n",
175             $data->sender->screen_name,
176             $data->recipient->screen_name,
177             $data->text);
178 });
179
180 class TwitterManager extends IoManager
181 {
182     function __construct(TwitterStreamReader $stream)
183     {
184         $this->stream = $stream;
185     }
186
187     function getSockets()
188     {
189         return $this->stream->getSockets();
190     }
191
192     function handleInput($data)
193     {
194         $this->stream->handleInput($data);
195         return true;
196     }
197
198     function start()
199     {
200         $this->stream->connect();
201         return true;
202     }
203
204     function finish()
205     {
206         $this->stream->close();
207         return true;
208     }
209
210     public static function get()
211     {
212         throw new Exception('not a singleton');
213     }
214 }
215
216 class TwitterStreamMaster extends IoMaster
217 {
218     function __construct($id, $ioManager)
219     {
220         parent::__construct($id);
221         $this->ioManager = $ioManager;
222     }
223
224     /**
225      * Initialize IoManagers which are appropriate to this instance.
226      */
227     function initManagers()
228     {
229         $this->instantiate($this->ioManager);
230     }
231 }
232
233 $master = new TwitterStreamMaster('TwitterStream', new TwitterManager($stream));
234 $master->init();
235 $master->service();