"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.sendPost = exports.getPostOwner = exports.WebshotHelpers = exports.ScreenNameNormalizer = exports.SessionManager = exports.urlSegmentToId = exports.idToUrlSegment = exports.isValidUrlSegment = exports.parseLink = exports.linkBuilder = void 0; const fs = require("fs"); const path = require("path"); const instagram_id_to_url_segment_1 = require("instagram-id-to-url-segment"); Object.defineProperty(exports, "idToUrlSegment", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.instagramIdToUrlSegment; } }); Object.defineProperty(exports, "urlSegmentToId", { enumerable: true, get: function () { return instagram_id_to_url_segment_1.urlSegmentToInstagramId; } }); const instagram_private_api_1 = require("instagram-private-api"); const socks_proxy_agent_1 = require("socks-proxy-agent"); const loggers_1 = require("./loggers"); const koishi_1 = require("./koishi"); const utils_1 = require("./utils"); const webshot_1 = require("./webshot"); const parseLink = (link) => { let match = /instagram\.com\/p\/([A-Za-z0-9\-_]+)/.exec(link); if (match) return { postUrlSegment: match[1] }; match = /instagram\.com\/([^\/?#]+)/.exec(link) || /^([^\/?#]+)$/.exec(link); if (match) return { userName: ScreenNameNormalizer.normalize(match[1]).split(':')[0] }; return; }; exports.parseLink = parseLink; const isValidUrlSegment = (input) => /^[A-Za-z0-9\-_]+$/.test(input); exports.isValidUrlSegment = isValidUrlSegment; const linkBuilder = (config) => { if (config.userName) return `https://www.instagram.com/${config.userName}/`; if (config.postUrlSegment) return `https://www.instagram.com/p/${config.postUrlSegment}/`; }; exports.linkBuilder = linkBuilder; class SessionManager { constructor(client, file, credentials) { 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(); }; this.login = () => this.ig.simulate.preLoginFlow() .then(() => this.ig.account.login(this.username, this.password)) .then(() => new Promise(resolve => { logger.info(`successfully logged in as ${this.username}`); process.nextTick(() => resolve(this.ig.simulate.postLoginFlow())); })); 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; } } 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 browserLogin = (page) => Promise.reject(); let browserSaveCookies = browserLogin; const acceptCookieConsent = (page) => page.click('button:has-text("すべて許可")', { timeout: 5000 }) .then(() => logger.info('accepted cookie consent')) .catch((err) => { if (err.name !== 'TimeoutError') throw err; }); exports.WebshotHelpers = { handleLogin: browserLogin, handleCookieConsent: acceptCookieConsent, }; let getPostOwner = (segmentId) => Promise.reject(); exports.getPostOwner = getPostOwner; let sendPost = (segmentId, receiver) => { throw Error(); }; exports.sendPost = sendPost; 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.webshotCookies = []; this.launch = () => { this.webshot = new webshot_1.default(this.wsUrl, this.mode, () => this.webshotCookies, doOnNewPage => { this.queryUserMedia = ((userName, targetId) => { let page; const url = linkBuilder({ userName }); logger.debug(`pulling ${targetId !== '0' ? `feed ${url} up to ${targetId}` : `top of feed ${url}`}...`); return doOnNewPage(newPage => { page = newPage; let timeout = this.webshotDelay; const startTime = new Date().getTime(); const getTimerTime = () => new Date().getTime() - startTime; const getTimeout = () => Math.max(500, timeout - getTimerTime()); return page.context().addCookies(this.webshotCookies) .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() })) .then(response => { if (response.status() !== 200) { const err = new Error(`error navigating to user page, error was: ${response.status()} ${response.statusText()}`); throw Object.defineProperty(err, 'name', { value: 'ResponseError', }); } }).then(() => acceptCookieConsent(page)) .then(() => (next => Promise.race([ browserLogin(page) .catch((err) => { if (err.name === 'TimeoutError') logger.warn('navigation timed out, assuming login has failed'); throw err; }) .then(() => browserSaveCookies(page)) .then(() => page.goto(url)).then(next), next(), ]))(() => page.waitForSelector('article', { timeout: getTimeout() }))).then(handle => { const postHandler = () => { const toId = (href) => { var _a; return instagram_id_to_url_segment_1.urlSegmentToInstagramId(((_a = /\/p\/(.*)\/$/.exec(href)) !== null && _a !== void 0 ? _a : [])[1]); }; if (targetId === '0') { return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]'))[0].href).then(href => href ? [toId(href)] : null); } return handle.$$eval('a', as => as.filter(a => !a.querySelector('[aria-label="IGTV"]')).map(a => a.href)).then(hrefs => { let id; const itemIds = []; for (const href of hrefs) { id = toId(href); if (id && utils_1.BigNumOps.compare(id, targetId) > 0) itemIds.push(id); else return itemIds; } logger.info('unable to find a smaller id than target, trying on next page...'); return null; }); }; return postHandler().then(itemIds => { if (itemIds) return itemIds; timeout += this.webshotDelay / 2; return handle.$$('a') .then(as => { as.pop().scrollIntoViewIfNeeded(); return as.length + 1; }) .then(loadedCount => page.waitForFunction(count => document.querySelectorAll('article a').length > count, loadedCount)) .then(postHandler); }); }).catch((err) => { if (err.name !== 'TimeoutError' && err.name !== 'ResponseError') throw err; if (err.name === 'ResponseError') { logger.warn(`error while fetching tweets for ${userName}: ${err.message}`); } else logger.warn(`navigation timed out at ${getTimerTime()} ms`); return []; }).then(itemIds => itemIds.map(id => this.lazyGetMediaById(id))); }).finally(() => { page.close(); }); }); setTimeout(this.work, this.workInterval * 1000); }); }; this.queryUser = (username) => this.client.user.searchExact(username) .then(user => `${user.username}:${user.pk}`); this.workOnMedia = (lazyMediaItems, sendMedia) => this.webshot(lazyMediaItems, sendMedia, this.webshotDelay); this.urlSegmentToId = instagram_id_to_url_segment_1.urlSegmentToInstagramId; this.lazyGetMediaById = (id) => ({ pk: id, item: () => this.client.media.info(id).then(media => { const mediaItem = media.items[0]; logger.debug(`api returned media post ${JSON.stringify(mediaItem)} for query id=${id}`); return mediaItem; }), }); this.getMedia = (segmentId, sender) => this.workOnMedia([this.lazyGetMediaById(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId))], sender); this.sendMedia = (source, ...to) => (msg, text, author) => { to.forEach(subscriber => { logger.info(`pushing data${source ? ` of ${koishi_1.Message.ellipseBase64(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)); } }); }); }; this.work = () => { const lock = this.lock; if (this.workInterval < 1) this.workInterval = 1; if (lock.feed.length === 0) { setTimeout(() => { this.work(); }, this.workInterval * 1000); return; } if (lock.workon >= lock.feed.length) lock.workon = 0; if (!lock.threads[lock.feed[lock.workon]] || !lock.threads[lock.feed[lock.workon]].subscribers || lock.threads[lock.feed[lock.workon]].subscribers.length === 0) { logger.warn(`nobody subscribes thread ${lock.feed[lock.workon]}, removing from feed`); delete lock.threads[lock.feed[lock.workon]]; lock.feed.splice(lock.workon, 1); fs.writeFileSync(path.resolve(this.lockfile), JSON.stringify(lock)); this.work(); return; } const currentFeed = lock.feed[lock.workon]; const promise = new Promise(resolve => { const match = /https:\/\/www\.instagram\.com\/([^\/]+)/.exec(currentFeed); if (match) { resolve(this.queryUserMedia(match[1], this.lock.threads[currentFeed].offset)); } resolve([]); }); promise.then((mediaItems) => { const currentThread = lock.threads[currentFeed]; const updateDate = () => currentThread.updatedAt = new Date().toString(); if (!mediaItems || mediaItems.length === 0) { updateDate(); return; } const topOfFeed = 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, this.sendMedia(`thread ${currentFeed}`, ...currentThread.subscribers)) .then(updateDate).then(updateOffset); }) .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); this.lockfile = opt.lockfile; this.webshotCookiesLockfile = opt.webshotCookiesLockfile; this.lock = opt.lock; this.workInterval = opt.workInterval; this.bot = opt.bot; this.webshotDelay = opt.webshotDelay; this.mode = opt.mode; this.wsUrl = opt.wsUrl; const cookiesFilePath = path.resolve(this.webshotCookiesLockfile); try { this.webshotCookies = JSON.parse(fs.readFileSync(cookiesFilePath, 'utf8')); logger.info(`loaded webshot cookies from file ${this.webshotCookiesLockfile}`); } catch (err) { logger.warn(`failed to load webshot cookies from file ${this.webshotCookiesLockfile}: `, err.message); logger.warn('cookies will be saved to this file when needed'); } browserLogin = page => page.fill('input[name="username"]', opt.credentials[0]) .then(() => logger.warn('blocked by login dialog, trying to log in manually...')) .then(() => page.fill('input[name="password"]', opt.credentials[1])) .then(() => page.click('button[type="submit"]')) .then(() => page.click('button:has-text("情報を保存")')); browserSaveCookies = page => page.context().cookies() .then(cookies => { this.webshotCookies = cookies; logger.info('successfully logged in, saving cookies to file...'); fs.writeFileSync(path.resolve(this.webshotCookiesLockfile), JSON.stringify(cookies, null, 2), 'utf-8'); }); exports.WebshotHelpers.handleLogin = page => browserLogin(page) .then(() => page.waitForSelector('img[data-testid="user-avatar"]', { timeout: this.webshotDelay })) .then(() => browserSaveCookies(page)) .catch((err) => { if (err.name === 'TimeoutError') logger.warn('navigation timed out, assuming login has failed'); throw err; }); ScreenNameNormalizer._queryUser = this.queryUser; const parseMediaError = (err) => { if (!(err instanceof instagram_private_api_1.IgResponseError && err.text === 'Media not found or unavailable')) { logger.warn(`error retrieving instagram media: ${err.message}`); return `获取媒体时出现错误:${err.message}`; } return '找不到请求的媒体,它可能已被删除。'; }; exports.getPostOwner = (segmentId) => this.client.media.info(instagram_id_to_url_segment_1.urlSegmentToInstagramId(segmentId)) .then(media => media.items[0].user) .then(user => `${user.username}:${user.pk}`) .catch((err) => { throw Error(parseMediaError(err)); }); exports.sendPost = (segmentId, receiver) => { this.getMedia(segmentId, this.sendMedia(`instagram media ${segmentId}`, receiver)) .catch((err) => { this.bot.sendTo(receiver, parseMediaError(err)); }); }; } } exports.default = default_1;