"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("util"); const axios_1 = require("axios"); const CallableInstance = require("callable-instance"); const html_entities_1 = require("html-entities"); const pngjs_1 = require("pngjs"); const puppeteer = require("puppeteer"); const sharp = require("sharp"); const loggers_1 = require("./loggers"); const koishi_1 = require("./koishi"); const utils_1 = require("./utils"); const xmlEntities = new html_entities_1.XmlEntities(); const ZHType = (type) => new class extends String { constructor() { super(...arguments); this.type = super.toString(); this.toString = () => `[${super.toString()}]`; } }(type); const typeInZH = { photo: ZHType('图片'), video: ZHType('视频'), animated_gif: ZHType('GIF'), }; const logger = loggers_1.getLogger('webshot'); class Webshot extends CallableInstance { constructor(mode, onready) { super('webshot'); this.connect = (onready) => puppeteer.connect({ browserURL: 'http://127.0.0.1:9222' }) .then(browser => this.browser = browser) .then(() => { logger.info('launched puppeteer browser'); if (onready) return onready(); }) .catch(error => this.reconnect(error, onready)); this.reconnect = (error, onready) => { logger.error(`connection error, reason: ${error}`); logger.warn('trying to reconnect in 2.5s...'); return util_1.promisify(setTimeout)(2500) .then(() => this.connect(onready)); }; this.extendEntity = (media) => { logger.info('not working on a tweet'); }; this.renderWebshot = (url, height, webshotDelay) => { const jpeg = (data) => data.pipe(sharp()).jpeg({ quality: 90, trellisQuantisation: true }); const sharpToBase64 = (pic) => new Promise(resolve => { pic.toBuffer().then(buffer => resolve(`base64://${buffer.toString('base64')}`)); }); const promise = new Promise((resolve, reject) => { const width = 720; const zoomFactor = 2; logger.info(`shooting ${width}*${height} webshot for ${url}`); this.browser.newPage() .then(page => { const startTime = new Date().getTime(); const getTimerTime = () => new Date().getTime() - startTime; const getTimeout = () => Math.max(500, webshotDelay - getTimerTime()); page.setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36') .then(() => page.setViewport({ width: width / zoomFactor, height: height / zoomFactor, isMobile: true, deviceScaleFactor: zoomFactor, })) .then(() => page.setBypassCSP(true)) .then(() => page.goto(url, { waitUntil: 'load', timeout: getTimeout() })) .then(() => page.addStyleTag({ content: 'header{display:none!important}path[d=\'M20.207 7.043a1 1 0 0 0-1.414 0L12 13.836 5.207 7.043a1 1 0 0 0-1.414 1.414l7.5 7.5a.996.996 0 0 0 1.414 0l7.5-7.5a1 1 0 0 0 0-1.414z\'],div[role=\'button\']{display: none;}', })) .then(() => page.evaluate(() => { const poll = setInterval(() => { document.querySelectorAll('div[data-testid="placementTracking"]').forEach(container => { if (container) { container.innerHTML = container.innerHTML; clearInterval(poll); } }); }, 250); })) .then(() => page.waitForSelector('article', { timeout: getTimeout() })) .catch((err) => { if (err.name !== 'TimeoutError') throw err; logger.warn(`navigation timed out at ${getTimerTime()} seconds`); return null; }) .then(handle => { if (handle === null) throw new puppeteer.errors.TimeoutError(); }) .then(() => page.evaluate(() => { const cardImg = document.querySelector('div[data-testid^="card.layout"][data-testid$=".media"] img'); if (typeof (cardImg === null || cardImg === void 0 ? void 0 : cardImg.getAttribute('src')) === 'string') { const match = /^(.*\/card_img\/(\d+)\/.+\?format=.*)&name=/.exec(cardImg === null || cardImg === void 0 ? void 0 : cardImg.getAttribute('src')); if (match) { const [media_url_https, id_str] = match.slice(1); return { media_url: media_url_https.replace(/^https/, 'http'), media_url_https, url: '', display_url: '', expanded_url: '', type: 'photo', id: Number(id_str), id_str, sizes: undefined, }; } } })) .then(cardImg => { if (cardImg) this.extendEntity(cardImg); }) .then(() => page.addScriptTag({ content: 'document.documentElement.scrollTop=0;', })) .then(() => util_1.promisify(setTimeout)(getTimeout())) .then(() => page.screenshot()) .then(screenshot => { new pngjs_1.PNG({ filterType: 4, deflateLevel: 0, }).on('parsed', function () { const idx = (x, y) => (this.width * y + x) << 2; let boundary = null; let x = zoomFactor * 2; for (let y = 0; y < this.height; y++) { if (this.data[idx(x, y)] !== 255 && this.data[idx(x, y)] === this.data[idx(x + zoomFactor * 10, y)]) { if (this.data[idx(x, y + 18 * zoomFactor)] !== 255) { boundary = null; } else { boundary = y; } break; } } if (boundary !== null) { logger.info(`found boundary at ${boundary}, cropping image`); this.data = this.data.slice(0, idx(this.width, boundary)); this.height = boundary; boundary = null; x = Math.floor(16 * zoomFactor); let flag = false; let cnt = 0; for (let y = this.height - 1; y >= 0; y--) { if ((this.data[idx(x, y)] === 255) === flag) { cnt++; flag = !flag; } else continue; if (cnt === 2) { boundary = y + 1; } if (cnt === 4) { const b = y + 1; if (this.height - boundary - (boundary - b) <= 1) { boundary = b; break; } } } if (boundary !== null) { logger.info(`found boundary at ${boundary}, trimming image`); this.data = this.data.slice(0, idx(this.width, boundary)); this.height = boundary; } sharpToBase64(jpeg(this.pack())).then(base64 => { logger.info(`finished webshot for ${url}`); resolve({ base64, boundary }); }); } else if (height >= 8 * 1920) { logger.warn('too large, consider as a bug, returning'); sharpToBase64(jpeg(this.pack())).then(base64 => { resolve({ base64, boundary: 0 }); }); } else { logger.info('unable to find boundary, try shooting a larger image'); resolve({ base64: '', boundary }); } }).parse(screenshot); }) .catch(err => { if (err instanceof Error && err.name !== 'TimeoutError') throw err; logger.error(`error shooting webshot for ${url}, could not load web page of tweet`); resolve({ base64: '', boundary: 0 }); }) .finally(() => { page.close(); }); }) .catch(reject); }); return promise.then(data => { if (data.boundary === null) return this.renderWebshot(url, height + 1920, webshotDelay); else return data.base64; }).catch(error => this.reconnect(error) .then(() => this.renderWebshot(url, height, webshotDelay))); }; this.fetchMedia = (url) => new Promise((resolve, reject) => { logger.info(`fetching ${url}`); axios_1.default({ method: 'get', url, responseType: 'arraybuffer', timeout: 150000, }).then(res => { if (res.status === 200) { logger.info(`successfully fetched ${url}`); resolve(res.data); } else { logger.error(`failed to fetch ${url}: ${res.status}`); reject(); } }).catch(err => { logger.error(`failed to fetch ${url}: ${err instanceof Error ? err.message : err}`); reject(); }); }).then(data => { var _a; return (ext => { const base64 = `base64://${Buffer.from(data).toString('base64')}`; switch (ext) { case 'jpg': case 'png': return koishi_1.message.image(base64); case 'mp4': return koishi_1.message.video(base64); } logger.warn('unable to find MIME type of fetched media, failing this fetch'); throw Error(); })(((_a = (/\?format=([a-z]+)&/.exec(url))) !== null && _a !== void 0 ? _a : (/.*\/.*\.([^?]+)/.exec(url)))[1]); }); if (this.mode = mode) { onready(); } else { this.connect(onready); } } webshot(tweets, callback, webshotDelay) { let promise = new Promise(resolve => { resolve(); }); tweets.forEach(twi => { promise = promise.then(() => { logger.info(`working on ${twi.user.screen_name}/${twi.id_str}`); }); const originTwi = twi.retweeted_status || twi; let messageChain = ''; let author = `${twi.user.name} (@${twi.user.screen_name}):\n`; if (twi.retweeted_status) author += `RT @${twi.retweeted_status.user.screen_name}: `; let text = originTwi.full_text; promise = promise.then(() => { if (originTwi.entities && originTwi.entities.urls && originTwi.entities.urls.length) { originTwi.entities.urls.forEach(url => { text = text.replace(new RegExp(url.url, 'gm'), url.expanded_url); }); } if (originTwi.extended_entities) { originTwi.extended_entities.media.forEach(media => { text = text.replace(new RegExp(media.url, 'gm'), this.mode === 1 ? typeInZH[media.type] : ''); }); } if (this.mode > 0) messageChain += (author + xmlEntities.decode(text)); }); if (this.mode === 0) { const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`; this.extendEntity = (cardImg) => { var _a, _b; originTwi.extended_entities = Object.assign(Object.assign({}, originTwi.extended_entities), { media: [ ...(_b = (_a = originTwi.extended_entities) === null || _a === void 0 ? void 0 : _a.media) !== null && _b !== void 0 ? _b : [], cardImg, ] }); }; promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay)) .then(base64url => { if (base64url) return koishi_1.message.image(base64url); return author + text; }) .then(msg => { if (msg) messageChain += msg; }); } if (1 - this.mode % 2) promise = promise.then(() => { if (originTwi.extended_entities) { return utils_1.chainPromises(originTwi.extended_entities.media.map(media => { let url; if (media.type === 'photo') { url = media.media_url_https.replace(/\.([a-z]+)$/, '?format=$1') + '&name=orig'; } else { url = media.video_info.variants .filter(variant => variant.bitrate !== undefined) .sort((var1, var2) => var2.bitrate - var1.bitrate) .map(variant => variant.url)[0]; } const altMessage = `\n[失败的${typeInZH[media.type].type}:${url}]`; return this.fetchMedia(url) .catch(error => { logger.warn('unable to fetch media, sending plain text instead...'); return altMessage; }) .then(msg => { messageChain += msg; }); })); } }); if (this.mode === 0) { if (originTwi.entities && originTwi.entities.urls && originTwi.entities.urls.length) { promise = promise.then(() => { const urls = originTwi.entities.urls .filter(urlObj => urlObj.indices[0] < originTwi.display_text_range[1]) .map(urlObj => `\n\ud83d\udd17 ${urlObj.expanded_url}`); if (urls.length) { messageChain += urls.join(''); } }); } } if (originTwi.is_quote_status) { promise = promise.then(() => { messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${originTwi.quoted_status.id_str}`; }); } promise.then(() => { logger.info(`done working on ${twi.user.screen_name}/${twi.id_str}, message chain:`); logger.info(JSON.stringify(koishi_1.ellipseBase64InMessage(messageChain))); callback(messageChain, xmlEntities.decode(text), author); }); }); return promise; } } exports.default = Webshot;