Skip to content

Commit

Permalink
feat(route): add nytimes book bestseller (DIYgod#7332)
Browse files Browse the repository at this point in the history
  • Loading branch information
MelvinTo authored May 25, 2021
1 parent 8bb8e1f commit 94dd2b5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/en/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions docs/traditional-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -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| 青少年 |

## 澎湃新闻

### 首页头条
Expand Down
1 change: 1 addition & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions lib/routes/nytimes/book.js
Original file line number Diff line number Diff line change
@@ -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,
};
};

0 comments on commit 94dd2b5

Please sign in to comment.