twitter.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. resolve(tweets);
  47. });
  48. }
  49. else {
  50. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  51. if (match) {
  52. const config = {
  53. screen_name: match[1],
  54. exclude_replies: false,
  55. };
  56. const offset = lock.threads[lock.feed[lock.workon]].offset;
  57. if (offset > 0)
  58. config.since_id = offset;
  59. this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
  60. resolve(tweets);
  61. });
  62. }
  63. }
  64. });
  65. promise.then((tweets) => {
  66. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
  67. if (tweets.length === 0)
  68. return;
  69. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  70. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  71. return;
  72. }
  73. if (lock.threads[lock.feed[lock.workon]].offset === 0)
  74. tweets.splice(1);
  75. return webshot_1.default(tweets, msg => {
  76. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  77. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  78. this.bot.bot('send_msg', {
  79. message_type: subscriber.chatType,
  80. user_id: subscriber.chatID,
  81. group_id: subscriber.chatID,
  82. discuss_id: subscriber.chatID,
  83. message: msg,
  84. });
  85. });
  86. }, this.webshotDelay)
  87. .then(() => {
  88. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  89. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  90. });
  91. })
  92. .then(() => {
  93. lock.workon++;
  94. let timeout = this.workInterval * 1000 / lock.feed.length;
  95. if (timeout < 1000)
  96. timeout = 1000;
  97. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  98. setTimeout(() => {
  99. this.work();
  100. }, timeout);
  101. });
  102. };
  103. this.client = new Twitter({
  104. consumer_key: opt.consumer_key,
  105. consumer_secret: opt.consumer_secret,
  106. access_token_key: opt.access_token_key,
  107. access_token_secret: opt.access_token_secret,
  108. });
  109. this.lockfile = opt.lockfile;
  110. this.lock = opt.lock;
  111. this.workInterval = opt.workInterval;
  112. this.bot = opt.bot;
  113. this.webshotDelay = opt.webshotDelay;
  114. }
  115. }
  116. exports.default = default_1;