fakestream.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  28. $shortoptions = 'n:';
  29. $longoptions = array('nick=','import','all');
  30. $helptext = <<<ENDOFHELP
  31. USAGE: fakestream.php -n <username>
  32. -n --nick=<username> Local user whose Twitter timeline to watch
  33. --import Experimental: run incoming messages through import
  34. --all Experimental: run multiuser; requires nick be the app owner
  35. Attempts a User Stream connection to Twitter as the given user, dumping
  36. data as it comes.
  37. ENDOFHELP;
  38. require_once INSTALLDIR.'/scripts/commandline.inc';
  39. if (have_option('n')) {
  40. $nickname = get_option_value('n');
  41. } else if (have_option('nick')) {
  42. $nickname = get_option_value('nickname');
  43. } else if (have_option('all')) {
  44. $nickname = null;
  45. } else {
  46. show_help($helptext);
  47. exit(0);
  48. }
  49. /**
  50. *
  51. * @param User $user
  52. * @return TwitterOAuthClient
  53. */
  54. function twitterAuthForUser(User $user)
  55. {
  56. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  57. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  58. if (!$token) {
  59. throw new ServerException("No Twitter OAuth credentials for this user.");
  60. }
  61. return new TwitterOAuthClient($token->key, $token->secret);
  62. }
  63. /**
  64. * Emulate the line-by-line output...
  65. *
  66. * @param Foreign_link $flink
  67. * @param mixed $data
  68. */
  69. function dumpMessage($flink, $data)
  70. {
  71. $msg = prepMessage($flink, $data);
  72. print json_encode($msg) . "\r\n";
  73. }
  74. function prepMessage($flink, $data)
  75. {
  76. $msg->for_user = $flink->foreign_id;
  77. $msg->message = $data;
  78. return $msg;
  79. }
  80. if (have_option('all')) {
  81. $users = array();
  82. $flink = new Foreign_link();
  83. $flink->service = TWITTER_SERVICE;
  84. $flink->find();
  85. while ($flink->fetch()) {
  86. if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
  87. FOREIGN_NOTICE_RECV) {
  88. $users[] = $flink->user_id;
  89. }
  90. }
  91. } else {
  92. $user = User::getKV('nickname', $nickname);
  93. $users = array($user->id);
  94. }
  95. $output = array();
  96. foreach ($users as $id) {
  97. $user = User::getKV('id', $id);
  98. if (!$user) {
  99. throw new Exception("No user for id $id");
  100. }
  101. $auth = twitterAuthForUser($user);
  102. $flink = Foreign_link::getByUserID($user->id,
  103. TWITTER_SERVICE);
  104. $friends->friends = $auth->friendsIds();
  105. dumpMessage($flink, $friends);
  106. $timeline = $auth->statusesHomeTimeline();
  107. foreach ($timeline as $status) {
  108. $output[] = prepMessage($flink, $status);
  109. }
  110. }
  111. usort($output, function($a, $b) {
  112. if ($a->message->id < $b->message->id) {
  113. return -1;
  114. } else if ($a->message->id == $b->message->id) {
  115. return 0;
  116. } else {
  117. return 1;
  118. }
  119. });
  120. foreach ($output as $msg) {
  121. print json_encode($msg) . "\r\n";
  122. }