forked from chengxuanying/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): add nytimes book bestseller (DIYgod#7332)
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |