streamtest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @category Plugin
  19. * @package GNUsocial
  20. * @author Brion Vibber <brion@status.net>
  21. * @copyright 2010 StatusNet, Inc.
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. define('INSTALLDIR', dirname(__DIR__, 3));
  25. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  26. $shortoptions = 'n:';
  27. $longoptions = array('nick=','import','all','apiroot=');
  28. $helptext = <<<ENDOFHELP
  29. USAGE: streamtest.php -n <username>
  30. -n --nick=<username> Local user whose Twitter timeline to watch
  31. --import Experimental: run incoming messages through import
  32. --all Experimental: run multiuser; requires nick be the app owner
  33. --apiroot=<url> Provide alternate streaming API root URL
  34. Attempts a User Stream connection to Twitter as the given user, dumping
  35. data as it comes.
  36. ENDOFHELP;
  37. require_once INSTALLDIR . '/scripts/commandline.inc';
  38. require_once dirname(dirname(__FILE__)) . '/lib/jsonstreamreader.php';
  39. require_once dirname(dirname(__FILE__)) . '/lib/twitterstreamreader.php';
  40. if (have_option('n')) {
  41. $nickname = get_option_value('n');
  42. } elseif (have_option('nick')) {
  43. $nickname = get_option_value('nickname');
  44. } else {
  45. show_help($helptext);
  46. exit(0);
  47. }
  48. /**
  49. *
  50. * @param User $user
  51. * @return TwitterOAuthClient
  52. */
  53. function twitterAuthForUser(User $user)
  54. {
  55. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  56. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  57. if (!$token) {
  58. throw new ServerException("No Twitter OAuth credentials for this user.");
  59. }
  60. return new TwitterOAuthClient($token->key, $token->secret);
  61. }
  62. function homeStreamForUser(User $user)
  63. {
  64. $auth = twitterAuthForUser($user);
  65. return new TwitterUserStream($auth);
  66. }
  67. function siteStreamForOwner(User $user)
  68. {
  69. // The user we auth as must be the owner of the application.
  70. $auth = twitterAuthForUser($user);
  71. if (have_option('apiroot')) {
  72. $stream = new TwitterSiteStream($auth, get_option_value('apiroot'));
  73. } else {
  74. $stream = new TwitterSiteStream($auth);
  75. }
  76. // Pull Twitter user IDs for all users we want to pull data for
  77. $userIds = array();
  78. $flink = new Foreign_link();
  79. $flink->service = TWITTER_SERVICE;
  80. $flink->find();
  81. while ($flink->fetch()) {
  82. if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
  83. FOREIGN_NOTICE_RECV) {
  84. $userIds[] = $flink->foreign_id;
  85. }
  86. }
  87. $stream->followUsers($userIds);
  88. return $stream;
  89. }
  90. $user = User::getKV('nickname', $nickname);
  91. global $myuser;
  92. $myuser = $user;
  93. if (have_option('all')) {
  94. $stream = siteStreamForOwner($user);
  95. } else {
  96. $stream = homeStreamForUser($user);
  97. }
  98. $stream->hookEvent('raw', function ($data, $context) {
  99. common_log(LOG_INFO, json_encode($data) . ' for ' . json_encode($context));
  100. });
  101. $stream->hookEvent('friends', function ($data, $context) {
  102. printf("Friend list: %s\n", implode(', ', $data->friends));
  103. });
  104. $stream->hookEvent('favorite', function ($data, $context) {
  105. printf(
  106. "%s favorited %s's notice: %s\n",
  107. $data->source->screen_name,
  108. $data->target->screen_name,
  109. $data->target_object->text
  110. );
  111. });
  112. $stream->hookEvent('unfavorite', function ($data, $context) {
  113. printf(
  114. "%s unfavorited %s's notice: %s\n",
  115. $data->source->screen_name,
  116. $data->target->screen_name,
  117. $data->target_object->text
  118. );
  119. });
  120. $stream->hookEvent('follow', function ($data, $context) {
  121. printf(
  122. '%s friended %s' . "\n",
  123. $data->source->screen_name,
  124. $data->target->screen_name
  125. );
  126. });
  127. $stream->hookEvent('unfollow', function ($data, $context) {
  128. printf(
  129. '%s unfriended %s' . "\n",
  130. $data->source->screen_name,
  131. $data->target->screen_name
  132. );
  133. });
  134. $stream->hookEvent('delete', function ($data, $context) {
  135. printf(
  136. 'Deleted status notification: %s' . "\n",
  137. $data->status->id
  138. );
  139. });
  140. $stream->hookEvent('scrub_geo', function ($data, $context) {
  141. printf(
  142. 'Req to scrub geo data for user id %s up to status ID %s' . "\n",
  143. $data->user_id,
  144. $data->up_to_status_id
  145. );
  146. });
  147. $stream->hookEvent('status', function ($data, $context) {
  148. printf(
  149. 'Received status update from %s: %s' . "\n",
  150. $data->user->screen_name,
  151. $data->text
  152. );
  153. if (have_option('import')) {
  154. $importer = new TwitterImport();
  155. printf("\timporting...");
  156. $notice = $importer->importStatus($data);
  157. if (!$notice instanceof Notice) {
  158. printf(" FAIL\n");
  159. }
  160. }
  161. });
  162. $stream->hookEvent('direct_message', function ($data) {
  163. printf(
  164. 'Direct message from %s to %s: %s' . "\n",
  165. $data->sender->screen_name,
  166. $data->recipient->screen_name,
  167. $data->text
  168. );
  169. });
  170. class TwitterManager extends IoManager
  171. {
  172. public function __construct(TwitterStreamReader $stream)
  173. {
  174. $this->stream = $stream;
  175. }
  176. public function getSockets()
  177. {
  178. return $this->stream->getSockets();
  179. }
  180. public function handleInput($data)
  181. {
  182. $this->stream->handleInput($data);
  183. return true;
  184. }
  185. public function start()
  186. {
  187. $this->stream->connect();
  188. return true;
  189. }
  190. public function finish()
  191. {
  192. $this->stream->close();
  193. return true;
  194. }
  195. public static function get()
  196. {
  197. throw new Exception('not a singleton');
  198. }
  199. }
  200. class TwitterStreamMaster extends IoMaster
  201. {
  202. public function __construct($id, $ioManager)
  203. {
  204. parent::__construct($id);
  205. $this->ioManager = $ioManager;
  206. }
  207. /**
  208. * Initialize IoManagers which are appropriate to this instance.
  209. */
  210. public function initManagers()
  211. {
  212. $this->instantiate($this->ioManager);
  213. }
  214. }
  215. $master = new TwitterStreamMaster('TwitterStream', new TwitterManager($stream));
  216. $master->init();
  217. $master->service();