twitter.ts 26 KB

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