twitter.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.sendTweet = exports.bigNumPlus = exports.ScreenNameNormalizer = void 0;
  13. const fs = require("fs");
  14. const path = require("path");
  15. const Twitter = require("twitter");
  16. const loggers_1 = require("./loggers");
  17. const webshot_1 = require("./webshot");
  18. class ScreenNameNormalizer {
  19. static normalizeLive(username) {
  20. return __awaiter(this, void 0, void 0, function* () {
  21. if (this._queryUser) {
  22. return yield this._queryUser(username)
  23. .catch((err) => {
  24. if (err[0].code !== 50) {
  25. logger.warn(`error looking up user: ${err[0].message}`);
  26. return username;
  27. }
  28. return null;
  29. });
  30. }
  31. return this.normalize(username);
  32. });
  33. }
  34. }
  35. exports.ScreenNameNormalizer = ScreenNameNormalizer;
  36. ScreenNameNormalizer.normalize = (username) => username.toLowerCase().replace(/^@/, '');
  37. exports.bigNumPlus = (num1, num2) => {
  38. const split = (num) => num.replace(/^(-?)(\d+)(\d{15})$/, '$1$2,$1$3')
  39. .replace(/^([^,]*)$/, '0,$1').split(',')
  40. .map(Number);
  41. let [high, low] = [split(num1), split(num2)].reduce((a, b) => [a[0] + b[0], a[1] + b[1]]);
  42. const [highSign, lowSign] = [high, low].map(Math.sign);
  43. if (highSign === 0)
  44. return low.toString();
  45. if (highSign !== lowSign) {
  46. [high, low] = [high - highSign, low - lowSign * Math.pow(10, 15)];
  47. }
  48. else {
  49. [high, low] = [high + ~~(low / Math.pow(10, 15)), low % Math.pow(10, 15)];
  50. }
  51. return `${high}${Math.abs(low).toString().padStart(15, '0')}`;
  52. };
  53. exports.sendTweet = (id, receiver) => {
  54. throw Error();
  55. };
  56. const logger = loggers_1.getLogger('twitter');
  57. const maxTrials = 3;
  58. const uploadTimeout = 10000;
  59. const retryInterval = 1500;
  60. const ordinal = (n) => {
  61. switch ((~~(n / 10) % 10 === 1) ? 0 : n % 10) {
  62. case 1:
  63. return `${n}st`;
  64. case 2:
  65. return `${n}nd`;
  66. case 3:
  67. return `${n}rd`;
  68. default:
  69. return `${n}th`;
  70. }
  71. };
  72. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  73. const retry = (reason, count) => {
  74. setTimeout(() => {
  75. let terminate = false;
  76. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  77. if (!terminate)
  78. doWork().then(resolve).catch(error => retry(error, count + 1));
  79. }, retryInterval);
  80. };
  81. doWork().then(resolve).catch(error => retry(error, 1));
  82. });
  83. class default_1 {
  84. constructor(opt) {
  85. this.launch = () => {
  86. this.webshot = new webshot_1.default(this.mode, () => setTimeout(this.work, this.workInterval * 1000));
  87. };
  88. this.queryUser = (username) => this.client.get('users/show', { screen_name: username })
  89. .then((user) => user.screen_name);
  90. this.workOnTweets = (tweets, sendTweets) => {
  91. const uploader = (message, lastResort) => {
  92. let timeout = uploadTimeout;
  93. return retryOnError(() => this.bot.uploadPic(message, timeout).then(() => message), (_, count, terminate) => {
  94. if (count <= maxTrials) {
  95. timeout *= (count + 2) / (count + 1);
  96. logger.warn(`retry uploading for the ${ordinal(count)} time...`);
  97. }
  98. else {
  99. logger.warn(`${count - 1} consecutive failures while uploading, trying plain text instead...`);
  100. terminate(lastResort());
  101. }
  102. });
  103. };
  104. return this.webshot(tweets, uploader, sendTweets, this.webshotDelay);
  105. };
  106. this.getTweet = (id, sender) => {
  107. const endpoint = 'statuses/show';
  108. const config = {
  109. id,
  110. tweet_mode: 'extended',
  111. };
  112. return this.client.get(endpoint, config)
  113. .then((tweet) => {
  114. logger.debug(`api returned tweet ${JSON.stringify(tweet)} for query id=${id}`);
  115. return this.workOnTweets([tweet], sender);
  116. });
  117. };
  118. this.sendTweets = (source, ...to) => (msg, text, author) => {
  119. to.forEach(subscriber => {
  120. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  121. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  122. if (count <= maxTrials) {
  123. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  124. }
  125. else {
  126. logger.warn(`${count - 1} consecutive failures while sending` +
  127. 'message chain, trying plain text instead...');
  128. terminate(this.bot.sendTo(subscriber, author + text));
  129. }
  130. });
  131. });
  132. };
  133. this.work = () => {
  134. const lock = this.lock;
  135. if (this.workInterval < 1)
  136. this.workInterval = 1;
  137. if (lock.feed.length === 0) {
  138. setTimeout(() => {
  139. this.work();
  140. }, this.workInterval * 1000);
  141. return;
  142. }
  143. if (lock.workon >= lock.feed.length)
  144. lock.workon = 0;
  145. if (!lock.threads[lock.feed[lock.workon]] ||
  146. !lock.threads[lock.feed[lock.workon]].subscribers ||
  147. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  148. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  149. delete lock.threads[lock.feed[lock.workon]];
  150. lock.feed.splice(lock.workon, 1);
  151. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  152. this.work();
  153. return;
  154. }
  155. const currentFeed = lock.feed[lock.workon];
  156. logger.debug(`pulling feed ${currentFeed}`);
  157. const promise = new Promise(resolve => {
  158. let match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/);
  159. let config;
  160. let endpoint;
  161. if (match) {
  162. if (match[1] === 'i') {
  163. config = {
  164. list_id: match[2],
  165. tweet_mode: 'extended',
  166. };
  167. }
  168. else {
  169. config = {
  170. owner_screen_name: match[1],
  171. slug: match[2],
  172. tweet_mode: 'extended',
  173. };
  174. }
  175. endpoint = 'lists/statuses';
  176. }
  177. else {
  178. match = currentFeed.match(/https:\/\/twitter.com\/([^\/]+)/);
  179. if (match) {
  180. config = {
  181. screen_name: match[1],
  182. exclude_replies: false,
  183. tweet_mode: 'extended',
  184. };
  185. endpoint = 'statuses/user_timeline';
  186. }
  187. }
  188. if (endpoint) {
  189. const offset = lock.threads[currentFeed].offset;
  190. if (offset > 0)
  191. config.since_id = offset;
  192. if (offset < -1)
  193. config.max_id = offset.slice(1);
  194. this.client.get(endpoint, config, (error, tweets, response) => {
  195. if (error) {
  196. if (error instanceof Array && error.length > 0 && error[0].code === 34) {
  197. logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  198. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  199. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  200. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  201. });
  202. }
  203. else {
  204. logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  205. }
  206. resolve();
  207. }
  208. else
  209. resolve(tweets);
  210. });
  211. }
  212. });
  213. promise.then((tweets) => {
  214. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
  215. const currentThread = lock.threads[currentFeed];
  216. const updateDate = () => currentThread.updatedAt = new Date().toString();
  217. if (!tweets || tweets.length === 0) {
  218. updateDate();
  219. return;
  220. }
  221. const topOfFeed = tweets[0].id_str;
  222. logger.info(`current offset: ${currentThread.offset}, current top of feed: ${topOfFeed}`);
  223. const bottomOfFeed = tweets[tweets.length - 1].id_str;
  224. const setOffset = (offset) => currentThread.offset = offset;
  225. const updateOffset = () => setOffset(topOfFeed);
  226. tweets = tweets.filter(twi => !twi.retweeted_status && twi.extended_entities);
  227. logger.info(`found ${tweets.length} tweets with extended entities`);
  228. if (currentThread.offset === '-1') {
  229. updateOffset();
  230. return;
  231. }
  232. if (currentThread.offset <= 0) {
  233. if (tweets.length === 0) {
  234. setOffset('-' + bottomOfFeed);
  235. lock.workon--;
  236. return;
  237. }
  238. tweets.splice(1);
  239. }
  240. if (tweets.length === 0) {
  241. updateDate();
  242. updateOffset();
  243. return;
  244. }
  245. return this.workOnTweets(tweets, this.sendTweets(`thread ${currentFeed}`, ...currentThread.subscribers))
  246. .then(updateDate).then(updateOffset);
  247. })
  248. .then(() => {
  249. lock.workon++;
  250. let timeout = this.workInterval * 1000 / lock.feed.length;
  251. if (timeout < 1000)
  252. timeout = 1000;
  253. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  254. setTimeout(() => {
  255. this.work();
  256. }, timeout);
  257. });
  258. };
  259. this.client = new Twitter({
  260. consumer_key: opt.consumer_key,
  261. consumer_secret: opt.consumer_secret,
  262. access_token_key: opt.access_token_key,
  263. access_token_secret: opt.access_token_secret,
  264. });
  265. this.lockfile = opt.lockfile;
  266. this.lock = opt.lock;
  267. this.workInterval = opt.workInterval;
  268. this.bot = opt.bot;
  269. this.webshotDelay = opt.webshotDelay;
  270. this.mode = opt.mode;
  271. ScreenNameNormalizer._queryUser = this.queryUser;
  272. exports.sendTweet = (id, receiver) => {
  273. this.getTweet(id, this.sendTweets(`tweet ${id}`, receiver))
  274. .catch((err) => {
  275. if (err[0].code !== 144) {
  276. logger.warn(`error retrieving tweet: ${err[0].message}`);
  277. this.bot.sendTo(receiver, `获取推文时出现错误:${err[0].message}`);
  278. }
  279. this.bot.sendTo(receiver, '找不到请求的推文,它可能已被删除。');
  280. });
  281. };
  282. }
  283. }
  284. exports.default = default_1;