twitter.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  69. return;
  70. }
  71. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  72. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  73. return;
  74. }
  75. if (lock.threads[lock.feed[lock.workon]].offset === 0)
  76. tweets.splice(1);
  77. return webshot_1.default(tweets, msg => {
  78. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  79. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  80. this.bot.bot('send_msg', {
  81. message_type: subscriber.chatType,
  82. user_id: subscriber.chatID,
  83. group_id: subscriber.chatID,
  84. discuss_id: subscriber.chatID,
  85. message: msg,
  86. });
  87. });
  88. }, this.webshotDelay)
  89. .then(() => {
  90. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  91. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  92. });
  93. })
  94. .then(() => {
  95. lock.workon++;
  96. let timeout = this.workInterval * 1000 / lock.feed.length;
  97. if (timeout < 1000)
  98. timeout = 1000;
  99. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  100. setTimeout(() => {
  101. this.work();
  102. }, timeout);
  103. });
  104. };
  105. this.client = new Twitter({
  106. consumer_key: opt.consumer_key,
  107. consumer_secret: opt.consumer_secret,
  108. access_token_key: opt.access_token_key,
  109. access_token_secret: opt.access_token_secret,
  110. });
  111. this.lockfile = opt.lockfile;
  112. this.lock = opt.lock;
  113. this.workInterval = opt.workInterval;
  114. this.bot = opt.bot;
  115. this.webshotDelay = opt.webshotDelay;
  116. }
  117. }
  118. exports.default = default_1;