|
@@ -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,15 +46,9 @@ 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) => {
|
|
|
- logger.info('not working on a tweet');
|
|
|
- };
|
|
|
- this.truncateLongThread = (atId) => {
|
|
|
- logger.info('not working on a tweet');
|
|
|
- };
|
|
|
this.renderWebshot = (url, height, webshotDelay, ...morePostProcessings) => {
|
|
|
temp.track();
|
|
|
const jpeg = (data) => data.pipe(sharp()).jpeg({ quality: 90, trellisQuantisation: true });
|
|
@@ -112,7 +106,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 +116,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 => {
|
|
@@ -130,53 +124,12 @@ class Webshot extends CallableInstance {
|
|
|
});
|
|
|
}).then(() => null);
|
|
|
})
|
|
|
- .then((handle) => {
|
|
|
+ .then(handle => {
|
|
|
if (handle === null)
|
|
|
throw new puppeteer.errors.TimeoutError();
|
|
|
- return handle.evaluate(div => {
|
|
|
- try {
|
|
|
- const selector = '[data-testid="tweet"] :nth-child(2)>:first-child a';
|
|
|
- const getProfileUrl = () => (div.querySelector(selector) || { href: '' }).href;
|
|
|
- const ownerProfileUrl = getProfileUrl();
|
|
|
- const bottom = div;
|
|
|
- while (div = div.previousElementSibling) {
|
|
|
- if (getProfileUrl() !== ownerProfileUrl || div === bottom.previousElementSibling)
|
|
|
- continue;
|
|
|
- const top = document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
|
|
|
- if (top > 10)
|
|
|
- return div.querySelector('article a[aria-label]').href.replace(/.*\/status\//, '');
|
|
|
- }
|
|
|
- }
|
|
|
- catch (_a) { }
|
|
|
- document.documentElement.scrollTop = 0;
|
|
|
- }).then(this.truncateLongThread).then(() => handle);
|
|
|
+ return (0, utils_1.chainPromises)(morePostProcessings.map(func => () => func(page, handle)));
|
|
|
})
|
|
|
- .then(handle => handle.evaluate(div => {
|
|
|
- const cardImg = div.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(() => utils_1.chainPromises(morePostProcessings.map(func => () => func(page))))
|
|
|
- .then(() => util_1.promisify(setTimeout)(getTimeout()))
|
|
|
+ .then(() => (0, util_1.promisify)(setTimeout)(getTimeout()))
|
|
|
.then(() => page.evaluate(() => document.activeElement.blur()))
|
|
|
.then(() => page.screenshot())
|
|
|
.then(screenshot => {
|
|
@@ -236,7 +189,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 +211,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 +233,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 +261,56 @@ 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) => {
|
|
|
+ const 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) => {
|
|
|
+ const 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.renderWebshot(url, 1920, webshotDelay, (_, tweetHandle) => tweetHandle.evaluate(div => {
|
|
|
+ try {
|
|
|
+ const selector = '[data-testid="tweet"] :nth-child(2)>:first-child a';
|
|
|
+ const getProfileUrl = () => (div.querySelector(selector) || { href: '' }).href;
|
|
|
+ const ownerProfileUrl = getProfileUrl();
|
|
|
+ const bottom = div;
|
|
|
+ while (div = div.previousElementSibling) {
|
|
|
+ if (getProfileUrl() !== ownerProfileUrl || div === bottom.previousElementSibling)
|
|
|
+ continue;
|
|
|
+ const top = document.documentElement.scrollTop = window.scrollY + div.getBoundingClientRect().top;
|
|
|
+ if (top > 10)
|
|
|
+ return div.querySelector('article a[aria-label]').href.replace(/.*\/status\//, '');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (_a) { }
|
|
|
+ document.documentElement.scrollTop = 0;
|
|
|
+ }).then(truncateLongThread), (_, tweetHandle) => tweetHandle.evaluate(div => {
|
|
|
+ const cardImg = div.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)
|
|
|
+ extendEntity(cardImg); })))
|
|
|
.then(fileurl => {
|
|
|
if (fileurl)
|
|
|
return koishi_1.Message.Image(fileurl);
|
|
@@ -338,7 +324,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 +371,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 +380,7 @@ class Webshot extends CallableInstance {
|
|
|
callback(cacheId, messageChain, xmlEntities.decode(text), author);
|
|
|
});
|
|
|
});
|
|
|
- return promise;
|
|
|
+ return Promise.all(promises).then();
|
|
|
}
|
|
|
}
|
|
|
exports.default = Webshot;
|