twitter.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const log4js = require("log4js");
  5. const path = require("path");
  6. const Twitter = require("twitter");
  7. const webshot_1 = require("./webshot");
  8. const logger = log4js.getLogger('twitter');
  9. logger.level = global.loglevel;
  10. class default_1 {
  11. constructor(opt) {
  12. this.launch = () => {
  13. this.webshot = new webshot_1.default(() => 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. };
  47. endpoint = 'lists/statuses';
  48. }
  49. else {
  50. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  51. if (match) {
  52. config = {
  53. screen_name: match[1],
  54. exclude_replies: false,
  55. };
  56. endpoint = 'statuses/user_timeline';
  57. }
  58. }
  59. if (endpoint) {
  60. const offset = lock.threads[lock.feed[lock.workon]].offset;
  61. if (offset > 0)
  62. config.since_id = offset;
  63. this.client.get(endpoint, config, (error, tweets, response) => {
  64. if (error) {
  65. if (error instanceof Array && error.length > 0 && error[0].code === 34) {
  66. logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  67. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  68. logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  69. this.bot.bot('send_msg', {
  70. message_type: subscriber.chatType,
  71. user_id: subscriber.chatID,
  72. group_id: subscriber.chatID,
  73. discuss_id: subscriber.chatID,
  74. message: `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`,
  75. });
  76. });
  77. }
  78. else {
  79. logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  80. }
  81. resolve();
  82. }
  83. else
  84. resolve(tweets);
  85. });
  86. }
  87. });
  88. promise.then((tweets) => {
  89. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
  90. if (!tweets || tweets.length === 0) {
  91. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  92. return;
  93. }
  94. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  95. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  96. return;
  97. }
  98. if (lock.threads[lock.feed[lock.workon]].offset === 0)
  99. tweets.splice(1);
  100. return this.webshot(tweets, msg => {
  101. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  102. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  103. this.bot.bot('send_msg', {
  104. message_type: subscriber.chatType,
  105. user_id: subscriber.chatID,
  106. group_id: subscriber.chatID,
  107. discuss_id: subscriber.chatID,
  108. message: msg,
  109. });
  110. });
  111. }, this.webshotDelay)
  112. .then(() => {
  113. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  114. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  115. });
  116. })
  117. .then(() => {
  118. lock.workon++;
  119. let timeout = this.workInterval * 1000 / lock.feed.length;
  120. if (timeout < 1000)
  121. timeout = 1000;
  122. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  123. setTimeout(() => {
  124. this.work();
  125. }, timeout);
  126. });
  127. };
  128. this.client = new Twitter({
  129. consumer_key: opt.consumer_key,
  130. consumer_secret: opt.consumer_secret,
  131. access_token_key: opt.access_token_key,
  132. access_token_secret: opt.access_token_secret,
  133. });
  134. this.lockfile = opt.lockfile;
  135. this.lock = opt.lock;
  136. this.workInterval = opt.workInterval;
  137. this.bot = opt.bot;
  138. this.webshotDelay = opt.webshotDelay;
  139. }
  140. }
  141. exports.default = default_1;