|
@@ -25,7 +25,7 @@ const typeInZH = {
|
|
|
video: ZHType('视频'),
|
|
|
animated_gif: ZHType('GIF'),
|
|
|
};
|
|
|
-const logger = loggers_1.getLogger('webshot');
|
|
|
+const logger = (0, loggers_1.getLogger)('webshot');
|
|
|
class Webshot extends CallableInstance {
|
|
|
constructor(wsUrl, mode, onready) {
|
|
|
super('webshot');
|
|
@@ -46,7 +46,7 @@ class Webshot extends CallableInstance {
|
|
|
this.reconnect = (error, onready) => {
|
|
|
logger.error(`connection error, reason: ${error}`);
|
|
|
logger.warn('trying to reconnect in 2.5s...');
|
|
|
- return util_1.promisify(setTimeout)(2500)
|
|
|
+ return (0, util_1.promisify)(setTimeout)(2500)
|
|
|
.then(() => this.connect(onready));
|
|
|
};
|
|
|
this.extendEntity = (media) => {
|
|
@@ -112,7 +112,7 @@ class Webshot extends CallableInstance {
|
|
|
const count = sensitiveToggles.length;
|
|
|
if (count)
|
|
|
logger.info(`found ${count} sensitive ${count === 1 ? 'tweet' : 'tweets'} on page, uncollapsing...`);
|
|
|
- return utils_1.chainPromises(sensitiveToggles.filter(toggle => toggle.isVisible()).map(toggle => () => toggle.click()));
|
|
|
+ return (0, utils_1.chainPromises)(sensitiveToggles.filter(toggle => toggle.isVisible()).map(toggle => () => toggle.click()));
|
|
|
})
|
|
|
.then(() => handle))
|
|
|
.then(handle => handle.$('[data-testid="tweet"]').then(owner => owner ? handle : null))
|
|
@@ -122,7 +122,7 @@ class Webshot extends CallableInstance {
|
|
|
logger.warn(`${err} (${getTimerTime()} ms)`);
|
|
|
return page.evaluate(() => document.documentElement.outerHTML).then(html => {
|
|
|
const path = temp.path({ suffix: '.html' });
|
|
|
- fs_1.writeFileSync(path, html);
|
|
|
+ (0, fs_1.writeFileSync)(path, html);
|
|
|
logger.warn(`saved debug html to ${path}`);
|
|
|
}).then(() => page.screenshot()).then(screenshot => {
|
|
|
sharpToFile(sharp(screenshot).jpeg({ quality: 90 })).then(fileUri => {
|
|
@@ -175,8 +175,8 @@ class Webshot extends CallableInstance {
|
|
|
if (cardImg)
|
|
|
this.extendEntity(cardImg);
|
|
|
})
|
|
|
- .then(() => utils_1.chainPromises(morePostProcessings.map(func => () => func(page))))
|
|
|
- .then(() => util_1.promisify(setTimeout)(getTimeout()))
|
|
|
+ .then(() => (0, utils_1.chainPromises)(morePostProcessings.map(func => () => func(page))))
|
|
|
+ .then(() => (0, util_1.promisify)(setTimeout)(getTimeout()))
|
|
|
.then(() => page.evaluate(() => document.activeElement.blur()))
|
|
|
.then(() => page.screenshot())
|
|
|
.then(screenshot => {
|
|
@@ -236,7 +236,7 @@ class Webshot extends CallableInstance {
|
|
|
};
|
|
|
this.fetchMedia = (url) => new Promise((resolve, reject) => {
|
|
|
logger.info(`fetching ${url}`);
|
|
|
- axios_1.default({
|
|
|
+ (0, axios_1.default)({
|
|
|
method: 'get',
|
|
|
url,
|
|
|
responseType: 'arraybuffer',
|
|
@@ -258,7 +258,7 @@ class Webshot extends CallableInstance {
|
|
|
var _a;
|
|
|
return (ext => {
|
|
|
const mediaTempFilePath = temp.path({ suffix: `.${ext}` });
|
|
|
- fs_1.writeFileSync(mediaTempFilePath, Buffer.from(data));
|
|
|
+ (0, fs_1.writeFileSync)(mediaTempFilePath, Buffer.from(data));
|
|
|
const path = `file://${mediaTempFilePath}`;
|
|
|
switch (ext) {
|
|
|
case 'jpg':
|
|
@@ -280,11 +280,8 @@ class Webshot extends CallableInstance {
|
|
|
}
|
|
|
}
|
|
|
webshot(tweets, callback, webshotDelay) {
|
|
|
- let promise = new Promise(resolve => {
|
|
|
- resolve();
|
|
|
- });
|
|
|
- tweets.forEach((twi, index) => {
|
|
|
- promise = promise.then(() => util_1.promisify(setTimeout)(webshotDelay / 4 * index)).then(() => {
|
|
|
+ const promises = tweets.map((twi, index) => {
|
|
|
+ let promise = (0, util_1.promisify)(setTimeout)(webshotDelay / 4 * index).then(() => {
|
|
|
logger.info(`working on ${twi.user.screen_name}/${twi.id_str}`);
|
|
|
});
|
|
|
const originTwi = twi.retweeted_status || twi;
|
|
@@ -311,20 +308,22 @@ class Webshot extends CallableInstance {
|
|
|
});
|
|
|
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,
|
|
|
- ] });
|
|
|
- };
|
|
|
- this.truncateLongThread = (atId) => {
|
|
|
- if (!atId)
|
|
|
- return;
|
|
|
- logger.info(`thread too long, truncating at tweet ${atId}...`);
|
|
|
- truncatedAt = atId;
|
|
|
- };
|
|
|
- promise = promise.then(() => this.renderWebshot(url, 1920, webshotDelay))
|
|
|
+ promise = promise.then(() => {
|
|
|
+ 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,
|
|
|
+ ] });
|
|
|
+ };
|
|
|
+ this.truncateLongThread = (atId) => {
|
|
|
+ if (!atId)
|
|
|
+ return;
|
|
|
+ logger.info(`thread too long, truncating at tweet ${atId}...`);
|
|
|
+ truncatedAt = atId;
|
|
|
+ };
|
|
|
+ return this.renderWebshot(url, 1920, webshotDelay);
|
|
|
+ })
|
|
|
.then(fileurl => {
|
|
|
if (fileurl)
|
|
|
return koishi_1.Message.Image(fileurl);
|
|
@@ -338,7 +337,7 @@ class Webshot extends CallableInstance {
|
|
|
if (1 - this.mode % 2)
|
|
|
promise = promise.then(() => {
|
|
|
if (originTwi.extended_entities) {
|
|
|
- return utils_1.chainPromises(originTwi.extended_entities.media.map(media => () => {
|
|
|
+ return (0, 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';
|
|
@@ -385,7 +384,7 @@ class Webshot extends CallableInstance {
|
|
|
messageChain += `\n回复此命令查看引用的推文:\n/twitter_view ${blockQuoteIdStr}`;
|
|
|
});
|
|
|
}
|
|
|
- promise.then(() => {
|
|
|
+ return promise.then(() => {
|
|
|
logger.info(`done working on ${twi.user.screen_name}/${twi.id_str}, message chain:`);
|
|
|
logger.info(JSON.stringify(koishi_1.Message.ellipseBase64(messageChain)));
|
|
|
let cacheId = twi.id_str;
|
|
@@ -394,7 +393,7 @@ class Webshot extends CallableInstance {
|
|
|
callback(cacheId, messageChain, xmlEntities.decode(text), author);
|
|
|
});
|
|
|
});
|
|
|
- return promise;
|
|
|
+ return Promise.all(promises).then();
|
|
|
}
|
|
|
}
|
|
|
exports.default = Webshot;
|