diff --git a/lib/routes/duozhuayu/search.js b/lib/routes/duozhuayu/search.js index 0a1b3dd147..93f3809f0c 100644 --- a/lib/routes/duozhuayu/search.js +++ b/lib/routes/duozhuayu/search.js @@ -1,35 +1,79 @@ -const puppeteer = require('@/utils/puppeteer'); -const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const aesjs = require('aes-js'); -module.exports = async (ctx, next) => { +module.exports = async (ctx) => { const wd = ctx.params.wd; const link = `https://www.duozhuayu.com/search/${wd}`; - const browser = await puppeteer(); - const page = await browser.newPage(); - await page.goto(link, { waitUntil: 'networkidle0' }); - // eslint-disable-next-line no-undef - const html = await page.evaluate(() => document.querySelector('html').innerHTML); - browser.close(); - const $ = cheerio.load(html); - const books = $('.result-list').find('.SearchBookItem'); + // token获取见 https://github.com/wong2/userscripts/blob/master/duozhuayu.user.js + const key = 'DkOliWvFNR7C4WvR'.split('').map(function(c) { + return c.charCodeAt(); + }); + const iv = 'GQWKUE2CVGOOBKXU'.split('').map(function(c) { + 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 - .map((index, i) => { - const book = $(i); - const text = book.find('.SearchBookItem-title').text() + ' - ' + book.find('.SearchBookItem-description').text() + ' - ' + book.find('.Price').text(); + .filter((item) => item.type === 'book') + .map((item) => { + const book = item.book; return { - title: text, - link: `https://www.duozhuayu.com${book.attr('href')}`, - description: book.html(), - guid: `https://www.duozhuayu.com${book.attr('href')}${book.find('.Price').text()}`, + title: book.title, + link: `https://www.duozhuayu.com/books/${book.id}`, + pubDate: new Date(book.updated).toUTCString(), + description: ` +
+ 书名:${book.title} ${book.originalTitle}
+ 作者:${book.rawAuthor}
+ ISBN:${book.isbn13}
+ 出版社:${book.publisher}
+ 出版时间:${book.publishDate}
+ 价格:${(book.price / 100).toFixed(2)}元起 ${(book.originalPrice / 100).toFixed(2)}元
+ `, + guid: book.id, }; - }) - .get(); + }); + ctx.state.data = { title: `多抓鱼搜索-${wd}`, link, description: `多抓鱼搜索-${wd}`, item, }; - await next(); }; diff --git a/package.json b/package.json index 92f8153e4b..fc94c42f99 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ }, "dependencies": { "@postlight/mercury-parser": "2.1.1", + "aes-js": "3.1.2", "art-template": "4.13.2", "cheerio": "1.0.0-rc.3", "co-redis": "2.1.1", diff --git a/yarn.lock b/yarn.lock index db75f8288a..a462223529 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1495,6 +1495,11 @@ acorn@^7.0.0: resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.0.0.tgz#26b8d1cd9a9b700350b71c0905546f64d1284e7a" 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: version "4.2.1" resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"