fakestream.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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');
  28. $helptext = <<<ENDOFHELP
  29. USAGE: fakestream.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. Attempts a User Stream connection to Twitter as the given user, dumping
  34. data as it comes.
  35. ENDOFHELP;
  36. require_once INSTALLDIR.'/scripts/commandline.inc';
  37. if (have_option('n')) {
  38. $nickname = get_option_value('n');
  39. } elseif (have_option('nick')) {
  40. $nickname = get_option_value('nickname');
  41. } elseif (have_option('all')) {
  42. $nickname = null;
  43. } else {
  44. show_help($helptext);
  45. exit(0);
  46. }
  47. /**
  48. *
  49. * @param User $user
  50. * @return TwitterOAuthClient
  51. */
  52. function twitterAuthForUser(User $user)
  53. {
  54. $flink = Foreign_link::getByUserID($user->id, TWITTER_SERVICE);
  55. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  56. if (!$token) {
  57. throw new ServerException("No Twitter OAuth credentials for this user.");
  58. }
  59. return new TwitterOAuthClient($token->key, $token->secret);
  60. }
  61. /**
  62. * Emulate the line-by-line output...
  63. *
  64. * @param Foreign_link $flink
  65. * @param mixed $data
  66. */
  67. function dumpMessage($flink, $data)
  68. {
  69. $msg = prepMessage($flink, $data);
  70. print json_encode($msg) . "\r\n";
  71. }
  72. function prepMessage($flink, $data)
  73. {
  74. $msg->for_user = $flink->foreign_id;
  75. $msg->message = $data;
  76. return $msg;
  77. }
  78. if (have_option('all')) {
  79. $users = array();
  80. $flink = new Foreign_link();
  81. $flink->service = TWITTER_SERVICE;
  82. $flink->find();
  83. while ($flink->fetch()) {
  84. if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
  85. FOREIGN_NOTICE_RECV) {
  86. $users[] = $flink->user_id;
  87. }
  88. }
  89. } else {
  90. $user = User::getKV('nickname', $nickname);
  91. $users = array($user->id);
  92. }
  93. $output = array();
  94. foreach ($users as $id) {
  95. $user = User::getKV('id', $id);
  96. if (!$user) {
  97. throw new Exception("No user for id $id");
  98. }
  99. $auth = twitterAuthForUser($user);
  100. $flink = Foreign_link::getByUserID(
  101. $user->id,
  102. TWITTER_SERVICE
  103. );
  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. } elseif ($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. }