twitterstatusfetcher.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @copyright 2008-2010 StatusNet, Inc
  19. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  20. */
  21. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  22. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  23. // Tune number of processes and how often to poll Twitter
  24. // XXX: Should these things be in config.php?
  25. define('MAXCHILDREN', 2);
  26. define('POLL_INTERVAL', 70); // in seconds, Twitter API v1.1 says 15 calls every 15 mins
  27. $shortoptions = 'di::';
  28. $longoptions = array('id::', 'debug');
  29. $helptext = <<<END_OF_TRIM_HELP
  30. Batch script for retrieving Twitter messages from foreign service.
  31. -i --id Identity (default 'generic')
  32. -d --debug Debug (lots of log output)
  33. END_OF_TRIM_HELP;
  34. require_once INSTALLDIR . '/scripts/commandline.inc';
  35. require_once INSTALLDIR . '/lib/util/common.php';
  36. require_once INSTALLDIR . '/lib/util/daemon.php';
  37. require_once dirname(__DIR__) . '/twitter.php';
  38. /**
  39. * Fetch statuses from Twitter
  40. *
  41. * Fetches statuses from Twitter and inserts them as notices
  42. *
  43. * NOTE: an Avatar path MUST be set in config.php for this
  44. * script to work, e.g.:
  45. * $config['avatar']['path'] = $config['site']['path'] . '/avatar/';
  46. *
  47. * @todo @fixme @gar Fix the above. For some reason $_path is always empty when
  48. * this script is run, so the default avatar path is always set wrong in
  49. * default.php. Therefore it must be set explicitly in config.php. --Z
  50. *
  51. * @category Twitter
  52. * @package StatusNet
  53. * @author Zach Copley <zach@status.net>
  54. * @author Evan Prodromou <evan@status.net>
  55. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  56. * @link http://status.net/
  57. */
  58. class TwitterStatusFetcher extends ParallelizingDaemon
  59. {
  60. /**
  61. * Constructor
  62. *
  63. * @param string $id the name/id of this daemon
  64. * @param int $interval sleep this long before doing everything again
  65. * @param int $max_children maximum number of child processes at a time
  66. * @param boolean $debug debug output flag
  67. *
  68. * @return void
  69. *
  70. **/
  71. public function __construct(
  72. $id = null,
  73. $interval = 60,
  74. $max_children = 2,
  75. $debug = null
  76. ) {
  77. parent::__construct($id, $interval, $max_children, $debug);
  78. }
  79. /**
  80. * Name of this daemon
  81. *
  82. * @return string Name of the daemon.
  83. */
  84. public function name()
  85. {
  86. return ('twitterstatusfetcher.'.$this->_id);
  87. }
  88. /**
  89. * Find all the Twitter foreign links for users who have requested
  90. * importing of their friends' timelines
  91. *
  92. * @return array flinks an array of Foreign_link objects
  93. */
  94. public function getObjects()
  95. {
  96. global $_DB_DATAOBJECT;
  97. $flink = new Foreign_link();
  98. $conn = &$flink->getDatabaseConnection();
  99. $flink->service = TWITTER_SERVICE;
  100. $flink->orderBy('last_noticesync');
  101. $flink->find();
  102. $flinks = array();
  103. while ($flink->fetch()) {
  104. if (($flink->noticesync & FOREIGN_NOTICE_RECV) ==
  105. FOREIGN_NOTICE_RECV) {
  106. $flinks[] = clone($flink);
  107. common_log(LOG_INFO, "sync: foreign id $flink->foreign_id");
  108. } else {
  109. common_log(LOG_INFO, "nothing to sync");
  110. }
  111. }
  112. $flink->free();
  113. unset($flink);
  114. $conn->disconnect();
  115. unset($_DB_DATAOBJECT['CONNECTIONS']);
  116. return $flinks;
  117. }
  118. // FIXME: make it so we can force a Foreign_link here without colliding with parent
  119. public function childTask($flink)
  120. {
  121. // Each child ps needs its own DB connection
  122. // Note: DataObject::getDatabaseConnection() creates
  123. // a new connection if there isn't one already
  124. $conn = &$flink->getDatabaseConnection();
  125. $this->getTimeline($flink, 'home_timeline');
  126. $this->getTimeline($flink, 'mentions_timeline');
  127. $flink->last_friendsync = common_sql_now();
  128. $flink->update();
  129. $conn->disconnect();
  130. // XXX: Couldn't find a less brutal way to blow
  131. // away a cached connection
  132. global $_DB_DATAOBJECT;
  133. unset($_DB_DATAOBJECT['CONNECTIONS']);
  134. }
  135. public function getTimeline(Foreign_link $flink, $timelineUri = 'home_timeline')
  136. {
  137. common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
  138. ' timeline for Twitter user ' . $flink->foreign_id);
  139. $client = null;
  140. if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
  141. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  142. $client = new TwitterOAuthClient($token->key, $token->secret);
  143. common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
  144. } else {
  145. common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
  146. $flink->foreign_id . " since not OAuth.");
  147. }
  148. $timeline = null;
  149. $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
  150. common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
  151. $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
  152. try {
  153. $timeline = $client->statusesTimeline($lastId, $timelineUri);
  154. } catch (Exception $e) {
  155. common_log(LOG_ERR, $this->name() .
  156. ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
  157. ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
  158. }
  159. if (empty($timeline)) {
  160. common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
  161. return;
  162. }
  163. common_log(LOG_INFO, $this->name() .
  164. ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
  165. ' - for user ' . $flink->user_id);
  166. if (!empty($timeline)) {
  167. $qm = QueueManager::get();
  168. // Reverse to preserve order
  169. foreach (array_reverse($timeline) as $status) {
  170. $data = array(
  171. 'status' => $status,
  172. 'for_user' => $flink->foreign_id,
  173. );
  174. $qm->enqueue($data, 'tweetin');
  175. }
  176. $lastId = twitter_id($timeline[0]);
  177. Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
  178. common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
  179. $timelineUri . "'");
  180. }
  181. // Okay, record the time we synced with Twitter for posterity
  182. $flink->last_noticesync = common_sql_now();
  183. $flink->update();
  184. }
  185. }
  186. $id = null;
  187. $debug = null;
  188. if (have_option('i')) {
  189. $id = get_option_value('i');
  190. } elseif (have_option('--id')) {
  191. $id = get_option_value('--id');
  192. } elseif (count($args) > 0) {
  193. $id = $args[0];
  194. } else {
  195. $id = null;
  196. }
  197. if (have_option('d') || have_option('debug')) {
  198. $debug = true;
  199. }
  200. $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
  201. $fetcher->runOnce();