add zhihu:zhuanlan

This commit is contained in:
huruji 2018-04-18 16:50:14 +08:00
parent f113a5b1e8
commit 6e806eb426
2 changed files with 34 additions and 0 deletions

View File

@ -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"
}

33
routes/zhihu/zhuanlan.js Normal file
View File

@ -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(),
});
};