twitter.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.queryByRegExp = exports.snowflake = void 0;
  4. const Twitter = require("twitter");
  5. const loggers_1 = require("./loggers");
  6. const utils_1 = require("./utils");
  7. const TWITTER_EPOCH = 1288834974657;
  8. const snowflake = (epoch) => Number.isNaN(epoch) ? undefined :
  9. utils_1.BigNumOps.lShift(String(epoch - 1 - TWITTER_EPOCH), 22);
  10. exports.snowflake = snowflake;
  11. const logger = loggers_1.getLogger('twitter');
  12. let queryByRegExp = (username, regexp, queryThreshold, until) => Promise.resolve(null);
  13. exports.queryByRegExp = queryByRegExp;
  14. class default_1 {
  15. constructor(opt) {
  16. this.lastQueries = {};
  17. this.queryUser = (username) => this.client.get('users/show', { screen_name: username })
  18. .then((user) => user.screen_name);
  19. this.queryTimelineReverse = (conf) => {
  20. if (!conf.since)
  21. return this.queryTimeline(conf);
  22. const count = conf.count;
  23. const maxID = conf.until;
  24. conf.count = undefined;
  25. const until = () => utils_1.BigNumOps.min(maxID, utils_1.BigNumOps.plus(conf.since, String(7 * 24 * 3600 * 1000 * Math.pow(2, 22))));
  26. conf.until = until();
  27. const promise = (tweets) => this.queryTimeline(conf).then(newTweets => {
  28. tweets = newTweets.concat(tweets);
  29. conf.since = conf.until;
  30. conf.until = until();
  31. if (tweets.length >= count ||
  32. utils_1.BigNumOps.compare(conf.since, conf.until) >= 0) {
  33. return tweets.slice(-count);
  34. }
  35. return promise(tweets);
  36. });
  37. return promise([]);
  38. };
  39. this.queryTimeline = ({ username, count, since, until, noreps, norts }) => {
  40. username = username.replace(/^@?(.*)$/, '@$1');
  41. logger.info(`querying timeline of ${username} with config: ${JSON.stringify(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (count && { count })), (since && { since })), (until && { until })), (noreps && { noreps })), (norts && { norts })))}`);
  42. const fetchTimeline = (config = {
  43. screen_name: username.slice(1),
  44. trim_user: true,
  45. exclude_replies: noreps !== null && noreps !== void 0 ? noreps : true,
  46. include_rts: !(norts !== null && norts !== void 0 ? norts : false),
  47. since_id: since,
  48. max_id: until,
  49. tweet_mode: 'extended',
  50. }, tweets = []) => this.client.get('statuses/user_timeline', config)
  51. .then((newTweets) => {
  52. if (newTweets.length) {
  53. logger.debug(`fetched tweets: ${JSON.stringify(newTweets)}`);
  54. config.max_id = utils_1.BigNumOps.plus('-1', newTweets[newTweets.length - 1].id_str);
  55. logger.info(`timeline query of ${username} yielded ${newTweets.length} new tweets, next query will start at offset ${config.max_id}`);
  56. tweets.push(...newTweets);
  57. }
  58. if (!newTweets.length || count === undefined || tweets.length >= count) {
  59. logger.info(`timeline query of ${username} finished successfully, ${tweets.length} tweets have been fetched`);
  60. return tweets.slice(0, count);
  61. }
  62. return fetchTimeline(config, tweets);
  63. });
  64. return fetchTimeline();
  65. };
  66. this.client = new Twitter({
  67. consumer_key: opt.consumerKey,
  68. consumer_secret: opt.consumerSecret,
  69. access_token_key: opt.accessTokenKey,
  70. access_token_secret: opt.accessTokenSecret,
  71. });
  72. exports.queryByRegExp = (username, regexp, queryThreshold, until) => {
  73. logger.info(`searching timeline of @${username} for matches of ${regexp}...`);
  74. const normalizedUsername = username.toLowerCase().replace(/^@/, '');
  75. const queryKey = `${normalizedUsername}:${regexp.toString()}`;
  76. const isOld = (then, threshold = 360) => {
  77. if (!then)
  78. return true;
  79. return utils_1.BigNumOps.compare(exports.snowflake(Date.now() - threshold * 1000), then) >= 0;
  80. };
  81. if (queryKey in this.lastQueries && !isOld(this.lastQueries[queryKey].id)) {
  82. const { match, id } = this.lastQueries[queryKey];
  83. logger.info(`found match ${JSON.stringify(match)} from cached tweet of id ${id}`);
  84. return Promise.resolve(match);
  85. }
  86. return this.queryTimeline({ username, norts: true, until })
  87. .then(tweets => {
  88. const found = tweets.find(tweet => regexp.test(tweet.full_text));
  89. if (found) {
  90. const match = regexp.exec(found.full_text);
  91. this.lastQueries[queryKey] = { match, id: found.id_str };
  92. logger.info(`found match ${JSON.stringify(match)} in tweet of id ${found.id_str} from timeline`);
  93. return match;
  94. }
  95. const last = tweets.slice(-1)[0].id_str;
  96. if (isOld(last, queryThreshold))
  97. return null;
  98. return exports.queryByRegExp(username, regexp, queryThreshold, last);
  99. });
  100. };
  101. }
  102. }
  103. exports.default = default_1;