From 9ec5d8f72eac23ea60d24553aec89a301457e59f Mon Sep 17 00:00:00 2001 From: Devin <42215787+devinmugen@users.noreply.github.com> Date: Fri, 16 Dec 2022 20:14:52 +0800 Subject: [PATCH] fix: Use puppeteer for picuki (#11458) * Use puppeteer for picuki * close the browser and the page --- lib/v2/picuki/maintainer.js | 2 +- lib/v2/picuki/profile.js | 19 +++++++++++-------- lib/v2/picuki/utils.js | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 lib/v2/picuki/utils.js diff --git a/lib/v2/picuki/maintainer.js b/lib/v2/picuki/maintainer.js index 116545d5a..02a60ac26 100644 --- a/lib/v2/picuki/maintainer.js +++ b/lib/v2/picuki/maintainer.js @@ -1,3 +1,3 @@ module.exports = { - '/profile/:id/:functionalFlag?': ['hoilc', 'Rongronggg9'], + '/profile/:id/:functionalFlag?': ['hoilc', 'Rongronggg9', 'devinmugen'], }; diff --git a/lib/v2/picuki/profile.js b/lib/v2/picuki/profile.js index 269f9cbc3..ef3da4cfa 100644 --- a/lib/v2/picuki/profile.js +++ b/lib/v2/picuki/profile.js @@ -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, diff --git a/lib/v2/picuki/utils.js b/lib/v2/picuki/utils.js new file mode 100644 index 000000000..c86ca37ef --- /dev/null +++ b/lib/v2/picuki/utils.js @@ -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, +};