twitter.ts 14 KB

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