From 94dd2b5889e90ebd9ed7645675464c6df87e6212 Mon Sep 17 00:00:00 2001 From: Melvin Date: Tue, 25 May 2021 21:35:56 +0800 Subject: [PATCH] feat(route): add nytimes book bestseller (#7332) --- docs/en/traditional-media.md | 19 ++++++++++ docs/traditional-media.md | 18 +++++++++ lib/router.js | 1 + lib/routes/nytimes/book.js | 71 ++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 lib/routes/nytimes/book.js diff --git a/docs/en/traditional-media.md b/docs/en/traditional-media.md index 4bdb4c36859677..b3157007d94e62 100644 --- a/docs/en/traditional-media.md +++ b/docs/en/traditional-media.md @@ -256,6 +256,25 @@ Provides a better reading experience (full text articles) over the official one. +### Best Seller Books + + + + +| Category | +| -------- | +| combined-print-and-e-book-nonfiction | +| hardcover-nonfiction| +| paperback-nonfiction| +| advice-how-to-and-miscellaneous| +| combined-print-and-e-book-fiction| +| hardcover-fiction| +| trade-fiction-paperback| +| childrens-middle-grade-hardcover| +| picture-books| +| series-books| +| young-adult-hardcover| + ## The Wall Street Journal (WSJ) ### News diff --git a/docs/traditional-media.md b/docs/traditional-media.md index 330d12fcb82b92..02f393904b6621 100644 --- a/docs/traditional-media.md +++ b/docs/traditional-media.md @@ -660,6 +660,24 @@ category 对应的关键词有 +### 畅销书排行榜 + + + +| Category | 中文 | +| -------- | -------- | +| combined-print-and-e-book-nonfiction | 非虚构类 - 综合 | +| hardcover-nonfiction| 非虚构类 - 精装本 | +| paperback-nonfiction| 非虚构类 - 平装本 | +| advice-how-to-and-miscellaneous| 工具类 | +| combined-print-and-e-book-fiction| 虚构类 - 综合 | +| hardcover-fiction| 虚构类 - 精装本 | +| trade-fiction-paperback| 虚构类 - 平装本 | +| childrens-middle-grade-hardcover| 儿童 - 中年级 | +| picture-books| 儿童 - 绘本 | +| series-books| 儿童 - 系列图书 | +| young-adult-hardcover| 青少年 | + ## 澎湃新闻 ### 首页头条 diff --git a/lib/router.js b/lib/router.js index 41892a7ed69ebf..dee0cf35abcf4f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -337,6 +337,7 @@ router.get('/yande.re/post/popular_recent/:period', require('./routes/yande.re/p // 纽约时报 router.get('/nytimes/morning_post', require('./routes/nytimes/morning_post')); +router.get('/nytimes/book/:category?', require('./routes/nytimes/book.js')); router.get('/nytimes/:lang?', require('./routes/nytimes/index')); // 3dm diff --git a/lib/routes/nytimes/book.js b/lib/routes/nytimes/book.js new file mode 100644 index 00000000000000..79bb4994a1173d --- /dev/null +++ b/lib/routes/nytimes/book.js @@ -0,0 +1,71 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const categoryList = { + "combined-print-and-e-book-nonfiction" : "非虚构类 - 综合", + "hardcover-nonfiction": "非虚构类 - 精装本", + "paperback-nonfiction": "非虚构类 - 平装本", + "advice-how-to-and-miscellaneous": "工具类", + "combined-print-and-e-book-fiction": "虚构类 - 综合", + "hardcover-fiction": "虚构类 - 精装本", + "trade-fiction-paperback": "虚构类 - 平装本", + "childrens-middle-grade-hardcover": "儿童 - 中年级", + "picture-books": "儿童 - 绘本", + "series-books": "儿童 - 系列图书", + "young-adult-hardcover": "青少年" +}; + +module.exports = async (ctx) => { + const category = ctx.params.category || "combined-print-and-e-book-nonfiction"; + + const url = `https://www.nytimes.com/books/best-sellers/${category}`; + + let items = []; + let dataTitle = ''; + if (categoryList[category]) { + const response = await got({ + method: 'get', + url, + }); + const data = response.data; + const $ = cheerio.load(data); + dataTitle = $('h1').eq(0).text(); + + items = $('article[itemprop=itemListElement]') + .map((index, elem) => { + const $item = $(elem); + const firstInfo = $item.find('p').eq(0).text(); + const $name = $item.find('h3[itemprop=name]'); + const author = $item.find('p[itemprop=author]').text(); + const publisher = $item.find('p[itemprop=publisher]').text(); + const description = $item.find('p[itemprop=description]').text(); + const imageLink = $item.find('img[itemprop=image]').attr('src'); + const $link = $item.find('ul[aria-label="Links to Book Retailers"]'); + const links = $link.find('a').toArray(); + + let primaryLink = links.length > 0 ? $(links[0]).attr('href') : ""; + + for (const link of links) { + const l = $(link); + if (l.text() === "Amazon") { + primaryLink = l.attr('href'); + break; + } + } + + return { + title: `${index + 1}: ${$name.text()}`, + author, + description: `
test
${description}

${firstInfo}
Author: ${author}
Publisher: ${publisher}`, + link: primaryLink + }; + }) + .get(); + } + + ctx.state.data = { + title: `The New York Times Best Sellers - ${dataTitle}`, + link: url, + item: items, + }; +};