| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 | "use strict";Object.defineProperty(exports, "__esModule", { value: true });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 mirai_1 = require("./mirai");const xmlEntities = new html_entities_1.XmlEntities();const typeInZH = {    photo: '[图片]',    video: '[视频]',    animated_gif: '[GIF]',};const logger = loggers_1.getLogger('webshot');class Webshot extends CallableInstance {    constructor(mode, onready) {        super('webshot');        // use local Chromium        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 new Promise(resolve => setTimeout(resolve, 2500))                .then(() => this.connect(onready));        };        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(`data:image/jpg;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 => {                    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: 150000 }))                        // hide header, "more options" button, like and retweet count                        .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.waitFor(webshotDelay))                        .then(() => page.addScriptTag({                        content: 'document.documentElement.scrollTop=0;',                    }))                        .then(() => page.screenshot())                        .then(screenshot => {                        new pngjs_1.PNG({                            filterType: 4,                            deflateLevel: 0,                        }).on('parsed', function () {                            // remove comment area                            // tslint:disable-next-line: no-shadowed-variable                            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) {                                    if (this.data[idx(x, y + 18 * zoomFactor)] !== 255) {                                        // footer kicks in                                        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;                                    // line above the "comment", "retweet", "like", "share" button row                                    if (cnt === 2) {                                        boundary = y + 1;                                    }                                    // if there are a "retweet" count and "like" count row, this will be the line above it                                    if (cnt === 4) {                                        const b = y + 1;                                        if (this.height - boundary - (boundary - b) <= 1) {                                            boundary = b;                                            //   }                                            // }                                            // // if "retweet" count and "like" count are two rows, this will be the line above the first                                            // if (cnt === 6) {                                            //   const c = y + 1;                                            //   if (this.height - boundary - 2 * (boundary - c) <= 2) {                                            //     boundary = c;                                            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);                    })                        .then(() => 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 => new Promise(resolve => this.reconnect(error, resolve))                .then(() => this.renderWebshot(url, height, webshotDelay)));        };        this.fetchImage = (url) => new Promise(resolve => {            logger.info(`fetching ${url}`);            axios_1.default({                method: 'get',                url,                responseType: 'arraybuffer',            }).then(res => {                if (res.status === 200) {                    logger.info(`successfully fetched ${url}`);                    resolve(res.data);                }                else {                    logger.error(`failed to fetch ${url}: ${res.status}`);                    resolve();                }            }).catch(err => {                logger.error(`failed to fetch ${url}: ${err.message}`);                resolve();            });        }).then(data => {            const mimetype = (ext => {                switch (ext) {                    case 'jpg':                        return 'image/jpeg';                    case 'png':                        return 'image/png';                    case 'gif':                        return 'image/gif';                }            })(url.match(/(\.[a-z]+):(.*)/)[1]);            return `data:${mimetype};base64,${Buffer.from(data).toString('base64')}`;        });        // tslint:disable-next-line: no-conditional-assignment        if (this.mode = mode) {            onready();        }        else {            this.connect(onready);        }    }    webshot(tweets, uploader, 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;            const messageChain = [];            // text processing            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.push(mirai_1.Message.Plain(author + xmlEntities.decode(text)));            });            // invoke webshot            if (this.mode === 0) {                const url = `https://mobile.twitter.com/${twi.user.screen_name}/status/${twi.id_str}`;                promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay))                    .then(base64url => {                    if (base64url) {                        return uploader(mirai_1.Message.Image('', base64url, url), () => mirai_1.Message.Plain(author + text));                    }                })                    .then(msg => {                    if (msg)                        messageChain.push(msg);                });            }            // fetch extra images            if (1 - this.mode % 2) {                if (originTwi.extended_entities) {                    originTwi.extended_entities.media.forEach(media => {                        const url = media.media_url_https + ':orig';                        promise = promise.then(() => this.fetchImage(url))                            .then(base64url => uploader(mirai_1.Message.Image('', base64url, url), () => mirai_1.Message.Plain(`[失败的图片:${url}]`)))                            .then(msg => {                            messageChain.push(msg);                        });                    });                }            }            // append URLs, if any            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 => urlObj.expanded_url);                        if (urls.length) {                            messageChain.push(mirai_1.Message.Plain(urls.join('\n')));                        }                    });                }            }            promise.then(() => {                logger.info(`done working on ${twi.user.screen_name}/${twi.id_str}, message chain:`);                logger.info(JSON.stringify(messageChain.map(message => {                    if (message.type === 'Image' && message.url.startsWith('data:')) {                        return mirai_1.Message.Image(message.imageId, 'data:[...]', message.path);                    }                    return message;                })));                callback(messageChain, xmlEntities.decode(text), author);            });        });        return promise;    }}exports.default = Webshot;
 |