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, + }; +};