From 36224c0a08a19de741248344b2a49bd6ea3b4a98 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 18 Dec 2019 11:58:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20Paul=20Graham=20Es?= =?UTF-8?q?says=20(#3589)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 增加 Paul Graham Essays * refactor: Rename router --- docs/blog.md | 8 ++++++ lib/router.js | 3 +++ lib/routes/blogs/paulgraham.js | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lib/routes/blogs/paulgraham.js diff --git a/docs/blog.md b/docs/blog.md index 86ba009d1d..47ed032072 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -77,3 +77,11 @@ pageClass: routes ### 文章 + +## Paul Graham 博客 + +通过提取文章全文,提供比官方源更佳的阅读体验。 + +### Essays + + diff --git a/lib/router.js b/lib/router.js index d3efaa6024..a3a0037348 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2042,4 +2042,7 @@ router.get('/vulture/:type', require('./routes/vulture/index')); // xinwenlianbo router.get('/xinwenlianbo/index', require('./routes/xinwenlianbo/index')); +// Paul Graham - Essays +router.get('/blogs/paulgraham', require('./routes/blogs/paulgraham')); + module.exports = router; diff --git a/lib/routes/blogs/paulgraham.js b/lib/routes/blogs/paulgraham.js new file mode 100644 index 0000000000..a83e84aeb8 --- /dev/null +++ b/lib/routes/blogs/paulgraham.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); +const parser = require('@/utils/rss-parser'); +const cheerio = require('cheerio'); + +const ProcessFeed = async (link) => { + const response = await got({ + method: 'get', + url: link, + }); + + const $ = cheerio.load(response.data); + + // 提取内容 + return $('font').html(); +}; + +module.exports = async (ctx) => { + const feed = await parser.parseURL('http://www.aaronsw.com/2002/feeds/pgessays.rss'); + + const items = await Promise.all( + feed.items.slice(0, 5).map(async (item) => { + const cache = await ctx.cache.get(item.link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const description = await ProcessFeed(item.link); + + const single = { + title: item.title, + description, + link: item.link, + author: item.author, + }; + ctx.cache.set(item.link, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: feed.title, + link: 'http://www.paulgraham.com/articles.html', + description: 'Essays by Paul Graham', + item: items, + }; +};