twitter.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 mirai_1 = require("./mirai");
  8. const webshot_1 = require("./webshot");
  9. const logger = loggers_1.getLogger('twitter');
  10. class default_1 {
  11. constructor(opt) {
  12. this.launch = () => {
  13. this.webshot = new webshot_1.default(this.webshotOutDir, this.mode, () => setTimeout(this.work, this.workInterval * 1000));
  14. };
  15. this.work = () => {
  16. const lock = this.lock;
  17. if (this.workInterval < 1)
  18. this.workInterval = 1;
  19. if (lock.feed.length === 0) {
  20. setTimeout(() => {
  21. this.work();
  22. }, this.workInterval * 1000);
  23. return;
  24. }
  25. if (lock.workon >= lock.feed.length)
  26. lock.workon = 0;
  27. if (!lock.threads[lock.feed[lock.workon]] ||
  28. !lock.threads[lock.feed[lock.workon]].subscribers ||
  29. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  30. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  31. delete lock.threads[lock.feed[lock.workon]];
  32. lock.feed.splice(lock.workon, 1);
  33. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  34. this.work();
  35. return;
  36. }
  37. logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
  38. const promise = new Promise(resolve => {
  39. let match = lock.feed[lock.workon].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 = lock.feed[lock.workon].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[lock.feed[lock.workon]].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 ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  69. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  70. logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  71. this.bot.sendTo(subscriber, `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`).catch();
  72. });
  73. }
  74. else {
  75. logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${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 ${lock.feed[lock.workon]}`);
  86. if (!tweets || tweets.length === 0) {
  87. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  88. return;
  89. }
  90. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  91. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  92. return;
  93. }
  94. if (lock.threads[lock.feed[lock.workon]].offset === 0)
  95. tweets.splice(1);
  96. const maxCount = 3;
  97. let sendTimeout = 10000;
  98. const retryTimeout = 1500;
  99. const ordinal = (n) => {
  100. switch ((~~(n / 10) % 10 === 1) ? 0 : n % 10) {
  101. case 1:
  102. return `${n}st`;
  103. case 2:
  104. return `${n}nd`;
  105. case 3:
  106. return `${n}rd`;
  107. default:
  108. return `${n}th`;
  109. }
  110. };
  111. const sendTweets = (msg, text, author) => {
  112. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  113. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  114. const retry = (reason, count) => {
  115. if (count <= maxCount)
  116. sendTimeout *= (count + 2) / (count + 1);
  117. setTimeout(() => {
  118. msg.forEach((message, pos) => {
  119. if (count > maxCount && message.type === 'Image') {
  120. if (pos === 0) {
  121. logger.warn(`${count - 1} consecutive failures sending webshot, trying plain text instead...`);
  122. msg[pos] = mirai_1.MiraiMessage.Plain(author + text);
  123. }
  124. else {
  125. msg[pos] = mirai_1.MiraiMessage.Plain(`[失败的图片:${message.path}]`);
  126. }
  127. }
  128. });
  129. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  130. this.bot.sendTo(subscriber, msg, sendTimeout).catch(error => retry(error, count + 1));
  131. }, retryTimeout);
  132. };
  133. this.bot.sendTo(subscriber, msg, sendTimeout).catch(error => retry(error, 1));
  134. });
  135. };
  136. return this.webshot(tweets, sendTweets, this.webshotDelay)
  137. .then(() => {
  138. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  139. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  140. });
  141. })
  142. .then(() => {
  143. lock.workon++;
  144. let timeout = this.workInterval * 1000 / lock.feed.length;
  145. if (timeout < 1000)
  146. timeout = 1000;
  147. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  148. setTimeout(() => {
  149. this.work();
  150. }, timeout);
  151. });
  152. };
  153. this.client = new Twitter({
  154. consumer_key: opt.consumer_key,
  155. consumer_secret: opt.consumer_secret,
  156. access_token_key: opt.access_token_key,
  157. access_token_secret: opt.access_token_secret,
  158. });
  159. this.lockfile = opt.lockfile;
  160. this.lock = opt.lock;
  161. this.workInterval = opt.workInterval;
  162. this.bot = opt.bot;
  163. this.webshotDelay = opt.webshotDelay;
  164. this.webshotOutDir = opt.webshotOutDir;
  165. this.mode = opt.mode;
  166. }
  167. }
  168. exports.default = default_1;