twitterstatusfetcher.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. foreach ($_DB_DATAOBJECT['CONNECTIONS'] as $k => $v) {
  116. if ($v === $conn) {
  117. unset($_DB_DATAOBJECT['CONNECTIONS'][$k]);
  118. }
  119. }
  120. return $flinks;
  121. }
  122. // FIXME: make it so we can force a Foreign_link here without colliding with parent
  123. public function childTask($flink)
  124. {
  125. // Each child ps needs its own DB connection
  126. // Note: DataObject::getDatabaseConnection() creates
  127. // a new connection if there isn't one already
  128. $conn = &$flink->getDatabaseConnection();
  129. $this->getTimeline($flink, 'home_timeline');
  130. $this->getTimeline($flink, 'mentions_timeline');
  131. $flink->last_friendsync = common_sql_now();
  132. $flink->update();
  133. $conn->disconnect();
  134. // XXX: Couldn't find a less brutal way to blow
  135. // away a cached connection
  136. global $_DB_DATAOBJECT;
  137. foreach ($_DB_DATAOBJECT['CONNECTIONS'] as $k => $v) {
  138. if ($v === $conn) {
  139. unset($_DB_DATAOBJECT['CONNECTIONS'][$k]);
  140. }
  141. }
  142. }
  143. public function getTimeline(Foreign_link $flink, $timelineUri = 'home_timeline')
  144. {
  145. common_log(LOG_DEBUG, $this->name() . ' - Trying to get ' . $timelineUri .
  146. ' timeline for Twitter user ' . $flink->foreign_id);
  147. $client = null;
  148. if (TwitterOAuthClient::isPackedToken($flink->credentials)) {
  149. $token = TwitterOAuthClient::unpackToken($flink->credentials);
  150. $client = new TwitterOAuthClient($token->key, $token->secret);
  151. common_log(LOG_DEBUG, $this->name() . ' - Grabbing ' . $timelineUri . ' timeline with OAuth.');
  152. } else {
  153. common_log(LOG_ERR, "Skipping " . $timelineUri . " timeline for " .
  154. $flink->foreign_id . " since not OAuth.");
  155. }
  156. $timeline = null;
  157. $lastId = Twitter_synch_status::getLastId($flink->foreign_id, $timelineUri);
  158. common_log(LOG_DEBUG, "Got lastId value '" . $lastId . "' for foreign id '" .
  159. $flink->foreign_id . "' and timeline '" . $timelineUri. "'");
  160. try {
  161. $timeline = $client->statusesTimeline($lastId, $timelineUri);
  162. } catch (Exception $e) {
  163. common_log(LOG_ERR, $this->name() .
  164. ' - Unable to get ' . $timelineUri . ' timeline for user ' . $flink->user_id .
  165. ' - code: ' . $e->getCode() . 'msg: ' . $e->getMessage());
  166. }
  167. if (empty($timeline)) {
  168. common_log(LOG_DEBUG, $this->name() . " - Empty '" . $timelineUri . "' timeline.");
  169. return;
  170. }
  171. common_log(LOG_INFO, $this->name() .
  172. ' - Retrieved ' . sizeof($timeline) . ' statuses from ' . $timelineUri . ' timeline' .
  173. ' - for user ' . $flink->user_id);
  174. if (!empty($timeline)) {
  175. $qm = QueueManager::get();
  176. // Reverse to preserve order
  177. foreach (array_reverse($timeline) as $status) {
  178. $data = array(
  179. 'status' => $status,
  180. 'for_user' => $flink->foreign_id,
  181. );
  182. $qm->enqueue($data, 'tweetin');
  183. }
  184. $lastId = twitter_id($timeline[0]);
  185. Twitter_synch_status::setLastId($flink->foreign_id, $timelineUri, $lastId);
  186. common_debug("Set lastId value '$lastId' for foreign id '{$flink->foreign_id}' and timeline '" .
  187. $timelineUri . "'");
  188. }
  189. // Okay, record the time we synced with Twitter for posterity
  190. $flink->last_noticesync = common_sql_now();
  191. $flink->update();
  192. }
  193. }
  194. $id = null;
  195. $debug = null;
  196. if (have_option('i')) {
  197. $id = get_option_value('i');
  198. } elseif (have_option('--id')) {
  199. $id = get_option_value('--id');
  200. } elseif (count($args) > 0) {
  201. $id = $args[0];
  202. } else {
  203. $id = null;
  204. }
  205. if (have_option('d') || have_option('debug')) {
  206. $debug = true;
  207. }
  208. $fetcher = new TwitterStatusFetcher($id, POLL_INTERVAL, MAXCHILDREN, $debug);
  209. $fetcher->runOnce();