twitter.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.work = () => {
  13. const lock = this.lock;
  14. if (this.workInterval < 1)
  15. this.workInterval = 1;
  16. if (lock.feed.length === 0) {
  17. setTimeout(() => {
  18. this.work();
  19. }, this.workInterval * 1000);
  20. return;
  21. }
  22. if (lock.workon >= lock.feed.length)
  23. lock.workon = 0;
  24. if (!lock.threads[lock.feed[lock.workon]] ||
  25. !lock.threads[lock.feed[lock.workon]].subscribers ||
  26. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  27. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  28. delete lock.threads[lock.feed[lock.workon]];
  29. lock.feed.splice(lock.workon, 1);
  30. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  31. this.work();
  32. return;
  33. }
  34. logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
  35. const promise = new Promise(resolve => {
  36. let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  37. if (match) {
  38. const config = {
  39. owner_screen_name: match[1],
  40. slug: match[2],
  41. };
  42. const offset = lock.threads[lock.feed[lock.workon]].offset;
  43. if (offset > 0)
  44. config.since_id = offset;
  45. this.client.get('lists/statuses', config, (error, tweets, response) => {
  46. if (error) {
  47. logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  48. }
  49. resolve(tweets);
  50. });
  51. }
  52. else {
  53. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  54. if (match) {
  55. const config = {
  56. screen_name: match[1],
  57. exclude_replies: false,
  58. };
  59. const offset = lock.threads[lock.feed[lock.workon]].offset;
  60. if (offset > 0)
  61. config.since_id = offset;
  62. this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
  63. if (error) {
  64. logger.error(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  65. }
  66. resolve(tweets);
  67. });
  68. }
  69. }
  70. });
  71. promise.then((tweets) => {
  72. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
  73. if (tweets && tweets.length === 0) {
  74. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  75. return;
  76. }
  77. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  78. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  79. return;
  80. }
  81. if (lock.threads[lock.feed[lock.workon]].offset === 0)
  82. tweets.splice(1);
  83. return webshot_1.default(tweets, msg => {
  84. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  85. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  86. this.bot.bot('send_msg', {
  87. message_type: subscriber.chatType,
  88. user_id: subscriber.chatID,
  89. group_id: subscriber.chatID,
  90. discuss_id: subscriber.chatID,
  91. message: msg,
  92. });
  93. });
  94. }, this.webshotDelay)
  95. .then(() => {
  96. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  97. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  98. });
  99. })
  100. .then(() => {
  101. lock.workon++;
  102. let timeout = this.workInterval * 1000 / lock.feed.length;
  103. if (timeout < 1000)
  104. timeout = 1000;
  105. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  106. setTimeout(() => {
  107. this.work();
  108. }, timeout);
  109. });
  110. };
  111. this.client = new Twitter({
  112. consumer_key: opt.consumer_key,
  113. consumer_secret: opt.consumer_secret,
  114. access_token_key: opt.access_token_key,
  115. access_token_secret: opt.access_token_secret,
  116. });
  117. this.lockfile = opt.lockfile;
  118. this.lock = opt.lock;
  119. this.workInterval = opt.workInterval;
  120. this.bot = opt.bot;
  121. this.webshotDelay = opt.webshotDelay;
  122. }
  123. }
  124. exports.default = default_1;