diff --git a/package.json b/package.json index e25132f1a..7f452b251 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "koa-router": "7.4.0", "lru-cache": "4.1.2", "path-to-regexp": "2.2.0", + "phantom": "^4.0.12", "readall": "1.0.0", "winston": "3.0.0-rc3" } diff --git a/routes/zhihu/zhuanlan.js b/routes/zhihu/zhuanlan.js new file mode 100644 index 000000000..d83c19dcd --- /dev/null +++ b/routes/zhihu/zhuanlan.js @@ -0,0 +1,33 @@ +const phantom = require('phantom'); +const art = require('art-template'); +const path = require('path'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const instance = await phantom.create(); + const page = await instance.createPage(); + await page.on('onResourceRequested', function(requestData){}); + await page.open(`https://zhuanlan.zhihu.com/${id}`); + const content = await page.property('content'); + await instance.exit(); + + const $ = cheerio.load(content); + const list = $(".PostListItem"); + + ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + title: $('title').text(), + link: `https://www.zhihu.com/collection/${id}`, + description: `知乎专栏-${$('title').text()}`, + lastBuildDate: new Date().toUTCString(), + item: list && list.map((index, item) => { + item = $(item); + return { + title: item.find('.PostListItem-title').text(), + description: `内容:${item.find('.PostListItem-summary').text()}`, + link: `https://zhuanlan.zhihu.com${item.find('.PostListItem-info a').attr('href')}` + }; + }).get(), + }); +}; \ No newline at end of file