mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-08 22:19:40 +08:00
refactor: duozhuayu replace puppeteer to api (#2973)
This commit is contained in:
@@ -1,35 +1,79 @@
|
|||||||
const puppeteer = require('@/utils/puppeteer');
|
const got = require('@/utils/got');
|
||||||
const cheerio = require('cheerio');
|
const aesjs = require('aes-js');
|
||||||
|
|
||||||
module.exports = async (ctx, next) => {
|
module.exports = async (ctx) => {
|
||||||
const wd = ctx.params.wd;
|
const wd = ctx.params.wd;
|
||||||
const link = `https://www.duozhuayu.com/search/${wd}`;
|
const link = `https://www.duozhuayu.com/search/${wd}`;
|
||||||
const browser = await puppeteer();
|
|
||||||
const page = await browser.newPage();
|
|
||||||
|
|
||||||
await page.goto(link, { waitUntil: 'networkidle0' });
|
// token获取见 https://github.com/wong2/userscripts/blob/master/duozhuayu.user.js
|
||||||
// eslint-disable-next-line no-undef
|
const key = 'DkOliWvFNR7C4WvR'.split('').map(function(c) {
|
||||||
const html = await page.evaluate(() => document.querySelector('html').innerHTML);
|
return c.charCodeAt();
|
||||||
browser.close();
|
});
|
||||||
const $ = cheerio.load(html);
|
const iv = 'GQWKUE2CVGOOBKXU'.split('').map(function(c) {
|
||||||
const books = $('.result-list').find('.SearchBookItem');
|
return c.charCodeAt();
|
||||||
|
});
|
||||||
|
const aesCfb = new aesjs.ModeOfOperation.cfb(key, iv);
|
||||||
|
|
||||||
|
const encrypt = (text) => {
|
||||||
|
const textBytes = aesjs.utils.utf8.toBytes(text);
|
||||||
|
const encryptedBytes = aesCfb.encrypt(textBytes);
|
||||||
|
return aesjs.utils.hex.fromBytes(encryptedBytes);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getCustomRequestHeaders = () => {
|
||||||
|
const timestamp = Date.now();
|
||||||
|
const userId = 0;
|
||||||
|
const securityKey = Math.floor(1e8 * Math.random());
|
||||||
|
const token = encrypt([timestamp, userId, securityKey].join(':'));
|
||||||
|
const requestId = [userId, timestamp, Math.round(1e5 * Math.random())].join('-');
|
||||||
|
return {
|
||||||
|
'x-timestamp': timestamp,
|
||||||
|
'x-security-key': securityKey,
|
||||||
|
'x-user-id': userId,
|
||||||
|
'x-request-token': token,
|
||||||
|
'x-request-misc': '{"platform":"browser"}',
|
||||||
|
'x-api-version': '0.0.15',
|
||||||
|
'x-request-id': requestId,
|
||||||
|
'x-refer-request-id': requestId,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await got({
|
||||||
|
method: 'get',
|
||||||
|
url: 'https://www.duozhuayu.com/api/search',
|
||||||
|
params: {
|
||||||
|
type: 'normal',
|
||||||
|
q: wd,
|
||||||
|
},
|
||||||
|
headers: getCustomRequestHeaders(),
|
||||||
|
});
|
||||||
|
const books = response.data.data || {};
|
||||||
|
|
||||||
const item = books
|
const item = books
|
||||||
.map((index, i) => {
|
.filter((item) => item.type === 'book')
|
||||||
const book = $(i);
|
.map((item) => {
|
||||||
const text = book.find('.SearchBookItem-title').text() + ' - ' + book.find('.SearchBookItem-description').text() + ' - ' + book.find('.Price').text();
|
const book = item.book;
|
||||||
return {
|
return {
|
||||||
title: text,
|
title: book.title,
|
||||||
link: `https://www.duozhuayu.com${book.attr('href')}`,
|
link: `https://www.duozhuayu.com/books/${book.id}`,
|
||||||
description: book.html(),
|
pubDate: new Date(book.updated).toUTCString(),
|
||||||
guid: `https://www.duozhuayu.com${book.attr('href')}${book.find('.Price').text()}`,
|
description: `
|
||||||
|
<img src="${book.images.origin}" ><br>
|
||||||
|
书名:${book.title} ${book.originalTitle}<br>
|
||||||
|
作者:${book.rawAuthor}<br>
|
||||||
|
ISBN:${book.isbn13}<br>
|
||||||
|
出版社:${book.publisher}<br>
|
||||||
|
出版时间:${book.publishDate}<br>
|
||||||
|
价格:${(book.price / 100).toFixed(2)}元起 <del>${(book.originalPrice / 100).toFixed(2)}元</del><br>
|
||||||
|
`,
|
||||||
|
guid: book.id,
|
||||||
};
|
};
|
||||||
})
|
});
|
||||||
.get();
|
|
||||||
ctx.state.data = {
|
ctx.state.data = {
|
||||||
title: `多抓鱼搜索-${wd}`,
|
title: `多抓鱼搜索-${wd}`,
|
||||||
link,
|
link,
|
||||||
description: `多抓鱼搜索-${wd}`,
|
description: `多抓鱼搜索-${wd}`,
|
||||||
item,
|
item,
|
||||||
};
|
};
|
||||||
await next();
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -52,6 +52,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@postlight/mercury-parser": "2.1.1",
|
"@postlight/mercury-parser": "2.1.1",
|
||||||
|
"aes-js": "3.1.2",
|
||||||
"art-template": "4.13.2",
|
"art-template": "4.13.2",
|
||||||
"cheerio": "1.0.0-rc.3",
|
"cheerio": "1.0.0-rc.3",
|
||||||
"co-redis": "2.1.1",
|
"co-redis": "2.1.1",
|
||||||
|
|||||||
@@ -1495,6 +1495,11 @@ acorn@^7.0.0:
|
|||||||
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.0.0.tgz#26b8d1cd9a9b700350b71c0905546f64d1284e7a"
|
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.0.0.tgz#26b8d1cd9a9b700350b71c0905546f64d1284e7a"
|
||||||
integrity sha512-PaF/MduxijYYt7unVGRuds1vBC9bFxbNf+VWqhOClfdgy7RlVkQqt610ig1/yxTgsDIfW1cWDel5EBbOy3jdtQ==
|
integrity sha512-PaF/MduxijYYt7unVGRuds1vBC9bFxbNf+VWqhOClfdgy7RlVkQqt610ig1/yxTgsDIfW1cWDel5EBbOy3jdtQ==
|
||||||
|
|
||||||
|
aes-js@3.1.2:
|
||||||
|
version "3.1.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.1.2.tgz#db9aabde85d5caabbfc0d4f2a4446960f627146a"
|
||||||
|
integrity sha1-25qr3oXVyqu/wNTypERpYPYnFGo=
|
||||||
|
|
||||||
agent-base@^4.1.0, agent-base@~4.2.1:
|
agent-base@^4.1.0, agent-base@~4.2.1:
|
||||||
version "4.2.1"
|
version "4.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
|
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
|
||||||
|
|||||||
Reference in New Issue
Block a user