fix: Use puppeteer for picuki (#11458)

* Use puppeteer for picuki

* close the browser and the page
This commit is contained in:
Devin 2022-12-16 20:14:52 +08:00 committed by GitHub
parent 178124ef31
commit 9ec5d8f72e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 9 deletions

View File

@ -1,3 +1,3 @@
module.exports = {
'/profile/:id/:functionalFlag?': ['hoilc', 'Rongronggg9'],
'/profile/:id/:functionalFlag?': ['hoilc', 'Rongronggg9', 'devinmugen'],
};

View File

@ -4,8 +4,13 @@ const chrono = require('chrono-node');
const { art } = require('@/utils/render');
const path = require('path');
const config = require('@/config').value;
const { puppeteerGet } = require('./utils');
module.exports = async (ctx) => {
// use Puppeteer due to the obstacle by cloudflare challenge
const browser = await require('@/utils/puppeteer')();
const id = ctx.params.id;
const displayVideo = ctx.params.functionalFlag !== '0';
const includeStories = ctx.params.functionalFlag === '10';
@ -15,8 +20,8 @@ module.exports = async (ctx) => {
const data = await ctx.cache.tryGet(
profileUrl,
async () => {
const _r = await got(profileUrl);
return _r.data;
const _r = await puppeteerGet(profileUrl, browser);
return _r;
},
config.cache.routeExpire,
false
@ -31,12 +36,8 @@ module.exports = async (ctx) => {
async function getMedia(url) {
const getPost = async () => {
const response = await got(url, {
headers: {
Referer: profileUrl,
},
});
return response.data;
const response = await puppeteerGet(url, browser);
return response;
};
let data = await ctx.cache.tryGet(url, getPost);
if (Object.prototype.toString.call(data) === '[object Object]') {
@ -178,6 +179,8 @@ module.exports = async (ctx) => {
}
}
await browser.close();
ctx.state.data = {
title: `${profileName} (@${id}) - Picuki`,
link: profileUrl,

21
lib/v2/picuki/utils.js Normal file
View File

@ -0,0 +1,21 @@
const puppeteerGet = async (url, browser) => {
const page = await browser.newPage();
// await page.setExtraHTTPHeaders({ referer: host });
await page.setRequestInterception(true);
page.on('request', (request) => {
request.resourceType() === 'document' ? request.continue() : request.abort();
});
await page.goto(url, {
waitUntil: 'domcontentloaded',
});
await page.waitForSelector('.wrapper');
const html = await page.evaluate(() => document.documentElement.innerHTML);
await page.close();
return html;
};
module.exports = {
puppeteerGet,
};