twitter.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const path = require("path");
  5. const Twitter = require("twitter");
  6. const loggers_1 = require("./loggers");
  7. const webshot_1 = require("./webshot");
  8. const logger = loggers_1.getLogger('twitter');
  9. class default_1 {
  10. constructor(opt) {
  11. this.launch = () => {
  12. this.webshot = new webshot_1.default(this.mode, () => setTimeout(this.work, this.workInterval * 1000));
  13. };
  14. this.work = () => {
  15. const lock = this.lock;
  16. if (this.workInterval < 1)
  17. this.workInterval = 1;
  18. if (lock.feed.length === 0) {
  19. setTimeout(() => {
  20. this.work();
  21. }, this.workInterval * 1000);
  22. return;
  23. }
  24. if (lock.workon >= lock.feed.length)
  25. lock.workon = 0;
  26. if (!lock.threads[lock.feed[lock.workon]] ||
  27. !lock.threads[lock.feed[lock.workon]].subscribers ||
  28. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  29. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  30. delete lock.threads[lock.feed[lock.workon]];
  31. lock.feed.splice(lock.workon, 1);
  32. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  33. this.work();
  34. return;
  35. }
  36. const currentFeed = lock.feed[lock.workon];
  37. logger.debug(`pulling feed ${currentFeed}`);
  38. const promise = new Promise(resolve => {
  39. let match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  40. let config;
  41. let endpoint;
  42. if (match) {
  43. config = {
  44. owner_screen_name: match[1],
  45. slug: match[2],
  46. tweet_mode: 'extended',
  47. };
  48. endpoint = 'lists/statuses';
  49. }
  50. else {
  51. match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)/);
  52. if (match) {
  53. config = {
  54. screen_name: match[1],
  55. exclude_replies: false,
  56. tweet_mode: 'extended',
  57. };
  58. endpoint = 'statuses/user_timeline';
  59. }
  60. }
  61. if (endpoint) {
  62. const offset = lock.threads[currentFeed].offset;
  63. if (offset > 0)
  64. config.since_id = offset;
  65. this.client.get(endpoint, config, (error, tweets, response) => {
  66. if (error) {
  67. if (error instanceof Array && error.length > 0 && error[0].code === 34) {
  68. logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  69. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  70. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  71. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  72. });
  73. }
  74. else {
  75. logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  76. }
  77. resolve();
  78. }
  79. else
  80. resolve(tweets);
  81. });
  82. }
  83. });
  84. promise.then((tweets) => {
  85. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
  86. const currentThread = lock.threads[currentFeed];
  87. const updateDate = () => currentThread.updatedAt = new Date().toString();
  88. if (!tweets || tweets.length === 0) {
  89. updateDate();
  90. return;
  91. }
  92. const topOfFeed = tweets[0].id_str;
  93. const updateOffset = () => currentThread.offset = topOfFeed;
  94. if (currentThread.offset === '-1') {
  95. updateOffset();
  96. return;
  97. }
  98. if (currentThread.offset === '0')
  99. tweets.splice(1);
  100. const maxCount = 3;
  101. const uploadTimeout = 10000;
  102. const retryInterval = 1500;
  103. const ordinal = (n) => {
  104. switch ((~~(n / 10) % 10 === 1) ? 0 : n % 10) {
  105. case 1:
  106. return `${n}st`;
  107. case 2:
  108. return `${n}nd`;
  109. case 3:
  110. return `${n}rd`;
  111. default:
  112. return `${n}th`;
  113. }
  114. };
  115. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  116. const retry = (reason, count) => {
  117. setTimeout(() => {
  118. let terminate = false;
  119. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  120. if (!terminate)
  121. doWork().then(resolve).catch(error => retry(error, count + 1));
  122. }, retryInterval);
  123. };
  124. doWork().then(resolve).catch(error => retry(error, 1));
  125. });
  126. const uploader = (message, lastResort) => {
  127. let timeout = uploadTimeout;
  128. return retryOnError(() => this.bot.uploadPic(message, timeout).then(() => message), (_, count, terminate) => {
  129. if (count <= maxCount) {
  130. timeout *= (count + 2) / (count + 1);
  131. logger.warn(`retry uploading for the ${ordinal(count)} time...`);
  132. }
  133. else {
  134. logger.warn(`${count - 1} consecutive failures while uploading, trying plain text instead...`);
  135. terminate(lastResort());
  136. }
  137. });
  138. };
  139. const sendTweets = (msg, text, author) => {
  140. currentThread.subscribers.forEach(subscriber => {
  141. logger.info(`pushing data of thread ${currentFeed} to ${JSON.stringify(subscriber)}`);
  142. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  143. if (count <= maxCount) {
  144. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  145. }
  146. else {
  147. logger.warn(`${count - 1} consecutive failures while sending` +
  148. 'message chain, trying plain text instead...');
  149. terminate(this.bot.sendTo(subscriber, author + text));
  150. }
  151. });
  152. });
  153. };
  154. return this.webshot(tweets, uploader, sendTweets, this.webshotDelay)
  155. .then(updateDate).then(updateOffset);
  156. })
  157. .then(() => {
  158. lock.workon++;
  159. let timeout = this.workInterval * 1000 / lock.feed.length;
  160. if (timeout < 1000)
  161. timeout = 1000;
  162. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  163. setTimeout(() => {
  164. this.work();
  165. }, timeout);
  166. });
  167. };
  168. this.client = new Twitter({
  169. consumer_key: opt.consumer_key,
  170. consumer_secret: opt.consumer_secret,
  171. access_token_key: opt.access_token_key,
  172. access_token_secret: opt.access_token_secret,
  173. });
  174. this.lockfile = opt.lockfile;
  175. this.lock = opt.lock;
  176. this.workInterval = opt.workInterval;
  177. this.bot = opt.bot;
  178. this.webshotDelay = opt.webshotDelay;
  179. this.mode = opt.mode;
  180. }
  181. }
  182. exports.default = default_1;