twitter.ts 13 KB

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