twitter.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. import * as crypto from 'crypto';
  2. import * as fs from 'fs';
  3. import * as http from 'http';
  4. import * as path from 'path';
  5. import { parse as parseUrl } from 'url';
  6. import { promisify } from 'util';
  7. import {
  8. IgApiClient,
  9. IgClientError, IgCookieNotFoundError, IgExactUserNotFoundError, IgLoginTwoFactorRequiredError, IgLoginRequiredError, IgNetworkError,
  10. ReelsMediaFeedResponseItem
  11. } from 'instagram-private-api';
  12. import { RequestError } from 'request-promise/errors';
  13. import { SocksProxyAgent } from 'socks-proxy-agent';
  14. import { relativeDate } from './datetime';
  15. import { getLogger } from './loggers';
  16. import * as json from './json';
  17. import QQBot from './koishi';
  18. import { Arr, BigNumOps, chainPromises } from './utils';
  19. import Webshot from './webshot';
  20. const parseLink = (link: string): {userName?: string, storyId?: string} => {
  21. let match =
  22. /instagram\.com\/stories\/([^\/?#]+)\/(\d+)/.exec(link);
  23. if (match) return {userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0], storyId: match[2]};
  24. match =
  25. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  26. /^([^\/?#]+)$/.exec(link);
  27. if (match) return {userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0]};
  28. return;
  29. };
  30. const linkBuilder = (config: ReturnType<typeof parseLink>): string => {
  31. if (!config.userName) return;
  32. if (!config.storyId) return `https://www.instagram.com/${config.userName}/`;
  33. return `https://www.instagram.com/stories/${config.userName}/${config.storyId}/`;
  34. };
  35. export {linkBuilder, parseLink};
  36. const igErrorIsAuthError = (error: IgClientError) =>
  37. / 40[1-3]/.test(error.message) || error instanceof IgLoginRequiredError || error instanceof IgCookieNotFoundError;
  38. interface IWorkerOption {
  39. sessionLockfile: string;
  40. credentials: [string, string];
  41. codeServicePort: number;
  42. proxyUrl: string;
  43. lock: ILock;
  44. lockfile: string;
  45. cache: ICache;
  46. cachefile: string;
  47. webshotCookiesLockfile: string;
  48. bot: QQBot;
  49. inactiveHours: string[];
  50. workInterval: number;
  51. webshotDelay: number;
  52. mode: number;
  53. wsUrl: string;
  54. }
  55. export class SessionManager {
  56. private ig: IgApiClient;
  57. private username: string;
  58. private password: string;
  59. private lockfile: string;
  60. private codeServicePort: number;
  61. constructor(client: IgApiClient, file: string, credentials: [string, string], codeServicePort: number) {
  62. this.ig = client;
  63. this.lockfile = file;
  64. [this.username, this.password] = credentials;
  65. this.codeServicePort = codeServicePort;
  66. }
  67. public init = () => {
  68. this.ig.state.generateDevice(this.username);
  69. this.ig.request.end$.subscribe(() => { this.save(); });
  70. const filePath = path.resolve(this.lockfile);
  71. if (fs.existsSync(filePath)) {
  72. try {
  73. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8')) as {[key: string]: any};
  74. return this.ig.state.deserialize(serialized).then(() => {
  75. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  76. });
  77. } catch (err) {
  78. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  79. return Promise.resolve();
  80. }
  81. } else {
  82. return this.login().catch((err: IgClientError) => {
  83. logger.error(`error while trying to log in as user ${this.username}, error: ${err}`);
  84. logger.warn('attempting to retry after 1 minute...');
  85. if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
  86. promisify(setTimeout)(60000).then(this.init);
  87. });
  88. }
  89. };
  90. public handle2FA = <T>(submitter: (code: string) => Promise<T>) => new Promise<T>((resolve, reject) => {
  91. const token = crypto.randomBytes(20).toString('hex');
  92. logger.info('please submit the code with a one-time token from your browser with this path:');
  93. logger.info(`/confirm-2fa?code=<the code you received>&token=${token}`);
  94. let working;
  95. const server = http.createServer((req, res) => {
  96. const {pathname, query} = parseUrl(req.url, true);
  97. if (!working && pathname === '/confirm-2fa' && query.token === token &&
  98. typeof(query.code) === 'string' && /^\d{6}$/.test(query.code)) {
  99. const code = query.code;
  100. logger.debug(`received code: ${code}`);
  101. working = true;
  102. submitter(code)
  103. .then(response => { res.write('OK'); res.end(); server.close(() => resolve(response)); })
  104. .catch(err => { res.write('Error'); res.end(); reject(err); })
  105. .finally(() => { working = false; });
  106. }
  107. });
  108. server.listen(this.codeServicePort);
  109. });
  110. public login = () =>
  111. this.ig.simulate.preLoginFlow()
  112. .catch((err) => logger.error(err))
  113. .then(() => this.ig.account.login(this.username, this.password))
  114. .catch((err: IgClientError) => {
  115. if (err instanceof IgLoginTwoFactorRequiredError) {
  116. const {two_factor_identifier, totp_two_factor_on} = err.response.body.two_factor_info;
  117. logger.debug(`2FA info: ${JSON.stringify(err.response.body.two_factor_info)}`);
  118. logger.info(`login is requesting two-factor authentication via ${totp_two_factor_on ? 'TOTP' : 'SMS'}`);
  119. return this.handle2FA(code => this.ig.account.twoFactorLogin({
  120. username: this.username,
  121. verificationCode: code,
  122. twoFactorIdentifier: two_factor_identifier,
  123. verificationMethod: totp_two_factor_on ? '0' : '1',
  124. }));
  125. }
  126. logger.error(err);
  127. })
  128. .then(user => {
  129. logger.info(`successfully logged in as ${this.username}`);
  130. return user;
  131. });
  132. public save = () =>
  133. this.ig.state.serialize()
  134. .then((serialized: {[key: string]: any}) => {
  135. delete serialized.constants;
  136. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  137. });
  138. }
  139. export class ScreenNameNormalizer {
  140. // tslint:disable-next-line: variable-name
  141. public static _queryUser: (username: string) => Promise<string>;
  142. public static normalize = (username: string) => `${username.toLowerCase().replace(/^@/, '')}:`;
  143. public static async normalizeLive(username: string) {
  144. if (this._queryUser) {
  145. return await this._queryUser(username)
  146. .catch((err: IgClientError) => {
  147. if (!(err instanceof IgExactUserNotFoundError)) {
  148. logger.warn(`error looking up user: ${err.message}`);
  149. return `${username}:`;
  150. }
  151. return null;
  152. });
  153. }
  154. return this.normalize(username);
  155. }
  156. }
  157. export let sendTimeline = (username: string, receiver: IChat): void => {
  158. throw Error();
  159. }
  160. export let sendStory = (username: string, storyId: string, receiver: IChat): void => {
  161. throw Error();
  162. }
  163. export let sendAllStories = (username: string, receiver: IChat, startIndex: number, count: number): void => {
  164. throw Error();
  165. };
  166. export type MediaItem = ReelsMediaFeedResponseItem;
  167. type CachedMediaItem = {pk: string, msgs: string, text: string, author: string, original: MediaItem};
  168. const logger = getLogger('instagram');
  169. const maxTrials = 3;
  170. const retryInterval = 1500;
  171. const ordinal = (n: number) => {
  172. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  173. case 1:
  174. return `${n}st`;
  175. case 2:
  176. return `${n}nd`;
  177. case 3:
  178. return `${n}rd`;
  179. default:
  180. return `${n}th`;
  181. }
  182. };
  183. const retryOnError = <T, U>(
  184. doWork: () => Promise<T>,
  185. onRetry: (error, count: number, terminate: (defaultValue: U) => void) => void
  186. ) => new Promise<T | U>(resolve => {
  187. const retry = (reason, count: number) => {
  188. setTimeout(() => {
  189. let terminate = false;
  190. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  191. if (!terminate) doWork().then(resolve).catch(error => retry(error, count + 1));
  192. }, retryInterval);
  193. };
  194. doWork().then(resolve).catch(error => retry(error, 1));
  195. });
  196. export interface ICache {
  197. [userId: string]: {
  198. user: ReelsMediaFeedResponseItem['user'];
  199. stories: {
  200. [storyId: string]: CachedMediaItem;
  201. };
  202. pullOrder: number; // one-based; -1: subscribed, awaiting shuffle; 0: not subscribed
  203. updated?: string; // Date.toString()
  204. };
  205. }
  206. export default class {
  207. private client: IgApiClient;
  208. private lock: ILock;
  209. private lockfile: string;
  210. private cache: ICache;
  211. private cachefile: string;
  212. private inactiveHours: string[];
  213. private workInterval: number;
  214. private bot: QQBot;
  215. private webshotDelay: number;
  216. private webshot: Webshot;
  217. private mode: number;
  218. private wsUrl: string;
  219. public session: SessionManager;
  220. constructor(opt: IWorkerOption) {
  221. this.client = new IgApiClient();
  222. if (opt.proxyUrl) {
  223. try {
  224. const url = new URL(opt.proxyUrl);
  225. if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol)) throw Error();
  226. if (!url.port) url.port = '1080';
  227. this.client.request.defaults.agent = new SocksProxyAgent({
  228. hostname: url.hostname,
  229. port: url.port,
  230. userId: url.username,
  231. password: url.password,
  232. });
  233. } catch (e) {
  234. logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
  235. }
  236. }
  237. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
  238. this.lockfile = opt.lockfile;
  239. this.lock = opt.lock;
  240. this.cachefile = opt.cachefile;
  241. this.cache = opt.cache;
  242. this.inactiveHours = opt.inactiveHours;
  243. this.workInterval = opt.workInterval;
  244. this.bot = opt.bot;
  245. this.webshotDelay = opt.webshotDelay;
  246. this.mode = opt.mode;
  247. this.wsUrl = opt.wsUrl;
  248. const workNow = (config: {
  249. rawUserName: string,
  250. action: (userId: number | string) => void,
  251. retryAction: () => void,
  252. reply: (msg: string) => void
  253. }) => {
  254. const {action, retryAction, reply, rawUserName} = config;
  255. return this.queryUser(rawUserName)
  256. .then(userNameId => {
  257. const userId = userNameId.split(':')[1];
  258. const lastUpdated = new Date((this.cache[userId] || {}).updated || 0).getTime();
  259. const storyCount = lastUpdated && Object.keys(this.cache[userId].stories || {}).length;
  260. if (new Date().getTime() - lastUpdated > this.workInterval * 1000 && storyCount) return userId;
  261. return this.client.feed.reelsMedia({userIds: [userId]}).items()
  262. .then(storyItems => Promise.all(storyItems
  263. .filter(item => !(item.pk in this.cache[userId].stories))
  264. .map(item => this.webshot(
  265. [{...item, user: this.cache[userId].user}], // guaranteed fresh cache entry
  266. (msgs: string, text: string, author: string) =>
  267. this.cache[userId].stories[item.pk] = {pk: item.pk, msgs, text, author, original: item},
  268. this.webshotDelay
  269. ))
  270. ).then(() => userId).finally(() => this.cache[userId].updated = Date()));
  271. })
  272. .then(action)
  273. .catch((error: IgClientError & Partial<RequestError>) => {
  274. if (error instanceof IgExactUserNotFoundError) {
  275. reply(`找不到用户 ${rawUserName.replace(/^@?(.*)$/, '@$1')}。`);
  276. } else if (error instanceof IgNetworkError) {
  277. if ((error.cause as Error).message === "Unexpected '<'") {
  278. logger.warn('login required, logging in again...');
  279. return this.session.login().then(retryAction);
  280. }
  281. logger.warn(`error while fetching stories for ${rawUserName}: ${JSON.stringify(error.cause)}`);
  282. reply(`获取 Stories 时出现错误:原因: ${error.cause}`);
  283. } else if (igErrorIsAuthError(error)) {
  284. logger.warn('login required, logging in again...');
  285. reply('等待登录中,稍后会处理请求,请稍候……');
  286. this.session.login().then(retryAction);
  287. } else {
  288. logger.error(`unhandled error while fetching stories for ${rawUserName}: ${error}`);
  289. reply(`获取 Stories 时发生未知错误: ${error}`);
  290. }
  291. });
  292. };
  293. ScreenNameNormalizer._queryUser = this.queryUser;
  294. sendTimeline = (rawUserName, receiver) => {
  295. const reply = msg => this.bot.sendTo(receiver, msg);
  296. workNow({
  297. rawUserName,
  298. action: userId => {
  299. const userName = this.cache[userId].user.username;
  300. const storyItems = Object.values(this.cache[userId].stories)
  301. .sort((i1, i2) => -BigNumOps.compare(i2.pk, i1.pk)); // ascending!
  302. if (storyItems.length === 0) return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
  303. return reply('#. 编号:发送时间\n' + storyItems.map(({original}, index) =>
  304. `\n${index + 1}. ${original.pk}: ${relativeDate(original.taken_at * 1000)}`).join(''))
  305. .then(() => reply(`请使用 /igstory_view ${userName} skip=<#-1> count=1
  306. 或 /igstory_view https://www.instagram.com/stories/${userName}/<编号>/
  307. 查看指定的限时动态。`));
  308. },
  309. reply,
  310. retryAction: () => sendTimeline(rawUserName, receiver),
  311. });
  312. }
  313. sendStory = (rawUserName, storyId, receiver) => {
  314. const reply = msg => this.bot.sendTo(receiver, msg);
  315. const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
  316. workNow({
  317. rawUserName,
  318. action: userId => {
  319. if (!(storyId in this.cache[userId].stories)) return reply('此动态不存在或已过期。');
  320. return this.workOnMedia([this.cache[userId].stories[storyId]], sender);
  321. },
  322. reply,
  323. retryAction: () => sendStory(rawUserName, storyId, receiver),
  324. });
  325. }
  326. sendAllStories = (rawUserName, receiver, startIndex = 0, count = 10) => {
  327. const reply = msg => this.bot.sendTo(receiver, msg);
  328. if (count < 1) return reply('最大查看数量参数值应为正整数。');
  329. const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
  330. workNow({
  331. rawUserName,
  332. action: userId => {
  333. const userName = this.cache[userId].user.username;
  334. const storyItems = Object.values(this.cache[userId].stories)
  335. .sort((i1, i2) => -BigNumOps.compare(i2.pk, i1.pk)); // ascending!
  336. if (storyItems.length === 0) return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
  337. if (startIndex < 0) startIndex += storyItems.length;
  338. if (startIndex < 0 || startIndex + 1 > storyItems.length) return reply('跳过数量到达或超过当前用户可用的限时动态数量。');
  339. const endIndex = Math.min(storyItems.length, startIndex + count);
  340. const sendRangeText = `${startIndex + 1}${endIndex - startIndex > 1 ? `-${endIndex}` : ''}`;
  341. return this.workOnMedia(storyItems.slice(startIndex, endIndex), sender)
  342. .then(() => reply(`已显示当前用户 ${storyItems.length} 条可用限时动态中的第 ${sendRangeText} 条。`));
  343. },
  344. reply,
  345. retryAction: () => sendAllStories(rawUserName, receiver, startIndex, count)
  346. });
  347. };
  348. }
  349. public launch = () => {
  350. this.webshot = new Webshot(
  351. this.wsUrl,
  352. this.mode,
  353. () => {
  354. const subscribedIds = this.lock.feed.map(feed => this.lock.threads[feed].id.toString());
  355. for (const id in this.cache) {
  356. if (this.cache[id].pullOrder !== 0 && !subscribedIds.includes(id)) {
  357. logger.warn(`disabling pull job of unsubscribed user @${this.cache[id].user.username}`);
  358. this.cache[id].pullOrder = 0;
  359. }
  360. }
  361. const userIdCache = this.pullOrders;
  362. return (() => {
  363. if (Object.values(userIdCache).length !== userIdCache.length) {
  364. this.pullOrders = Arr.shuffle(userIdCache);
  365. return json.writeFile(path.resolve(this.cachefile), this.cache);
  366. }
  367. return Promise.resolve(true);
  368. })().then(() => {
  369. const timeout = Math.max(1000, this.workInterval * 1000 / this.lock.feed.length);
  370. setInterval(() => { this.pullOrders = Arr.shuffle(this.pullOrders); }, 21600000);
  371. setTimeout(this.workForAll, timeout);
  372. this.work();
  373. });
  374. }
  375. );
  376. };
  377. public queryUserObject = (userName: string) =>
  378. this.client.user.searchExact(userName)
  379. .catch((error: IgClientError) => {
  380. if (igErrorIsAuthError(error)) {
  381. logger.warn('login required, logging in again...');
  382. return this.session.login().then(() => this.client.user.searchExact(userName));
  383. } else throw error;
  384. });
  385. public queryUser = (rawUserName: string) => {
  386. const username = ScreenNameNormalizer.normalize(rawUserName).split(':')[0];
  387. for (const {user} of Object.values(this.cache)) {
  388. if (user.username === username) return Promise.resolve(`${username}:${user.pk}`);
  389. }
  390. return this.queryUserObject(username)
  391. .then(({pk, username, full_name}) => {
  392. this.cache[pk] = {user: {pk, username, full_name}, stories: {}, pullOrder: 0};
  393. return json.writeFile(path.resolve(this.cachefile), this.cache).then(res => {
  394. if (res) logger.info(`initialized cache item for user ${full_name} (@${username})`);
  395. return `${username}:${pk}`;
  396. });
  397. });
  398. };
  399. private workOnMedia = (
  400. mediaItems: CachedMediaItem[],
  401. sendMedia: (msg: string, text: string, author: string) => void
  402. ) => chainPromises(mediaItems.map(({msgs, text, author, original}) => {
  403. const findFilePath = (mediaMsg) => /=file:\/\/(.*?)]/.exec(mediaMsg)[1];
  404. const filePath = findFilePath(msgs);
  405. return () =>
  406. (fs.existsSync(filePath) ?
  407. Promise.resolve() :
  408. this.webshot.fetchBestCandidate(original).then(mediaMsg => {
  409. logger.warn(`media file missing, refetched media for ${author}/${original.code}`);
  410. return promisify(fs.rename)(findFilePath(mediaMsg), filePath);
  411. })
  412. ).then(() => sendMedia(msgs, text, author));
  413. }));
  414. private sendStories = (source?: string, ...to: IChat[]) => (msg: string, text: string, author: string) => {
  415. to.forEach(subscriber => {
  416. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  417. retryOnError(
  418. () => this.bot.sendTo(subscriber, msg),
  419. (_, count, terminate: (doNothing: Promise<void>) => void) => {
  420. if (count <= maxTrials) {
  421. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  422. } else {
  423. logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
  424. terminate(this.bot.sendTo(subscriber, author + text, true));
  425. }
  426. });
  427. });
  428. };
  429. private get pullOrders() {
  430. const arr: number[] = [];
  431. Object.values(this.cache).forEach(item => {
  432. if (item.pullOrder > 0) arr[item.pullOrder - 1] = item.user.pk;
  433. });
  434. return arr;
  435. };
  436. private set pullOrders(arr: number[]) {
  437. Object.values(this.cache).forEach(item => {
  438. item.pullOrder = arr.indexOf(item.user.pk) + 1;
  439. });
  440. }
  441. private workForAll = () => {
  442. const timeout = Math.max(1000, this.workInterval * 1000 / this.lock.feed.length);
  443. if (this.isInactiveTime) { setTimeout(this.workForAll, timeout); return; }
  444. chainPromises(Object.entries(this.lock.threads).map(([feed, thread]) => () => {
  445. const id = thread.id;
  446. const userName = parseLink(feed).userName;
  447. logger.debug(`preparing to add user @${userName} to next pull task...`);
  448. if (id in this.cache) {
  449. const item = this.cache[id];
  450. if (item.pullOrder === 0) item.pullOrder = -1;
  451. return Promise.resolve();
  452. }
  453. return promisify(setTimeout)((Math.random() * 2 + 1) * 5000).then(() =>
  454. this.client.user.info(id).then(({pk, username, full_name}) => {
  455. this.cache[id] = {user: {pk, username, full_name}, stories: {}, pullOrder: -1};
  456. return json.writeFile(path.resolve(this.cachefile), this.cache).then(() =>
  457. logger.info(`initialized cache item for user ${full_name} (@${username})`)
  458. );
  459. })
  460. );
  461. }))
  462. .then(() => {
  463. const userIdCache = Object.values(this.cache).some(item => item.pullOrder < 0) ?
  464. this.pullOrders = Arr.shuffle(Object.keys(this.cache)).map(Number) :
  465. this.pullOrders;
  466. return chainPromises(
  467. Arr.chunk(userIdCache.filter(v => Date.now() - new Date(this.cache[v].updated).getTime() > 3600000), 30).map(userIds => () => {
  468. logger.info(`pulling stories from users:${userIds.map(id => ` @${this.cache[id].user.username}`)}`);
  469. return this.client.feed.reelsMedia({userIds}).request()
  470. .then(({reels}) => chainPromises(
  471. Object.keys(reels).map(userId => this.cache[userId]).map(({user, stories}) => () =>
  472. this.queryUserObject(user.username)
  473. .catch((error: IgClientError) => {
  474. if (error instanceof IgExactUserNotFoundError) {
  475. return this.client.user.info(user.pk)
  476. .then(({username, full_name}) => {
  477. user.username = username;
  478. user.full_name = full_name;
  479. });
  480. }
  481. }).finally(() => Promise.all(reels[user.pk].items
  482. .filter(item => !(item.pk in stories))
  483. .map(item => this.webshot(
  484. [{...item, user}],
  485. (msgs: string, text: string, author: string) =>
  486. stories[item.pk] = {pk: item.pk, msgs, text, author, original: item},
  487. this.webshotDelay
  488. ))
  489. )
  490. )
  491. )
  492. ))
  493. .finally(() => json.writeFile(path.resolve(this.cachefile), this.cache).then(() =>
  494. Object.values(this.lock.threads).forEach(thread => {
  495. if (userIds.includes(thread.id)) {
  496. thread.updatedAt = this.cache[thread.id].updated = Date();
  497. }
  498. })
  499. )) as unknown as Promise<void>;
  500. }),
  501. (lp1, lp2) => () =>
  502. lp1().then(() => promisify(setTimeout)(timeout).then(lp2))
  503. );
  504. })
  505. .catch((error: IgClientError & Partial<RequestError>) => {
  506. if (error instanceof IgNetworkError) {
  507. if ((error.cause as Error).message === "Unexpected '<'") {
  508. logger.warn('login required, logging in again...');
  509. return this.session.login().then(this.workForAll);
  510. }
  511. logger.warn(`error while fetching stories for all: ${JSON.stringify(error.cause)}`);
  512. } else if (igErrorIsAuthError(error)) {
  513. logger.warn('login required, logging in again...');
  514. this.session.login().then(this.workForAll);
  515. } else {
  516. logger.error(`unhandled error on fetching media for all: ${error}`);
  517. }
  518. })
  519. .then(() => {
  520. setTimeout(this.workForAll, this.workInterval * 1000);
  521. });
  522. };
  523. public get isInactiveTime() {
  524. const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
  525. return this.inactiveHours
  526. .map(rangeStr => ((start, end) => ({start, end}))(
  527. ...rangeStr
  528. .split('-', 2)
  529. .map(timeStr => timeToEpoch(...timeStr.split(':', 2)
  530. .map(Number))) as [number, number?]
  531. ))
  532. .some(range => {
  533. const now = new Date().getTime();
  534. return now >= range.start && now < range.end;
  535. });
  536. }
  537. public work = () => {
  538. const lock = this.lock;
  539. const timeout = Math.max(1000, this.workInterval * 1000 / lock.feed.length);
  540. const nextTurn = () => { setTimeout(this.work, timeout); return; };
  541. if (lock.feed.length === 0) return nextTurn();
  542. if (lock.workon >= lock.feed.length) lock.workon = 0;
  543. const currentFeed = lock.feed[lock.workon];
  544. if (!lock.threads[currentFeed] ||
  545. !lock.threads[currentFeed].subscribers ||
  546. lock.threads[currentFeed].subscribers.length === 0) {
  547. logger.warn(`nobody subscribes thread ${currentFeed}, removing from feed`);
  548. delete lock.threads[currentFeed];
  549. (this.cache[parseLink(currentFeed).userName] || {} as {pullOrder: number}).pullOrder = 0;
  550. return json.writeFile(path.resolve(this.cachefile), this.cache).then(() => {
  551. lock.feed.splice(lock.workon, 1);
  552. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  553. return this.work();
  554. });
  555. }
  556. logger.debug(`searching for new items from ${currentFeed} in cache`);
  557. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  558. if (!match) {
  559. logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
  560. lock.workon++;
  561. return nextTurn();
  562. }
  563. const cachedFeed = this.cache[lock.threads[currentFeed].id];
  564. if (!cachedFeed) return nextTurn();
  565. const newer = (item: CachedMediaItem) =>
  566. BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  567. const promise = Promise.resolve(Object.values(cachedFeed.stories)
  568. .filter(newer)
  569. .sort((i1, i2) => BigNumOps.compare(i2.pk, i1.pk))
  570. .slice(-5)
  571. );
  572. promise.then((mediaItems: CachedMediaItem[]) => {
  573. const currentThread = lock.threads[currentFeed];
  574. if (!mediaItems || mediaItems.length === 0) return;
  575. const updateOffset = () => currentThread.offset = mediaItems[0].pk;
  576. if (currentThread.offset === '-1') { updateOffset(); return; }
  577. const questionIndex = mediaItems.findIndex(story => story.original.story_questions);
  578. if (questionIndex > 0) mediaItems.splice(0, questionIndex);
  579. if (currentThread.offset === '0') mediaItems.splice(1);
  580. return this.workOnMedia(
  581. mediaItems.slice(0).reverse(),
  582. this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers)
  583. )
  584. .then(updateOffset)
  585. .then(() => {
  586. if (questionIndex > -1) {
  587. currentThread.subscribers.forEach(subscriber => {
  588. const username = cachedFeed.user.username;
  589. const author = `${cachedFeed.user.full_name} (@${username}) `;
  590. this.bot.sendTo(subscriber,
  591. `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${
  592. (questionIndex > 0) ? `\n本次推送已截止于此条动态,下次推送在 ${this.workInterval} 秒后。` : ''
  593. }`
  594. );
  595. });
  596. }
  597. });
  598. })
  599. .then(() => {
  600. lock.workon++;
  601. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  602. nextTurn();
  603. });
  604. };
  605. }