twitter.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import * as fs from 'fs';
  2. import * as path from 'path';
  3. import * as Twitter from 'twitter';
  4. import TwitterTypes from 'twitter-d';
  5. import { getLogger } from './loggers';
  6. import QQBot from './koishi';
  7. import { chainPromises, BigNumOps } from './utils';
  8. import Webshot from './webshot';
  9. interface IWorkerOption {
  10. lock: ILock;
  11. lockfile: string;
  12. bot: QQBot;
  13. workInterval: number;
  14. webshotDelay: number;
  15. consumerKey: string;
  16. consumerSecret: string;
  17. accessTokenKey: string;
  18. accessTokenSecret: string;
  19. mode: number;
  20. }
  21. export class ScreenNameNormalizer {
  22. // tslint:disable-next-line: variable-name
  23. public static _queryUser: (username: string) => Promise<string>;
  24. public static normalize = (username: string) => username.toLowerCase().replace(/^@/, '');
  25. public static async normalizeLive(username: string) {
  26. if (this._queryUser) {
  27. return await this._queryUser(username)
  28. .catch((err: {code: number, message: string}[]) => {
  29. if (err[0].code !== 50) {
  30. logger.warn(`error looking up user: ${err[0].message}`);
  31. return username;
  32. }
  33. return null;
  34. });
  35. }
  36. return this.normalize(username);
  37. }
  38. }
  39. export let sendTweet = (id: string, receiver: IChat): void => {
  40. throw Error();
  41. };
  42. export interface ITimelineQueryConfig {
  43. username: string;
  44. count?: number;
  45. since?: string;
  46. until?: string;
  47. noreps?: boolean;
  48. norts?: boolean;
  49. }
  50. export let sendTimeline = (
  51. conf: {[key in keyof ITimelineQueryConfig]: string},
  52. receiver: IChat
  53. ): void => {
  54. throw Error();
  55. };
  56. const TWITTER_EPOCH = 1288834974657;
  57. const snowflake = (epoch: number) => Number.isNaN(epoch) ? undefined :
  58. BigNumOps.lShift(String(epoch - 1 - TWITTER_EPOCH), 22);
  59. const logger = getLogger('twitter');
  60. const maxTrials = 3;
  61. const retryInterval = 1500;
  62. const ordinal = (n: number) => {
  63. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  64. case 1:
  65. return `${n}st`;
  66. case 2:
  67. return `${n}nd`;
  68. case 3:
  69. return `${n}rd`;
  70. default:
  71. return `${n}th`;
  72. }
  73. };
  74. const retryOnError = <T, U>(
  75. doWork: () => Promise<T>,
  76. onRetry: (error, count: number, terminate: (defaultValue: U) => void) => void
  77. ) => new Promise<T | U>(resolve => {
  78. const retry = (reason, count: number) => {
  79. setTimeout(() => {
  80. let terminate = false;
  81. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  82. if (!terminate) doWork().then(resolve).catch(error => retry(error, count + 1));
  83. }, retryInterval);
  84. };
  85. doWork().then(resolve).catch(error => retry(error, 1));
  86. });
  87. export type FullUser = TwitterTypes.FullUser;
  88. export type Entities = TwitterTypes.Entities;
  89. export type ExtendedEntities = TwitterTypes.ExtendedEntities;
  90. export type MediaEntity = TwitterTypes.MediaEntity;
  91. interface ITweet extends TwitterTypes.Status {
  92. user: FullUser;
  93. retweeted_status?: Tweet;
  94. }
  95. export type Tweet = ITweet;
  96. export type Tweets = ITweet[];
  97. export default class {
  98. private client: Twitter;
  99. private lock: ILock;
  100. private lockfile: string;
  101. private workInterval: number;
  102. private bot: QQBot;
  103. private webshotDelay: number;
  104. private webshot: Webshot;
  105. private mode: number;
  106. constructor(opt: IWorkerOption) {
  107. this.client = new Twitter({
  108. consumer_key: opt.consumerKey,
  109. consumer_secret: opt.consumerSecret,
  110. access_token_key: opt.accessTokenKey,
  111. access_token_secret: opt.accessTokenSecret,
  112. });
  113. this.lockfile = opt.lockfile;
  114. this.lock = opt.lock;
  115. this.workInterval = opt.workInterval;
  116. this.bot = opt.bot;
  117. this.webshotDelay = opt.webshotDelay;
  118. this.mode = opt.mode;
  119. ScreenNameNormalizer._queryUser = this.queryUser;
  120. sendTweet = (id, receiver) => {
  121. this.getTweet(id, this.sendTweets(`tweet ${id}`, receiver))
  122. .catch((err: {code: number, message: string}[]) => {
  123. if (err[0].code !== 144) {
  124. logger.warn(`error retrieving tweet: ${err[0].message}`);
  125. this.bot.sendTo(receiver, `获取推文时出现错误:${err[0].message}`);
  126. }
  127. this.bot.sendTo(receiver, '找不到请求的推文,它可能已被删除。');
  128. });
  129. };
  130. sendTimeline = ({username, count, since, until, noreps, norts}, receiver) => {
  131. const countNum = Number(count) || 10;
  132. (countNum > 0 ? this.queryTimeline : this.queryTimelineReverse)({
  133. username,
  134. count: Math.abs(countNum),
  135. since: BigNumOps.parse(since) || snowflake(new Date(since).getTime()),
  136. until: BigNumOps.parse(until) || snowflake(new Date(until).getTime()),
  137. noreps: {on: true, off: false}[noreps],
  138. norts: {on: true, off: false}[norts],
  139. })
  140. .then(tweets => chainPromises(
  141. tweets.map(tweet => this.bot.sendTo(receiver, `\
  142. 编号:${tweet.id_str}
  143. 时间:${tweet.created_at}
  144. 媒体:${tweet.extended_entities ? '有' : '无'}
  145. 正文:\n${tweet.full_text.replace(/^([\s\S\n]{50})[\s\S\n]+?( https:\/\/t.co\/.*)?$/, '$1…$2')}`
  146. ))
  147. .concat(this.bot.sendTo(receiver, tweets.length ?
  148. '时间线查询完毕,使用 /twitter_view <编号> 查看推文详细内容。' :
  149. '时间线查询完毕,没有找到符合条件的推文。'
  150. ))
  151. ))
  152. .catch((err: {code: number, message: string}[]) => {
  153. if (err[0]?.code !== 34) {
  154. logger.warn(`error retrieving timeline: ${err[0]?.message || err}`);
  155. return this.bot.sendTo(receiver, `获取时间线时出现错误:${err[0]?.message || err}`);
  156. }
  157. this.bot.sendTo(receiver, `找不到用户 ${username.replace(/^@?(.*)$/, '@$1')}。`);
  158. });
  159. };
  160. }
  161. public launch = () => {
  162. this.webshot = new Webshot(
  163. this.mode,
  164. () => setTimeout(this.work, this.workInterval * 1000)
  165. );
  166. };
  167. public queryUser = (username: string) => this.client.get('users/show', {screen_name: username})
  168. .then((user: FullUser) => user.screen_name);
  169. public queryTimelineReverse = (conf: ITimelineQueryConfig) => {
  170. if (!conf.since) return this.queryTimeline(conf);
  171. const count = conf.count;
  172. const maxID = conf.until;
  173. conf.count = undefined;
  174. const until = () => BigNumOps.min(maxID, BigNumOps.plus(conf.since, String(7 * 24 * 3600 * 1000 * 2 ** 22)));
  175. conf.until = until();
  176. const promise = (tweets: ITweet[]): Promise<ITweet[]> =>this.queryTimeline(conf).then(newTweets => {
  177. tweets = newTweets.concat(tweets);
  178. conf.since = conf.until;
  179. conf.until = until();
  180. if (
  181. tweets.length >= count ||
  182. BigNumOps.compare(conf.since, conf.until) >= 0
  183. ) {
  184. return tweets.slice(-count);
  185. }
  186. return promise(tweets);
  187. });
  188. return promise([]);
  189. };
  190. public queryTimeline = (
  191. { username, count, since, until, noreps, norts }: ITimelineQueryConfig
  192. ) => {
  193. username = username.replace(/^@?(.*)$/, '@$1');
  194. logger.info(`querying timeline of ${username} with config: ${
  195. JSON.stringify({
  196. ...(count && {count}),
  197. ...(since && {since}),
  198. ...(until && {until}),
  199. ...(noreps && {noreps}),
  200. ...(norts && {norts}),
  201. })}`);
  202. const fetchTimeline = (
  203. config = {
  204. screen_name: username.slice(1),
  205. trim_user: true,
  206. exclude_replies: noreps ?? true,
  207. include_rts: !(norts ?? false),
  208. since_id: since,
  209. max_id: until,
  210. tweet_mode: 'extended',
  211. },
  212. tweets: ITweet[] = []
  213. ): Promise<ITweet[]> => this.client.get('statuses/user_timeline', config)
  214. .then((newTweets: ITweet[]) => {
  215. if (newTweets.length) {
  216. logger.debug(`fetched tweets: ${JSON.stringify(newTweets)}`);
  217. config.max_id = BigNumOps.plus('-1', newTweets[newTweets.length - 1].id_str);
  218. logger.info(`timeline query of ${username} yielded ${
  219. newTweets.length
  220. } new tweets, next query will start at offset ${config.max_id}`);
  221. tweets.push(...newTweets);
  222. }
  223. if (!newTweets.length || tweets.length >= count) {
  224. logger.info(`timeline query of ${username} finished successfully, ${
  225. tweets.length
  226. } tweets have been fetched`);
  227. return tweets.slice(0, count);
  228. }
  229. return fetchTimeline(config, tweets);
  230. });
  231. return fetchTimeline();
  232. };
  233. private workOnTweets = (
  234. tweets: Tweets,
  235. sendTweets: (msg: string, text: string, author: string) => void
  236. ) => this.webshot(tweets, sendTweets, this.webshotDelay);
  237. public getTweet = (id: string, sender: (msg: string, text: string, author: string) => void) => {
  238. const endpoint = 'statuses/show';
  239. const config = {
  240. id,
  241. tweet_mode: 'extended',
  242. };
  243. return this.client.get(endpoint, config)
  244. .then((tweet: Tweet) => {
  245. logger.debug(`api returned tweet ${JSON.stringify(tweet)} for query id=${id}`);
  246. return this.workOnTweets([tweet], sender);
  247. });
  248. };
  249. private sendTweets = (source?: string, ...to: IChat[]) => (msg: string, text: string, author: string) => {
  250. to.forEach(subscriber => {
  251. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  252. retryOnError(
  253. () => this.bot.sendTo(subscriber, msg),
  254. (_, count, terminate: (doNothing: Promise<void>) => void) => {
  255. if (count <= maxTrials) {
  256. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  257. } else {
  258. logger.warn(`${count - 1} consecutive failures while sending` +
  259. 'message chain, trying plain text instead...');
  260. terminate(this.bot.sendTo(subscriber, author + text));
  261. }
  262. });
  263. });
  264. };
  265. public work = () => {
  266. const lock = this.lock;
  267. if (this.workInterval < 1) this.workInterval = 1;
  268. if (lock.feed.length === 0) {
  269. setTimeout(() => {
  270. this.work();
  271. }, this.workInterval * 1000);
  272. return;
  273. }
  274. if (lock.workon >= lock.feed.length) lock.workon = 0;
  275. if (!lock.threads[lock.feed[lock.workon]] ||
  276. !lock.threads[lock.feed[lock.workon]].subscribers ||
  277. lock.threads[lock.feed[lock.workon]].subscribers.length === 0) {
  278. logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`);
  279. delete lock.threads[lock.feed[lock.workon]];
  280. lock.feed.splice(lock.workon, 1);
  281. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  282. this.work();
  283. return;
  284. }
  285. const currentFeed = lock.feed[lock.workon];
  286. logger.debug(`pulling feed ${currentFeed}`);
  287. const promise = new Promise(resolve => {
  288. let match = /https:\/\/twitter.com\/([^\/]+)\/lists\/([^\/]+)/.exec(currentFeed);
  289. let config: {[key: string]: any};
  290. let endpoint: string;
  291. if (match) {
  292. if (match[1] === 'i') {
  293. config = {
  294. list_id: match[2],
  295. tweet_mode: 'extended',
  296. };
  297. } else {
  298. config = {
  299. owner_screen_name: match[1],
  300. slug: match[2],
  301. tweet_mode: 'extended',
  302. };
  303. }
  304. endpoint = 'lists/statuses';
  305. } else {
  306. match = /https:\/\/twitter.com\/([^\/]+)/.exec(currentFeed);
  307. if (match) {
  308. config = {
  309. screen_name: match[1],
  310. exclude_replies: false,
  311. tweet_mode: 'extended',
  312. };
  313. endpoint = 'statuses/user_timeline';
  314. }
  315. }
  316. if (endpoint) {
  317. const offset = lock.threads[currentFeed].offset as unknown as number;
  318. if (offset > 0) config.since_id = offset;
  319. this.client.get(endpoint, config, (error: {[key: string]: any}[], tweets, response) => {
  320. if (error) {
  321. if (error instanceof Array && error.length > 0 && error[0].code === 34) {
  322. logger.warn(`error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  323. lock.threads[currentFeed].subscribers.forEach(subscriber => {
  324. logger.info(`sending notfound message of ${currentFeed} to ${JSON.stringify(subscriber)}`);
  325. this.bot.sendTo(subscriber, `链接 ${currentFeed} 指向的用户或列表不存在,请退订。`).catch();
  326. });
  327. } else {
  328. logger.error(`unhandled error on fetching tweets for ${currentFeed}: ${JSON.stringify(error)}`);
  329. }
  330. resolve([]);
  331. } else resolve(tweets);
  332. });
  333. }
  334. });
  335. promise.then((tweets: Tweets) => {
  336. logger.debug(`api returned ${JSON.stringify(tweets)} for feed ${currentFeed}`);
  337. const currentThread = lock.threads[currentFeed];
  338. const updateDate = () => currentThread.updatedAt = new Date().toString();
  339. if (!tweets || tweets.length === 0) { updateDate(); return; }
  340. const topOfFeed = tweets[0].id_str;
  341. const updateOffset = () => currentThread.offset = topOfFeed;
  342. if (currentThread.offset === '-1') { updateOffset(); return; }
  343. if (currentThread.offset === '0') tweets.splice(1);
  344. return this.workOnTweets(tweets, this.sendTweets(`thread ${currentFeed}`, ...currentThread.subscribers))
  345. .then(updateDate).then(updateOffset);
  346. })
  347. .then(() => {
  348. lock.workon++;
  349. let timeout = this.workInterval * 1000 / lock.feed.length;
  350. if (timeout < 1000) timeout = 1000;
  351. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  352. setTimeout(() => {
  353. this.work();
  354. }, timeout);
  355. });
  356. };
  357. }