123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.sendAllStories = exports.sendStory = exports.sendTimeline = exports.ScreenNameNormalizer = exports.SessionManager = exports.parseLink = exports.linkBuilder = void 0;
- const crypto = require("crypto");
- const fs = require("fs");
- const http = require("http");
- const path = require("path");
- const url_1 = require("url");
- const util_1 = require("util");
- const instagram_private_api_1 = require("instagram-private-api");
- const socks_proxy_agent_1 = require("socks-proxy-agent");
- const datetime_1 = require("./datetime");
- const loggers_1 = require("./loggers");
- const utils_1 = require("./utils");
- const webshot_1 = require("./webshot");
- const parseLink = (link) => {
- let match = /instagram\.com\/stories\/([^\/?#]+)\/(\d+)/.exec(link);
- if (match)
- return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0], storyId: match[2] };
- match =
- /instagram\.com\/([^\/?#]+)/.exec(link) ||
- /^([^\/?#]+)$/.exec(link);
- if (match)
- return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] };
- return;
- };
- exports.parseLink = parseLink;
- const linkBuilder = (config) => {
- if (!config.userName)
- return;
- if (!config.storyId)
- return `https://www.instagram.com/${config.userName}/`;
- return `https://www.instagram.com/stories/${config.userName}/${config.storyId}/`;
- };
- exports.linkBuilder = linkBuilder;
- class SessionManager {
- constructor(client, file, credentials, codeServicePort) {
- this.init = () => {
- this.ig.state.generateDevice(this.username);
- this.ig.request.end$.subscribe(() => { this.save(); });
- const filePath = path.resolve(this.lockfile);
- if (fs.existsSync(filePath)) {
- try {
- const serialized = JSON.parse(fs.readFileSync(filePath, 'utf8'));
- return this.ig.state.deserialize(serialized).then(() => {
- logger.info(`successfully loaded client session cookies for user ${this.username}`);
- });
- }
- catch (err) {
- logger.error(`failed to load client session cookies from file ${this.lockfile}: `, err);
- return Promise.resolve();
- }
- }
- else {
- return this.login().catch((err) => {
- logger.error(`error while trying to log in as user ${this.username}, error: ${err}`);
- logger.warn('attempting to retry after 1 minute...');
- if (fs.existsSync(filePath))
- fs.unlinkSync(filePath);
- util_1.promisify(setTimeout)(60000).then(this.init);
- });
- }
- };
- this.handle2FA = (submitter) => new Promise((resolve, reject) => {
- const token = crypto.randomBytes(20).toString('hex');
- logger.info('please submit the code with a one-time token from your browser with this path:');
- logger.info(`/confirm-2fa?code=<the code you received>&token=${token}`);
- let working;
- const server = http.createServer((req, res) => {
- const { pathname, query } = url_1.parse(req.url, true);
- if (!working && pathname === '/confirm-2fa' && query.token === token &&
- typeof (query.code) === 'string' && /^\d{6}$/.test(query.code)) {
- const code = query.code;
- logger.debug(`received code: ${code}`);
- working = true;
- submitter(code)
- .then(response => { res.write('OK'); res.end(); server.close(() => resolve(response)); })
- .catch(err => { res.write('Error'); res.end(); reject(err); })
- .finally(() => { working = false; });
- }
- });
- server.listen(this.codeServicePort);
- });
- this.login = () => this.ig.simulate.preLoginFlow()
- .then(() => this.ig.account.login(this.username, this.password))
- .catch((err) => {
- if (err instanceof instagram_private_api_1.IgLoginTwoFactorRequiredError) {
- const { two_factor_identifier, totp_two_factor_on } = err.response.body.two_factor_info;
- logger.debug(`2FA info: ${JSON.stringify(err.response.body.two_factor_info)}`);
- logger.info(`login is requesting two-factor authentication via ${totp_two_factor_on ? 'TOTP' : 'SMS'}`);
- return this.handle2FA(code => this.ig.account.twoFactorLogin({
- username: this.username,
- verificationCode: code,
- twoFactorIdentifier: two_factor_identifier,
- verificationMethod: totp_two_factor_on ? '0' : '1',
- }));
- }
- throw err;
- })
- .then(user => new Promise(resolve => {
- logger.info(`successfully logged in as ${this.username}`);
- process.nextTick(() => resolve(this.ig.simulate.postLoginFlow().then(() => user)));
- }));
- this.save = () => this.ig.state.serialize()
- .then((serialized) => {
- delete serialized.constants;
- return fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(serialized, null, 2), 'utf-8');
- });
- this.ig = client;
- this.lockfile = file;
- [this.username, this.password] = credentials;
- this.codeServicePort = codeServicePort;
- }
- }
- exports.SessionManager = SessionManager;
- class ScreenNameNormalizer {
- static normalizeLive(username) {
- return __awaiter(this, void 0, void 0, function* () {
- if (this._queryUser) {
- return yield this._queryUser(username)
- .catch((err) => {
- if (!(err instanceof instagram_private_api_1.IgExactUserNotFoundError)) {
- logger.warn(`error looking up user: ${err.message}`);
- return `${username}:`;
- }
- return null;
- });
- }
- return this.normalize(username);
- });
- }
- }
- exports.ScreenNameNormalizer = ScreenNameNormalizer;
- ScreenNameNormalizer.normalize = (username) => `${username.toLowerCase().replace(/^@/, '')}:`;
- let sendTimeline = (username, receiver) => {
- throw Error();
- };
- exports.sendTimeline = sendTimeline;
- let sendStory = (username, storyId, receiver) => {
- throw Error();
- };
- exports.sendStory = sendStory;
- let sendAllStories = (username, receiver, startIndex, count) => {
- throw Error();
- };
- exports.sendAllStories = sendAllStories;
- const logger = loggers_1.getLogger('instagram');
- const maxTrials = 3;
- const retryInterval = 1500;
- const ordinal = (n) => {
- switch ((Math.trunc(n / 10) % 10 === 1) ? 0 : n % 10) {
- case 1:
- return `${n}st`;
- case 2:
- return `${n}nd`;
- case 3:
- return `${n}rd`;
- default:
- return `${n}th`;
- }
- };
- const retryOnError = (doWork, onRetry) => new Promise(resolve => {
- const retry = (reason, count) => {
- setTimeout(() => {
- let terminate = false;
- onRetry(reason, count, defaultValue => { terminate = true; resolve(defaultValue); });
- if (!terminate)
- doWork().then(resolve).catch(error => retry(error, count + 1));
- }, retryInterval);
- };
- doWork().then(resolve).catch(error => retry(error, 1));
- });
- class default_1 {
- constructor(opt) {
- this.launch = () => {
- this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => {
- setTimeout(this.workForAll, this.workInterval * 1000 / this.lock.feed.length);
- setTimeout(() => {
- this.work();
- setInterval(() => { this.pullOrders = utils_1.Arr.shuffle(this.pullOrders); }, 21600000);
- setInterval(this.workForAll, this.workInterval * 1000);
- }, this.workInterval * 1200 / this.lock.feed.length);
- });
- };
- this.queryUser = (rawUserName) => {
- const username = ScreenNameNormalizer.normalize(rawUserName).split(':')[0];
- if (username in this.cache) {
- return Promise.resolve(`${username}:${this.cache[username].user.pk}`);
- }
- return this.client.user.searchExact(username)
- .catch((error) => {
- if (error instanceof instagram_private_api_1.IgLoginRequiredError || error instanceof instagram_private_api_1.IgCookieNotFoundError) {
- logger.warn('login required, logging in again...');
- return this.session.login().then(() => this.client.user.searchExact(username));
- }
- else
- throw error;
- })
- .then(user => {
- logger.info(`initialized cache item for user ${user.full_name} (@${username})`);
- this.cache[user.username] = { user, stories: {}, pullOrder: 0 };
- return `${user.username}:${user.pk}`;
- });
- };
- this.workOnMedia = (mediaItems, sendMedia) => Promise.resolve(mediaItems.forEach(({ msgs, text, author }) => sendMedia(msgs, text, author)));
- this.sendStories = (source, ...to) => (msg, text, author) => {
- to.forEach(subscriber => {
- logger.info(`pushing data${source ? ` of ${source}` : ''} to ${JSON.stringify(subscriber)}`);
- retryOnError(() => this.bot.sendTo(subscriber, msg), (_, count, terminate) => {
- if (count <= maxTrials) {
- logger.warn(`retry sending to ${subscriber.chatID} for the ${ordinal(count)} time...`);
- }
- else {
- logger.warn(`${count - 1} consecutive failures while sending message chain, trying plain text instead...`);
- terminate(this.bot.sendTo(subscriber, author + text, true));
- }
- });
- });
- };
- this.cache = {};
- this.workForAll = () => {
- if (this.isInactiveTime)
- return;
- logger.debug(`current cache: ${JSON.stringify(this.cache)}`);
- utils_1.chainPromises(Object.entries(this.lock.threads).map(([feed, thread]) => {
- const id = thread.id;
- const userName = parseLink(feed).userName;
- logger.debug(`preparing to add user @${userName} to next pull task...`);
- return (map = {}) => {
- if (userName in this.cache) {
- const item = this.cache[userName];
- if (item.pullOrder === 0)
- item.pullOrder = -1;
- return Promise.resolve(Object.assign(map, { [id]: item.user }));
- }
- return util_1.promisify(setTimeout)((Math.random() * 2 + 1) * 5000).then(() => this.client.user.info(id).then(user => {
- logger.info(`initialized cache item for user ${user.full_name} (@${userName})`);
- this.cache[userName] = { user, stories: {}, pullOrder: -1 };
- return Object.assign(map, { [id]: user });
- }));
- };
- }))
- .then(idToUserMap => {
- const userIdCache = Object.values(this.cache).some(item => item.pullOrder < 0) ?
- this.pullOrders = utils_1.Arr.shuffle(Object.keys(idToUserMap)).map(Number) :
- this.pullOrders;
- return utils_1.chainPromises(utils_1.Arr.chunk(userIdCache, 20).map(userIds => () => {
- const itemToUserName = (item) => idToUserMap[item.user.pk].username;
- logger.info(`pulling stories from users:${userIds.map(id => ` @${idToUserMap[id].username}`)}`);
- return this.client.feed.reelsMedia({ userIds }).items()
- .then(storyItems => Promise.all(storyItems
- .filter(item => !(item.pk in this.cache[itemToUserName(item)].stories))
- .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))))
- .finally(() => Object.values(this.lock.threads).forEach(thread => {
- if (userIds.includes(thread.id)) {
- thread.updatedAt = (this.cache[idToUserMap[thread.id].username].updated = new Date()).toString();
- }
- }));
- }), (lp1, lp2) => () => lp1().then(() => util_1.promisify(setTimeout)(this.workInterval * 1000 / this.lock.feed.length).then(lp2)));
- })
- .catch((error) => {
- if (error instanceof instagram_private_api_1.IgNetworkError) {
- if (error.cause.message === "Unexpected '<'") {
- logger.warn('login required, logging in again...');
- return this.session.login().then(this.workForAll);
- }
- logger.warn(`error while fetching stories for all: ${JSON.stringify(error.cause)}`);
- }
- else if (error instanceof instagram_private_api_1.IgLoginRequiredError || error instanceof instagram_private_api_1.IgCookieNotFoundError) {
- logger.warn('login required, logging in again...');
- this.session.login().then(this.workForAll);
- }
- else {
- logger.error(`unhandled error on fetching media for all: ${error}`);
- }
- });
- };
- this.work = () => {
- const lock = this.lock;
- if (this.workInterval < 1)
- this.workInterval = 1;
- if (this.isInactiveTime || lock.feed.length === 0) {
- setTimeout(() => {
- this.workForAll();
- setTimeout(this.work, this.workInterval * 200);
- }, this.workInterval * 1000 / lock.feed.length);
- return;
- }
- if (lock.workon >= lock.feed.length)
- lock.workon = 0;
- const currentFeed = lock.feed[lock.workon];
- if (!lock.threads[currentFeed] ||
- !lock.threads[currentFeed].subscribers ||
- lock.threads[currentFeed].subscribers.length === 0) {
- logger.warn(`nobody subscribes thread ${currentFeed}, removing from feed`);
- delete lock.threads[currentFeed];
- (this.cache[parseLink(currentFeed).userName] || {}).pullOrder = 0;
- lock.feed.splice(lock.workon, 1);
- fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
- this.work();
- return;
- }
- logger.debug(`searching for new items from ${currentFeed} in cache`);
- const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed);
- if (!match) {
- logger.error(`current feed "${currentFeed}" is invalid, please remove this feed manually`);
- lock.workon++;
- setTimeout(this.work, this.workInterval * 1000 / lock.feed.length);
- return;
- }
- const cachedFeed = this.cache[match[1]];
- if (!cachedFeed) {
- setTimeout(this.work, this.workInterval * 1000 / lock.feed.length);
- return;
- }
- const newer = (item) => utils_1.BigNumOps.compare(item.pk, lock.threads[currentFeed].offset) > 0;
- const promise = Promise.resolve(Object.values(cachedFeed.stories)
- .filter(newer)
- .sort((i1, i2) => utils_1.BigNumOps.compare(i2.pk, i1.pk))
- .slice(-5));
- promise.then((mediaItems) => {
- const currentThread = lock.threads[currentFeed];
- if (!mediaItems || mediaItems.length === 0)
- return;
- const question = mediaItems.find(story => story.original.story_questions);
- const topOfFeed = question ? question.pk : mediaItems[0].pk;
- const updateOffset = () => currentThread.offset = topOfFeed;
- if (currentThread.offset === '-1') {
- updateOffset();
- return;
- }
- if (currentThread.offset === '0')
- mediaItems.splice(1);
- return this.workOnMedia(mediaItems.reverse(), this.sendStories(`thread ${currentFeed}`, ...currentThread.subscribers))
- .then(updateOffset)
- .then(() => {
- if (question) {
- currentThread.subscribers.forEach(subscriber => {
- const username = cachedFeed.user.username;
- const author = `${cachedFeed.user.full_name} (@${username}) `;
- this.bot.sendTo(subscriber, `请注意,用户${author}已开启问答互动。需退订请回复:/igstory_unsub ${username}${Object.keys(cachedFeed.stories).some(id => id > topOfFeed) ?
- `\n本次推送已截止于此条动态,下次推送在 ${Math.floor(this.workInterval * 1000 / lock.feed.length)} 秒后。` : ''}`);
- });
- }
- });
- })
- .then(() => {
- lock.workon++;
- let timeout = this.workInterval * 1000 / lock.feed.length;
- if (timeout < 1000)
- timeout = 1000;
- fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock));
- setTimeout(this.work, timeout);
- });
- };
- this.client = new instagram_private_api_1.IgApiClient();
- if (opt.proxyUrl) {
- try {
- const url = new URL(opt.proxyUrl);
- if (!/^socks(?:4a?|5h?)?:$/.test(url.protocol))
- throw Error();
- if (!url.port)
- url.port = '1080';
- this.client.request.defaults.agent = new socks_proxy_agent_1.SocksProxyAgent({
- hostname: url.hostname,
- port: url.port,
- userId: url.username,
- password: url.password,
- });
- }
- catch (e) {
- logger.warn(`invalid socks proxy url: ${opt.proxyUrl}, ignoring`);
- }
- }
- this.session = new SessionManager(this.client, opt.sessionLockfile, opt.credentials, opt.codeServicePort);
- this.lockfile = opt.lockfile;
- this.lock = opt.lock;
- this.inactiveHours = opt.inactiveHours;
- this.workInterval = opt.workInterval;
- this.bot = opt.bot;
- this.webshotDelay = opt.webshotDelay;
- this.mode = opt.mode;
- this.wsUrl = opt.wsUrl;
- const workNow = (config) => {
- const { action, retryAction, reply, rawUserName } = config;
- return this.queryUser(rawUserName)
- .then(userNameId => {
- var _a, _b;
- const [userName, userId] = userNameId.split(':');
- 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 &&
- Object.keys(this.cache[userName].stories).length > 0) {
- return userName;
- }
- return this.client.feed.reelsMedia({ userIds: [userId] }).items()
- .then(storyItems => Promise.all(storyItems
- .filter(item => !(item.pk in this.cache[userName].stories))
- .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()));
- })
- .then(action)
- .catch((error) => {
- if (error instanceof instagram_private_api_1.IgExactUserNotFoundError) {
- reply(`找不到用户 ${rawUserName.replace(/^@?(.*)$/, '@$1')}。`);
- }
- if (error instanceof instagram_private_api_1.IgNetworkError) {
- if (error.cause.message === "Unexpected '<'") {
- logger.warn('login required, logging in again...');
- return this.session.login().then(retryAction);
- }
- logger.warn(`error while fetching stories for ${rawUserName}: ${JSON.stringify(error.cause)}`);
- reply(`获取 Stories 时出现错误:原因: ${error.cause}`);
- }
- else if (error instanceof instagram_private_api_1.IgLoginRequiredError || error instanceof instagram_private_api_1.IgCookieNotFoundError) {
- logger.warn('login required, logging in again...');
- reply('等待登陆中,稍后会处理请求,请稍候……');
- this.session.login().then(retryAction);
- }
- else {
- logger.error(`unhandled error while fetching stories for ${rawUserName}: ${error}`);
- reply(`获取 Stories 时发生未知错误: ${error}`);
- }
- });
- };
- ScreenNameNormalizer._queryUser = this.queryUser;
- exports.sendTimeline = (rawUserName, receiver) => {
- const reply = msg => this.bot.sendTo(receiver, msg);
- workNow({
- rawUserName,
- action: userName => {
- const storyItems = Object.values(this.cache[userName].stories)
- .sort((i1, i2) => -utils_1.BigNumOps.compare(i2.pk, i1.pk));
- if (storyItems.length === 0)
- return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
- return reply('#. 编号:发送时间\n' + storyItems.map(({ original }, index) => `\n${index + 1}. ${original.pk}: ${datetime_1.relativeDate(original.taken_at * 1000)}`).join(''))
- .then(() => reply(`请使用 /igstory_view ${userName} skip=<#-1> count=1
- 或 /igstory_view https://www.instagram.com/stories/${userName}/<编号>/
- 查看指定的限时动态。`));
- },
- reply,
- retryAction: () => exports.sendTimeline(rawUserName, receiver),
- });
- };
- exports.sendStory = (rawUserName, storyId, receiver) => {
- const reply = msg => this.bot.sendTo(receiver, msg);
- const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
- workNow({
- rawUserName,
- action: userName => {
- if (!(storyId in this.cache[userName].stories))
- return reply('此动态不存在或已过期。');
- return this.workOnMedia([this.cache[userName].stories[storyId]], sender);
- },
- reply,
- retryAction: () => exports.sendStory(rawUserName, storyId, receiver),
- });
- };
- exports.sendAllStories = (rawUserName, receiver, startIndex = 0, count = 10) => {
- const reply = msg => this.bot.sendTo(receiver, msg);
- if (startIndex < 0)
- return reply('跳过数量参数值应为非负整数。');
- if (count < 1)
- return reply('最大查看数量参数值应为正整数。');
- const sender = this.sendStories(`instagram stories for ${rawUserName}`, receiver);
- workNow({
- rawUserName,
- action: userName => {
- const storyItems = Object.values(this.cache[userName].stories)
- .sort((i1, i2) => -utils_1.BigNumOps.compare(i2.pk, i1.pk));
- if (storyItems.length === 0)
- return reply(`当前用户 (@${userName}) 没有可用的 Instagram 限时动态。`);
- if (startIndex + 1 > storyItems.length)
- return reply('跳过数量到达或超过当前用户可用的限时动态数量。');
- const endIndex = Math.min(storyItems.length, startIndex + count);
- const sendRangeText = `${startIndex + 1}${endIndex - startIndex > 1 ? `-${endIndex}` : ''}`;
- return this.workOnMedia(storyItems.slice(startIndex, endIndex), sender)
- .then(() => reply(`已显示当前用户 ${storyItems.length} 条可用限时动态中的第 ${sendRangeText} 条。`));
- },
- reply,
- retryAction: () => exports.sendAllStories(rawUserName, receiver, startIndex, count)
- });
- };
- }
- get pullOrders() {
- const arr = [];
- Object.values(this.cache).forEach(item => { if (item.pullOrder > 0)
- arr[item.pullOrder - 1] = item.user.pk; });
- return arr;
- }
- ;
- set pullOrders(arr) {
- Object.values(this.cache).forEach(item => { item.pullOrder = arr.indexOf(item.user.pk) + 1; });
- }
- get isInactiveTime() {
- const timeToEpoch = (h = 0, m = 0) => new Date().setHours(h, m, 0, 0);
- return this.inactiveHours
- .map(rangeStr => ((start, end) => ({ start, end }))(...rangeStr.split('-', 2).map(timeStr => timeToEpoch(...timeStr.split(':', 2).map(Number)))))
- .some(range => (now => now >= range.start && now < range.end)(Date.now()));
- }
- }
- exports.default = default_1;
|