twitter.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.sendAllStories = exports.ScreenNameNormalizer = exports.SessionManager = exports.parseLink = exports.linkBuilder = void 0;
  13. const crypto = require("crypto");
  14. const fs = require("fs");
  15. const http = require("http");
  16. const path = require("path");
  17. const url_1 = require("url");
  18. const util_1 = require("util");
  19. const instagram_private_api_1 = require("instagram-private-api");
  20. const socks_proxy_agent_1 = require("socks-proxy-agent");
  21. const loggers_1 = require("./loggers");
  22. const utils_1 = require("./utils");
  23. const webshot_1 = require("./webshot");
  24. const parseLink = (link) => {
  25. let match = /instagram\.com\/stories\/([^\/?#]+)\/(\d+)/.exec(link);
  26. if (match)
  27. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0], storyId: match[2] };
  28. match =
  29. /instagram\.com\/([^\/?#]+)/.exec(link) ||
  30. /^([^\/?#]+)$/.exec(link);
  31. if (match)
  32. return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] };
  33. return;
  34. };
  35. exports.parseLink = parseLink;
  36. const linkBuilder = (config) => {
  37. if (!config.userName)
  38. return;
  39. if (!config.storyId)
  40. return `https://www.instagram.com/${config.userName}/`;
  41. return `https://www.instagram.com/stories/${config.userName}/${config.storyId}/`;
  42. };
  43. exports.linkBuilder = linkBuilder;
  44. class SessionManager {
  45. constructor(client, file, credentials, codeServicePort) {
  46. this.init = () => {
  47. this.ig.state.generateDevice(this.username);
  48. this.ig.request.end$.subscribe(() => { this.save(); });
  49. const filePath = path.resolve(this.lockfile);
  50. if (fs.existsSync(filePath)) {
  51. try {
  52. const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8'));
  53. return this.ig.state.deserialize(serialized).then(() => {
  54. logger.info(`successfully loaded client session cookies for user ${this.username}`);
  55. });
  56. }
  57. catch (err) {
  58. logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
  59. return Promise.resolve();
  60. }
  61. }
  62. else {
  63. return this.login().catch((err) => {
  64. logger.error(`error while trying to log in as user ${this.username}, error: ${err}`);
  65. logger.warn('attempting to retry after 1 minute...');
  66. if (fs.existsSync(filePath))
  67. fs.unlinkSync(filePath);
  68. util_1.promisify(setTimeout)(60000).then(this.init);
  69. });
  70. }
  71. };
  72. this.handle2FA = (submitter) => new Promise((resolve, reject) => {
  73. const token = crypto.randomBytes(20).toString('hex');
  74. logger.info('please submit the code with a one-time token from your browser with this path:');
  75. logger.info(`/confirm-2fa?code=<the code you received>&token=${token}`);
  76. let working;
  77. const server = http.createServer((req, res) => {
  78. const { pathname, query } = url_1.parse(req.url, true);
  79. if (!working && pathname === '/confirm-2fa' && query.token === token &&
  80. typeof (query.code) === 'string' && /^\d{6}$/.test(query.code)) {
  81. const code = query.code;
  82. logger.debug(`received code: ${code}`);
  83. working = true;
  84. submitter(code)
  85. .then(response => { res.write('OK'); res.end(); server.close(() => resolve(response)); })
  86. .catch(err => { res.write('Error'); res.end(); reject(err); })
  87. .finally(() => { working = false; });
  88. }
  89. });
  90. server.listen(this.codeServicePort);
  91. });
  92. this.login = () => this.ig.simulate.preLoginFlow()
  93. .then(() => this.ig.account.login(this.username, this.password))
  94. .catch((err) => {
  95. if (err instanceof instagram_private_api_1.IgLoginTwoFactorRequiredError) {
  96. const { two_factor_identifier, totp_two_factor_on } = err.response.body.two_factor_info;
  97. logger.debug(`2FA info: ${JSON.stringify(err.response.body.two_factor_info)}`);
  98. logger.info(`login is requesting two-factor authentication via ${totp_two_factor_on ? 'TOTP' : 'SMS'}`);
  99. return this.handle2FA(code => this.ig.account.twoFactorLogin({
  100. username: this.username,
  101. verificationCode: code,
  102. twoFactorIdentifier: two_factor_identifier,
  103. verificationMethod: totp_two_factor_on ? '0' : '1',
  104. }));
  105. }
  106. throw err;
  107. })
  108. .then(user => new Promise(resolve => {
  109. logger.info(`successfully logged in as ${this.username}`);
  110. process.nextTick(() => resolve(this.ig.simulate.postLoginFlow().then(() => user)));
  111. }));
  112. this.save = () => this.ig.state.serialize()
  113. .then((serialized) => {
  114. delete serialized.constants;
  115. return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
  116. });
  117. this.ig = client;
  118. this.lockfile = file;
  119. [this.username, this.password] = credentials;
  120. this.codeServicePort = codeServicePort;
  121. }
  122. }
  123. exports.SessionManager = SessionManager;
  124. class ScreenNameNormalizer {
  125. static normalizeLive(username) {
  126. return __awaiter(this, void 0, void 0, function* () {
  127. if (this._queryUser) {
  128. return yield this._queryUser(username)
  129. .catch((err) => {
  130. if (!(err instanceof instagram_private_api_1.IgExactUserNotFoundError)) {
  131. logger.warn(`error looking up user: ${err.message}`);
  132. return `${username}:`;
  133. }
  134. return null;
  135. });
  136. }
  137. return this.normalize(username);
  138. });
  139. }
  140. }
  141. exports.ScreenNameNormalizer = ScreenNameNormalizer;
  142. ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
  143. let sendAllStories = (segmentId, receiver, startIndex, count) => {
  144. throw Error();
  145. };
  146. exports.sendAllStories = sendAllStories;
  147. const logger = loggers_1.getLogger('instagram');
  148. const maxTrials = 3;
  149. const retryInterval = 1500;
  150. const ordinal = (n) => {
  151. switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
  152. case 1:
  153. return `${n}st`;
  154. case 2:
  155. return `${n}nd`;
  156. case 3:
  157. return `${n}rd`;
  158. default:
  159. return `${n}th`;
  160. }
  161. };
  162. const retryOnError = (doWork, onRetry) => new Promise(resolve => {
  163. const retry = (reason, count) => {
  164. setTimeout(() => {
  165. let terminate = false;
  166. onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
  167. if (!terminate)
  168. doWork().then(resolve).catch(error => retry(error, count + 1));
  169. }, retryInterval);
  170. };
  171. doWork().then(resolve).catch(error => retry(error, 1));
  172. });
  173. class default_1 {
  174. constructor(opt) {
  175. this.launch = () => {
  176. this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => {
  177. setTimeout(this.workForAll, this.workInterval * 1000 / this.lock.feed.length);
  178. setTimeout(() => {
  179. this.work();
  180. setInterval(() => { this.pullOrders = utils_1.Arr.shuffle(this.pullOrders); }, 21600000);
  181. setInterval(this.workForAll, this.workInterval * 1000);
  182. }, this.workInterval * 1200 / this.lock.feed.length);
  183. });
  184. };
  185. this.queryUser = (rawUserName) => {
  186. const username = ScreenNameNormalizer.normalize(rawUserName).split(':')[0];
  187. if (username in this.cache) {
  188. return Promise.resolve(`${username}:${this.cache[username].user.pk}`);
  189. }
  190. return this.client.user.searchExact(username)
  191. .catch((error) => {
  192. if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
  193. logger.warn('login required, logging in again...');
  194. return this.session.login().then(() => this.client.user.searchExact(username));
  195. }
  196. else
  197. throw error;
  198. })
  199. .then(user => {
  200. logger.info(`initialized cache item for user ${user.full_name} (@${username})`);
  201. this.cache[user.username] = { user, stories: {}, pullOrder: 0 };
  202. return `${user.username}:${user.pk}`;
  203. });
  204. };
  205. this.workOnMedia = (mediaItems, sendMedia) => Promise.resolve(mediaItems.forEach(({ msgs, text, author }) => sendMedia(msgs, text, author)));
  206. this.sendStories = (source, ...to) => (msg, text, author) => {
  207. to.forEach(subscriber => {
  208. logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
  209. retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
  210. if (count <= maxTrials) {
  211. logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
  212. }
  213. else {
  214. logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
  215. terminate(this.bot.sendTo(subscriber, author + text, true));
  216. }
  217. });
  218. });
  219. };
  220. this.cache = {};
  221. this.workForAll = () => {
  222. if (this.isInactiveTime)
  223. return;
  224. logger.debug(`current cache: ${JSON.stringify(this.cache)}`);
  225. utils_1.chainPromises(Object.entries(this.lock.threads).map(([feed, thread]) => {
  226. const id = thread.id;
  227. const userName = parseLink(feed).userName;
  228. logger.debug(`preparing to add user @${userName} to next pull task...`);
  229. return (map = {}) => {
  230. if (userName in this.cache) {
  231. const item = this.cache[userName];
  232. if (item.pullOrder === 0)
  233. item.pullOrder = -1;
  234. return Promise.resolve(Object.assign(map, { [id]: item.user }));
  235. }
  236. return util_1.promisify(setTimeout)((Math.random() * 2 + 1) * 5000).then(() => this.client.user.info(id).then(user => {
  237. logger.info(`initialized cache item for user ${user.full_name} (@${userName})`);
  238. this.cache[userName] = { user, stories: {}, pullOrder: -1 };
  239. return Object.assign(map, { [id]: user });
  240. }));
  241. };
  242. }))
  243. .then(idToUserMap => {
  244. const userIdCache = Object.values(this.cache).some(item => item.pullOrder < 0) ?
  245. this.pullOrders = utils_1.Arr.shuffle(Object.keys(idToUserMap)).map(Number) :
  246. this.pullOrders;
  247. return utils_1.chainPromises(utils_1.Arr.chunk(userIdCache, 20).map(userIds => () => {
  248. const itemToUserName = (item) => idToUserMap[item.user.pk].username;
  249. logger.info(`pulling stories from users:${userIds.map(id => ` @${idToUserMap[id].username}`)}`);
  250. return this.client.feed.reelsMedia({ userIds }).items()
  251. .then(storyItems => Promise.all(storyItems
  252. .filter(item => !(item.pk in this.cache[itemToUserName(item)].stories))
  253. .map(item => this.webshot([Object.assign(Object.assign({}, item), { user: this.cache[itemToUserName(item)].user })], (msgs, text, author) => this.cache[itemToUserName(item)].stories[item.pk] = { pk: item.pk, msgs, text, author, original: item }, this.webshotDelay))))
  254. .finally(() => Object.values(this.lock.threads).forEach(thread => {
  255. if (userIds.includes(thread.id)) {
  256. thread.updatedAt = (this.cache[idToUserMap[thread.id].username].updated = new Date()).toString();
  257. }
  258. }));
  259. }), (lp1, lp2) => () => lp1().then(() => util_1.promisify(setTimeout)(this.workInterval * 1000 / this.lock.feed.length).then(lp2)));
  260. })
  261. .catch((error) => {
  262. if (error instanceof instagram_private_api_1.IgNetworkError) {
  263. if (error.cause.message === "Unexpected '<'") {
  264. logger.warn('login required, logging in again...');
  265. return this.session.login().then(this.workForAll);
  266. }
  267. logger.warn(`error while fetching stories for all: ${JSON.stringify(error.cause)}`);
  268. }
  269. else if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
  270. logger.warn('login required, logging in again...');
  271. this.session.login().then(this.workForAll);
  272. }
  273. else {
  274. logger.error(`unhandled error on fetching media for all: ${error}`);
  275. }
  276. });
  277. };
  278. this.work = () => {
  279. const lock = this.lock;
  280. if (this.workInterval < 1)
  281. this.workInterval = 1;
  282. if (this.isInactiveTime || lock.feed.length === 0) {
  283. setTimeout(() => {
  284. this.workForAll();
  285. setTimeout(this.work, this.workInterval * 200);
  286. }, this.workInterval * 1000 / lock.feed.length);
  287. return;
  288. }
  289. if (lock.workon >= lock.feed.length)
  290. lock.workon = 0;
  291. const currentFeed = lock.feed[lock.workon];
  292. if (!lock.threads[currentFeed] ||
  293. !lock.threads[currentFeed].subscribers ||
  294. lock.threads[currentFeed].subscribers.length === 0) {
  295. logger.warn(`nobody subscribes thread ${currentFeed}, removing from feed`);
  296. delete lock.threads[currentFeed];
  297. this.cache[parseLink(currentFeed).userName].pullOrder = 0;
  298. lock.feed.splice(lock.workon, 1);
  299. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  300. this.work();
  301. return;
  302. }
  303. logger.debug(`searching for new items from ${currentFeed} in cache`);
  304. const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
  305. if (!match) {
  306. logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
  307. lock.workon++;
  308. setTimeout(this.work, this.workInterval * 1000 / lock.feed.length);
  309. return;
  310. }
  311. const cachedFeed = this.cache[match[1]];
  312. if (!cachedFeed) {
  313. setTimeout(this.work, this.workInterval * 1000 / lock.feed.length);
  314. return;
  315. }
  316. const newer = (item) => utils_1.BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
  317. const promise = Promise.resolve(Object.values(cachedFeed.stories)
  318. .filter(newer)
  319. .sort((i1, i2) => utils_1.BigNumOps.compare(i2.pk, i1.pk))
  320. .slice(-5));
  321. promise.then((mediaItems) => {
  322. const currentThread = lock.threads[currentFeed];
  323. if (!mediaItems || mediaItems.length === 0)
  324. return;
  325. const question = mediaItems.find(story => story.original.story_questions);
  326. const topOfFeed = question ? question.pk : mediaItems[0].pk;
  327. const updateOffset = () => currentThread.offset = topOfFeed;
  328. if (currentThread.offset === '-1') {
  329. updateOffset();
  330. return;
  331. }
  332. if (currentThread.offset === '0')
  333. mediaItems.splice(1);
  334. return this.workOnMedia(mediaItems.reverse(), this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers))
  335. .then(updateOffset)
  336. .then(() => {
  337. if (question) {
  338. currentThread.subscribers.forEach(subscriber => {
  339. const username = cachedFeed.user.username;
  340. const author = `${cachedFeed.user.full_name} (@${username}) `;
  341. this.bot.sendTo(subscriber, `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${Object.keys(cachedFeed.stories).some(id => id > topOfFeed) ?
  342. `\n本次推送已截止于此条动态,下次推送在 ${Math.floor(this.workInterval * 1000 / lock.feed.length)} 秒后。` : ''}`);
  343. });
  344. }
  345. });
  346. })
  347. .then(() => {
  348. lock.workon++;
  349. let timeout = this.workInterval * 1000 / lock.feed.length;
  350. if (timeout < 1000)
  351. timeout = 1000;
  352. fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
  353. setTimeout(this.work, timeout);
  354. });
  355. };
  356. this.client = new instagram_private_api_1.IgApiClient();
  357. if (opt.proxyUrl) {
  358. try {
  359. const url = new URL(opt.proxyUrl);
  360. if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol))
  361. throw Error();
  362. if (!url.port)
  363. url.port = '1080';
  364. this.client.request.defaults.agent = new socks_proxy_agent_1.SocksProxyAgent({
  365. hostname: url.hostname,
  366. port: url.port,
  367. userId: url.username,
  368. password: url.password,
  369. });
  370. }
  371. catch (e) {
  372. logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
  373. }
  374. }
  375. this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
  376. this.lockfile = opt.lockfile;
  377. this.lock = opt.lock;
  378. this.inactiveHours = opt.inactiveHours;
  379. this.workInterval = opt.workInterval;
  380. this.bot = opt.bot;
  381. this.webshotDelay = opt.webshotDelay;
  382. this.mode = opt.mode;
  383. this.wsUrl = opt.wsUrl;
  384. ScreenNameNormalizer._queryUser = this.queryUser;
  385. exports.sendAllStories = (rawUserName, receiver, startIndex = 0, count = 10) => {
  386. if (startIndex < 0)
  387. return this.bot.sendTo(receiver, '跳过数量参数值应为非负整数。');
  388. if (count < 1)
  389. return this.bot.sendTo(receiver, '最大查看数量参数值应为正整数。');
  390. const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
  391. this.queryUser(rawUserName)
  392. .then(userNameId => {
  393. var _a, _b;
  394. const [userName, userId] = userNameId.split(':');
  395. if (Date.now() - ((_b = (_a = this.cache[userName]) === null || _a === void 0 ? void 0 : _a.updated) === null || _b === void 0 ? void 0 : _b.getTime()) > this.workInterval * 1000 &&
  396. Object.keys(this.cache[userName].stories).length > 0) {
  397. return userName;
  398. }
  399. return this.client.feed.reelsMedia({ userIds: [userId] }).items()
  400. .then(storyItems => Promise.all(storyItems
  401. .filter(item => !(item.pk in this.cache[userName].stories))
  402. .map(item => this.webshot([Object.assign(Object.assign({}, item), { user: this.cache[userName].user })], (msgs, text, author) => this.cache[userName].stories[item.pk] = { pk: item.pk, msgs, text, author, original: item }, this.webshotDelay))).then(() => userName).finally(() => this.cache[userName].updated = new Date()));
  403. })
  404. .then(userName => {
  405. const storyItems = Object.values(this.cache[userName].stories)
  406. .sort((i1, i2) => -utils_1.BigNumOps.compare(i2.pk, i1.pk));
  407. if (storyItems.length === 0)
  408. return this.bot.sendTo(receiver, `当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
  409. if (startIndex + 1 > storyItems.length)
  410. return this.bot.sendTo(receiver, '跳过数量到达或超过当前用户可用的限时动态数量。');
  411. const endIndex = Math.min(storyItems.length, startIndex + count);
  412. const sendRangeText = `${startIndex + 1}${endIndex - startIndex > 1 ? `-${endIndex}` : ''}`;
  413. return this.workOnMedia(storyItems.slice(startIndex, endIndex), sender)
  414. .then(() => this.bot.sendTo(receiver, `已显示当前用户 ${storyItems.length} 条可用限时动态中的第 ${sendRangeText} 条。`));
  415. })
  416. .catch((error) => {
  417. if (error instanceof instagram_private_api_1.IgExactUserNotFoundError) {
  418. this.bot.sendTo(receiver, `找不到用户 ${rawUserName.replace(/^@?(.*)$/, '@$1')}。`);
  419. }
  420. if (error instanceof instagram_private_api_1.IgNetworkError) {
  421. if (error.cause.message === "Unexpected '<'") {
  422. logger.warn('login required, logging in again...');
  423. return this.session.login().then(() => exports.sendAllStories(rawUserName, receiver, startIndex, count));
  424. }
  425. logger.warn(`error while fetching stories for ${rawUserName}: ${JSON.stringify(error.cause)}`);
  426. this.bot.sendTo(receiver, `获取 Stories 时出现错误:原因: ${error.cause}`);
  427. }
  428. else if (error instanceof instagram_private_api_1.IgLoginRequiredError) {
  429. logger.warn('login required, logging in again...');
  430. this.session.login().then(() => exports.sendAllStories(rawUserName, receiver, startIndex, count));
  431. }
  432. else {
  433. logger.error(`unhandled error while fetching stories for ${rawUserName}: ${error}`);
  434. this.bot.sendTo(receiver, `获取 Stories 时发生未知错误: ${error}`);
  435. }
  436. });
  437. };
  438. }
  439. get pullOrders() {
  440. const arr = [];
  441. Object.values(this.cache).forEach(item => { if (item.pullOrder > 0)
  442. arr[item.pullOrder - 1] = item.user.pk; });
  443. return arr;
  444. }
  445. ;
  446. set pullOrders(arr) {
  447. Object.values(this.cache).forEach(item => { item.pullOrder = arr.indexOf(item.user.pk) + 1; });
  448. }
  449. get isInactiveTime() {
  450. const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
  451. return this.inactiveHours
  452. .map(rangeStr => ((start, end) => ({ start, end }))(...rangeStr.split('-', 2).map(timeStr => timeToEpoch(...timeStr.split(':', 2).map(Number)))))
  453. .some(range => (now => now >= range.start && now < range.end)(Date.now()));
  454. }
  455. }
  456. exports.default = default_1;