From dc0520c753f9927acd2f91b263b4794b692cf61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E6=A5=BD=E5=9D=82=E3=81=BF=E3=81=9A=E3=81=8D?= <45122329+CokeMine@users.noreply.github.com> Date: Tue, 2 Mar 2021 12:56:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=8D=9A=E5=AE=A2?= =?UTF-8?q?=E6=9D=A5=E7=BD=91=E8=B7=AF=E4=B9=A6=E5=BA=97=20(#6969)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: NeverBehave --- docs/reading.md | 6 +++++ lib/router.js | 3 +++ lib/routes/bookscomtw/newbooks.js | 38 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 lib/routes/bookscomtw/newbooks.js diff --git a/docs/reading.md b/docs/reading.md index 0d569e9b10..e6d649c6b6 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -123,6 +123,12 @@ pageClass: routes ::: +## 博客来 + +### 新书出版 + + + ## 吹牛部落 ### 栏目 diff --git a/lib/router.js b/lib/router.js index 03c7fb8de9..bfa8538868 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3982,6 +3982,9 @@ router.get('/iyiou', require('./routes/iyiou')); // 香港商报 router.get('/hkcd/pdf', require('./routes/hkcd/pdf')); +// 博客来 +router.get('/bookscomtw/newbooks/:category', require('./routes/bookscomtw/newbooks')); + // Elite Babes router.get('/elitebabes/videos/:sort?', require('./routes/elitebabes/videos')); router.get('/elitebabes/search/:keyword?', require('./routes/elitebabes/search')); diff --git a/lib/routes/bookscomtw/newbooks.js b/lib/routes/bookscomtw/newbooks.js new file mode 100644 index 0000000000..8ec4dd6e8c --- /dev/null +++ b/lib/routes/bookscomtw/newbooks.js @@ -0,0 +1,38 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +module.exports = async (ctx) => { + const URL = `https://www.books.com.tw/web/${ctx.params.category}/`; + const {data} = await got({ + method: 'get', + url: URL + }); + const $ = cheerio.load(data); + const list = $('div.mod_a div.wrap div.item'); + const items = list.map((i, item) => { + item = $(item); + const title = item.find('h4').text().trim(); + let imgLink = item.find('a img.cover').attr('src').trim(); + imgLink = imgLink.slice(imgLink.indexOf('https', 1), imgLink.indexOf('.jpg') + 4); + const link = item.find('a').attr('href'); + const desc = item.find('div.txt_cont p').text().trim(); + const author = item.find('li.info>a').text(); + const publisher = item.find('li.info span>a').text(); + const pubInfo = item.find('li.info').text(); + let price = item.find('ul.price li.set2 strong'); + price = price.length === 1 ? parseInt(price.text().trim()) : + parseInt(price.eq(0).text() > 10 ? (price.eq(1).text() * 100 / price.eq(0).text()).toFixed() : + (price.eq(1).text() * 10 / price.eq(0).text()).toFixed()); + return { + title, + description: `${desc}
${title}
NT$${price}元
${pubInfo}`, + link: link.slice(0, link.indexOf('?')), + author: `${author} / ${publisher}`, + }; + }).get(); + ctx.state.data = { + title: $('title').text(), + link: URL, + item: items + }; +}; +