From 6ddeb99ebdd246cd22a20fe8b2ba2245877592bb Mon Sep 17 00:00:00 2001 From: kotoyuuko Date: Mon, 13 Apr 2020 19:51:43 +0800 Subject: [PATCH] fix: hhgal needs to bypass YunSuoAutoJump (#4382) --- lib/routes/galgame/hhgal.js | 42 ++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/routes/galgame/hhgal.js b/lib/routes/galgame/hhgal.js index ac8e0abd9..c27ab0cb0 100644 --- a/lib/routes/galgame/hhgal.js +++ b/lib/routes/galgame/hhgal.js @@ -1,14 +1,42 @@ const cheerio = require('cheerio'); +const cloudscraper = require('cloudscraper'); module.exports = async (ctx) => { - const browser = await require('@/utils/puppeteer')(); - const page = await browser.newPage(); - await page.goto('https://www.hhgal.com/'); - const htmlHandle = await page.$('html'); - const html = await page.evaluate((html) => html.innerHTML, htmlHandle); - browser.close(); + const baseUrl = 'https://www.hhgal.com/'; + const stringToHex = (str) => { + let val = ''; + for (let i = 0; i < str.length; i++) { + if (val === '') { + val = str.charCodeAt(i).toString(16); + } else { + val += str.charCodeAt(i).toString(16); + } + } + return val; + }; - const $ = cheerio.load(html); + const jar = cloudscraper.jar(); + const response = await cloudscraper({ + uri: baseUrl, + jar: jar, + }); + let html = null; + + if (response.indexOf('YunSuoAutoJump') > -1) { + const screenData = stringToHex('1280,720'); + const urlData = stringToHex(baseUrl); + jar.setCookie('srcurl=' + urlData, baseUrl); + await cloudscraper({ + uri: baseUrl + '?security_verify_data=' + screenData, + jar: jar, + }); + html = await cloudscraper({ + uri: baseUrl, + jar: jar, + }); + } + + const $ = html === null ? cheerio.load(response) : cheerio.load(html); const list = $('#article-list').find('.article'); ctx.state.data = {