twitter.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import * as Twitter from 'twitter';
  4. import { getLogger } from './loggers';
  5. import QQBot from './mirai';
  6. import Webshot from './webshot';
  7. interface IWorkerOption {
  8. lock: ILock;
  9. lockfile: string;
  10. bot: QQBot;
  11. workInterval: number;
  12. webshotDelay: number;
  13. webshotOutDir: string;
  14. consumer_key: string;
  15. consumer_secret: string;
  16. access_token_key: string;
  17. access_token_secret: string;
  18. mode: number;
  19. }
  20. const logger = getLogger('twitter');
  21. export default class {
  22. private client;
  23. private lock: ILock;
  24. private lockfile: string;
  25. private workInterval: number;
  26. private bot: QQBot;
  27. private webshotDelay: number;
  28. private webshotOutDir: string;
  29. private webshot: Webshot;
  30. private mode: number;
  31. constructor(opt: IWorkerOption) {
  32. this.client = new Twitter({
  33. consumer_key: opt.consumer_key,
  34. consumer_secret: opt.consumer_secret,
  35. access_token_key: opt.access_token_key,
  36. access_token_secret: opt.access_token_secret,
  37. });
  38. this.lockfile = opt.lockfile;
  39. this.lock = opt.lock;
  40. this.workInterval = opt.workInterval;
  41. this.bot = opt.bot;
  42. this.webshotDelay = opt.webshotDelay;
  43. this.webshotOutDir = opt.webshotOutDir;
  44. this.mode = opt.mode;
  45. }
  46. public launch = () => {
  47. this.webshot = new Webshot(
  48. this.webshotOutDir,
  49. this.mode,
  50. () => setTimeout(this.work, this.workInterval * 1000)
  51. );
  52. }
  53. public work = () => {
  54. const lock = this.lock;
  55. if (this.workInterval < 1) this.workInterval = 1;
  56. if (lock.feed.length === 0) {
  57. setTimeout(() => {
  58. this.work();
  59. }, this.workInterval * 1000);
  60. return;
  61. }
  62. if (lock.workon >= lock.feed.length) lock.workon = 0;
  63. if (!lock.threads[lock.feed[lock.workon]] ||
  64. !lock.threads[lock.feed[lock.workon]].subscribers ||
  65. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  66. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  67. delete lock.threads[lock.feed[lock.workon]];
  68. lock.feed.splice(lock.workon, 1);
  69. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  70. this.work();
  71. return;
  72. }
  73. logger.debug(`pulling feed ${lock.feed[lock.workon]}`);
  74. const promise = new Promise(resolve => {
  75. let match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  76. let config: any;
  77. let endpoint: string;
  78. if (match) {
  79. config = {
  80. owner_screen_name: match[1],
  81. slug: match[2],
  82. tweet_mode: 'extended',
  83. };
  84. endpoint = 'lists/statuses';
  85. } else {
  86. match = lock.feed[lock.workon].match(/https:\/\/twitter.com\/([^\/]+)/);
  87. if (match) {
  88. config = {
  89. screen_name: match[1],
  90. exclude_replies: false,
  91. tweet_mode: 'extended',
  92. };
  93. endpoint = 'statuses/user_timeline';
  94. }
  95. }
  96. if (endpoint) {
  97. const offset = lock.threads[lock.feed[lock.workon]].offset;
  98. if (offset > 0) config.since_id = offset;
  99. this.client.get(endpoint, config, (error, tweets, response) => {
  100. if (error) {
  101. if (error instanceof Array && error.length > 0 && error[0].code === 34) {
  102. logger.warn(`error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  103. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  104. logger.info(`sending notfound message of ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  105. this.bot.sendTo(subscriber, `链接 ${lock.feed[lock.workon]} 指向的用户或列表不存在,请退订。`);
  106. });
  107. } else {
  108. logger.error(`unhandled error on fetching tweets for ${lock.feed[lock.workon]}: ${JSON.stringify(error)}`);
  109. }
  110. resolve();
  111. } else resolve(tweets);
  112. });
  113. }
  114. });
  115. promise.then((tweets: any) => {
  116. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${lock.feed[lock.workon]}`);
  117. if (!tweets || tweets.length === 0) {
  118. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  119. return;
  120. }
  121. if (lock.threads[lock.feed[lock.workon]].offset === -1) {
  122. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  123. return;
  124. }
  125. if (lock.threads[lock.feed[lock.workon]].offset === 0) tweets.splice(1);
  126. return (this.webshot as any)(tweets, msg => {
  127. lock.threads[lock.feed[lock.workon]].subscribers.forEach(subscriber => {
  128. logger.info(`pushing data of thread ${lock.feed[lock.workon]} to ${JSON.stringify(subscriber)}`);
  129. this.bot.sendTo(subscriber, msg);
  130. });
  131. }, this.webshotDelay)
  132. .then(() => {
  133. lock.threads[lock.feed[lock.workon]].offset = tweets[0].id_str;
  134. lock.threads[lock.feed[lock.workon]].updatedAt = new Date().toString();
  135. });
  136. })
  137. .then(() => {
  138. lock.workon++;
  139. let timeout = this.workInterval * 1000 / lock.feed.length;
  140. if (timeout < 1000) timeout = 1000;
  141. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  142. setTimeout(() => {
  143. this.work();
  144. }, timeout);
  145. });
  146. }
  147. }