diff --git a/docs/en/reading.md b/docs/en/reading.md index 7ebac82ba5..8db8c8582c 100644 --- a/docs/en/reading.md +++ b/docs/en/reading.md @@ -57,6 +57,16 @@ Eg: +## Penguin Random House + +### Book Lists + + + +### Articles + + + ## syosetu ### chapter diff --git a/docs/reading.md b/docs/reading.md index e83ce5529f..9ff701babd 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -69,6 +69,16 @@ pageClass: routes +## Penguin Random House + +### Book Lists + + + +### Articles + + + ## SoBooks ### 首页 diff --git a/lib/v2/diskanalyzer/whats-new.js b/lib/v2/diskanalyzer/whats-new.js index e26da81043..03c214fa7c 100644 --- a/lib/v2/diskanalyzer/whats-new.js +++ b/lib/v2/diskanalyzer/whats-new.js @@ -37,6 +37,7 @@ module.exports = async (ctx) => { link: currentUrl, description, pubDate: parseDate(title.match(/\((.*)\)/)[1], ['D MMMM YYYY', 'D MMM YYYY']), + guid: title, }; }); diff --git a/lib/v2/penguin-random-house/articles.js b/lib/v2/penguin-random-house/articles.js new file mode 100644 index 0000000000..86e8afe132 --- /dev/null +++ b/lib/v2/penguin-random-house/articles.js @@ -0,0 +1,27 @@ +const utils = require('./utils'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const link = 'https://www.penguinrandomhouse.com/articles/'; + const res = await got(link); + const $ = cheerio.load(res.data); + + const itemArray = $('.archive-module-half-container,.archive-module-third-container') + .map(function () { + return { + url: $(this).find('a').attr('href'), + title: $(this).find('.archive-module-text').first().text(), + }; + }) + .get(); + + const out = await utils.parseList(itemArray, ctx, utils.parseArticle); + + ctx.state.data = { + title: 'Penguin Random House Articles', + link, + description: 'In-depth interviews, author essays, fascinating essays. Go deeper into the books you love.', + item: out, + }; +}; diff --git a/lib/v2/penguin-random-house/maintainer.js b/lib/v2/penguin-random-house/maintainer.js new file mode 100644 index 0000000000..a2b2bf8fc9 --- /dev/null +++ b/lib/v2/penguin-random-house/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/the-read-down': ['StevenRCE0'], + '/articles': ['StevenRCE0'], +}; diff --git a/lib/v2/penguin-random-house/radar.js b/lib/v2/penguin-random-house/radar.js new file mode 100644 index 0000000000..a1b0210000 --- /dev/null +++ b/lib/v2/penguin-random-house/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'penguinrandomhouse.com': { + _name: 'Penguin Random House', + '.': [ + { + title: 'Penguin Random House Book Lists', + docs: 'https://docs.rsshub.app/reading.html#penguin-random-house', + source: ['/the-read-down'], + target: '/penguin-random-house/the-read-down', + }, + { + title: 'Penguin Random House Articles', + docs: 'https://docs.rsshub.app/reading.html#penguin-random-house', + source: ['/articles'], + target: '/penguin-random-house/articles', + }, + ], + }, +}; diff --git a/lib/v2/penguin-random-house/router.js b/lib/v2/penguin-random-house/router.js new file mode 100644 index 0000000000..ff0201887e --- /dev/null +++ b/lib/v2/penguin-random-house/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/articles', require('./articles')); + router.get('/the-read-down', require('./thereaddown')); +}; diff --git a/lib/v2/penguin-random-house/templates/articleHeader.art b/lib/v2/penguin-random-house/templates/articleHeader.art new file mode 100644 index 0000000000..4d9ece759e --- /dev/null +++ b/lib/v2/penguin-random-house/templates/articleHeader.art @@ -0,0 +1,5 @@ +

+{{ imageAlt }} +
+{{ description }} +

\ No newline at end of file diff --git a/lib/v2/penguin-random-house/templates/book.art b/lib/v2/penguin-random-house/templates/book.art new file mode 100644 index 0000000000..c03b8a134e --- /dev/null +++ b/lib/v2/penguin-random-house/templates/book.art @@ -0,0 +1,4 @@ +{{ imageAlt }} +

