feat(route): add nytimes book bestseller (#7332)

This commit is contained in:
Melvin 2021-05-25 21:35:56 +08:00 committed by GitHub
parent 8bb8e1fcb2
commit 94dd2b5889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 109 additions and 0 deletions

View File

@ -256,6 +256,25 @@ Provides a better reading experience (full text articles) over the official one.
</RouteEn>
### Best Seller Books
<RouteEn author="melvinto" example="/nytimes/book/combined-print-and-e-book-nonfiction" path="/nytimes/book/: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|
## The Wall Street Journal (WSJ)
### News

View File

@ -660,6 +660,24 @@ category 对应的关键词有
<Route author="xyqfer" example="/nytimes/morning_post" path="/nytimes/morning_post"/>
### 畅销书排行榜
<Route author="melvinto" example="/nytimes/book/combined-print-and-e-book-nonfiction" path="/nytimes/book/: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| 青少年 |
## 澎湃新闻
### 首页头条

View File

@ -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

View File

@ -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: `<figure><img src="${imageLink}" alt="test"/><figcaption><span>${description}</span></figcaption></figure><br/>${firstInfo}<br/>Author: ${author}<br/>Publisher: ${publisher}`,
link: primaryLink
};
})
.get();
}
ctx.state.data = {
title: `The New York Times Best Sellers - ${dataTitle}`,
link: url,
item: items,
};
};