fakestream.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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,
  57. TWITTER_SERVICE);
  58. if (!$flink) {
  59. throw new ServerException("No Twitter config for this user.");
  60. }
  61. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  62. if (!$token) {
  63. throw new ServerException("No Twitter OAuth credentials for this user.");
  64. }
  65. return new TwitterOAuthClient($token->key, $token->secret);
  66. }
  67. /**
  68. * Emulate the line-by-line output...
  69. *
  70. * @param Foreign_link $flink
  71. * @param mixed $data
  72. */
  73. function dumpMessage($flink, $data)
  74. {
  75. $msg = prepMessage($flink, $data);
  76. print json_encode($msg) . "\r\n";
  77. }
  78. function prepMessage($flink, $data)
  79. {
  80. $msg->for_user = $flink->foreign_id;
  81. $msg->message = $data;
  82. return $msg;
  83. }
  84. if (have_option('all')) {
  85. $users = array();
  86. $flink = new Foreign_link();
  87. $flink->service = TWITTER_SERVICE;
  88. $flink->find();
  89. while ($flink->fetch()) {
  90. if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
  91. FOREIGN_NOTICE_RECV) {
  92. $users[] = $flink->user_id;
  93. }
  94. }
  95. } else {
  96. $user = User::getKV('nickname', $nickname);
  97. $users = array($user->id);
  98. }
  99. $output = array();
  100. foreach ($users as $id) {
  101. $user = User::getKV('id', $id);
  102. if (!$user) {
  103. throw new Exception("No user for id $id");
  104. }
  105. $auth = twitterAuthForUser($user);
  106. $flink = Foreign_link::getByUserID($user->id,
  107. TWITTER_SERVICE);
  108. $friends->friends = $auth->friendsIds();
  109. dumpMessage($flink, $friends);
  110. $timeline = $auth->statusesHomeTimeline();
  111. foreach ($timeline as $status) {
  112. $output[] = prepMessage($flink, $status);
  113. }
  114. }
  115. usort($output, function($a, $b) {
  116. if ($a->message->id < $b->message->id) {
  117. return -1;
  118. } else if ($a->message->id == $b->message->id) {
  119. return 0;
  120. } else {
  121. return 1;
  122. }
  123. });
  124. foreach ($output as $msg) {
  125. print json_encode($msg) . "\r\n";
  126. }