{{ title }}

+

{{ author }}

+

{{ description }}

\ No newline at end of file diff --git a/lib/v2/penguin-random-house/thereaddown.js b/lib/v2/penguin-random-house/thereaddown.js new file mode 100644 index 0000000000..b6ecd28b4c --- /dev/null +++ b/lib/v2/penguin-random-house/thereaddown.js @@ -0,0 +1,27 @@ +const utils = require('./utils'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const link = 'https://www.penguinrandomhouse.com/the-read-down/'; + const res = await got(link); + const $ = cheerio.load(res.data); + + const itemArray = $('.archive-module-half-container,.archive-module-third-container') + .map(function () { + return { + url: $(this).find('a').attr('href'), + title: $(this).find('.archive-module-text').first().text(), + }; + }) + .get(); + + const out = await utils.parseList(itemArray, ctx, utils.parseBooks); + + ctx.state.data = { + title: 'Penguin Random House Book Lists', + link, + description: 'Never wonder what to read next! Check out these lists to find your next favorite book.', + item: out, + }; +}; diff --git a/lib/v2/penguin-random-house/utils.js b/lib/v2/penguin-random-house/utils.js new file mode 100644 index 0000000000..69cadfacde --- /dev/null +++ b/lib/v2/penguin-random-house/utils.js @@ -0,0 +1,119 @@ +const cheerio = require('cheerio'); +const got = require('@/utils/got'); +const { art } = require('@/utils/render'); +const path = require('path'); +const { parseDate } = require('@/utils/parse-date'); + +const parseBookInList = (element) => { + const $ = cheerio.load(element); + const title = $('h2').first().text(); + const author = $('h2.author').first().text(); + const description = $('.desc').first().text(); + let imageSrc = $('img.cover__backcover').attr('src'); + let imageAlt = $('img.cover__backcover').attr('alt'); + if (!imageSrc) { + imageSrc = $('img.img-responsive').attr('data-src'); + imageAlt = $('img.img-responsive').attr('alt'); + } + + return art(path.join(__dirname, 'templates/book.art'), { + title, + author, + description, + imageSrc, + imageAlt, + }); +}; + +const parsePubDate = (data) => { + const dateString = data('script') + .get() + .filter((element) => { + const fullString = element.children[0]; + if (!fullString || !fullString.data) { + return false; + } + return fullString.data.includes('post_date'); + })[0]; + if (dateString.length === 0) { + return; + } + + const dateMatch = dateString.children[0].data.match(/(?<="post_date":").*?(?=")/); + if (!dateMatch) { + return; + } + + return parseDate(dateMatch[0]); +}; + +const parseBooks = (element) => { + const $ = cheerio.load(element); + const description = $('h2.read-down-text').first().html(); + let mainBlock = ''; + + $('.awesome-list>li').map((i, element) => { + const appending = parseBookInList(element); + mainBlock += appending; + return appending; + }); + + return { + description, + content: mainBlock, + pubDate: parsePubDate($), + }; +}; + +const parseArticle = (element) => { + const $ = cheerio.load(element); + const description = $('h2.hdr-smalltxt').first().html(); + const imageSrc = $('div.img-block>img').first().attr('src'); + const imageAlt = $('div.img-block>img').first().attr('alt'); + let mainBlock = ''; + + const descriptionBlock = art(path.join(__dirname, 'templates/articleHeader.art'), { + description, + imageSrc, + imageAlt, + }); + + $('div.main-content>p,div.main-content>ul').map((i, element) => { + const appending = cheerio.load(element); + mainBlock += appending.html(); + return appending; + }); + + return { + description: descriptionBlock, + content: mainBlock, + pubDate: parsePubDate($), + }; +}; + +const parseList = (items, ctx, contentParser) => + Promise.all( + items.map((item) => + ctx.cache.tryGet(item.url, async () => { + const itemRes = await got(item.url); + + const itemPage = itemRes.data; + const itemParsed = contentParser(itemPage); + + const result = { + title: item.title, + description: itemParsed.description + '
' + itemParsed.content, + pubDate: itemParsed.pubDate, + link: item.url, + }; + + return result; + }) + ) + ); + +module.exports = { + parseList, + parseBooks, + parseArticle, +};