twitter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = 'info';
  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.error(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  28. lock.feed.splice(lock.workon, 1);
  29. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  30. this.work();
  31. return;
  32. }
  33. const promise = new Promise(resolve => {
  34. let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  35. if (match) {
  36. const config = {
  37. owner_screen_name: match[1],
  38. slug: match[2],
  39. };
  40. const offset = lock.threads[lock.feed[lock.workon]].offset;
  41. if (offset > 0)
  42. config.since_id = offset;
  43. this.client.get('lists/statuses', config, (error, tweets, response) => {
  44. resolve(tweets);
  45. });
  46. }
  47. else {
  48. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  49. if (match) {
  50. const config = {
  51. screen_name: match[1],
  52. exclude_replies: false,
  53. };
  54. const offset = lock.threads[lock.feed[lock.workon]].offset;
  55. if (offset > 0)
  56. config.since_id = offset;
  57. this.client.get('statuses/user_timeline', config, (error, tweets, response) => {
  58. resolve(tweets);
  59. });
  60. }
  61. }
  62. });
  63. promise.then((tweets) => {
  64. if (tweets.length === 0)
  65. return;
  66. if (lock.threads[lock.feed[lock.workon]].offset !== -1) {
  67. webshot_1.default(tweets, msg => {
  68. lock.threads[lock.feed[lock.workon]].subscribers.forEach(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: msg,
  75. });
  76. });
  77. }, this.webshotDelay);
  78. }
  79. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id;
  80. })
  81. .then(() => {
  82. lock.workon++;
  83. let timeout = this.workInterval * 1000 / lock.feed.length;
  84. if (timeout < 1000)
  85. timeout = 1000;
  86. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  87. setTimeout(() => {
  88. this.work();
  89. }, timeout);
  90. });
  91. };
  92. this.client = new Twitter({
  93. consumer_key: opt.consumer_key,
  94. consumer_secret: opt.consumer_secret,
  95. access_token_key: opt.access_token_key,
  96. access_token_secret: opt.access_token_secret,
  97. });
  98. this.lockfile = opt.lockfile;
  99. this.lock = opt.lock;
  100. this.workInterval = opt.workInterval;
  101. this.bot = opt.bot;
  102. this.webshotDelay = opt.webshotDelay;
  103. }
  104. }
  105. exports.default = default_1;