From 6e806eb42648d69cf43bfb4126a177701bd264bf Mon Sep 17 00:00:00 2001 From: huruji <594613537@qq.com> Date: Wed, 18 Apr 2018 16:50:14 +0800 Subject: [PATCH 1/3] add zhihu:zhuanlan --- package.json | 1 + routes/zhihu/zhuanlan.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 routes/zhihu/zhuanlan.js 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 From 7bf0f484fae5ec40e77575030f91db7e1627f4d2 Mon Sep 17 00:00:00 2001 From: huruji <594613537@qq.com> Date: Wed, 18 Apr 2018 16:56:11 +0800 Subject: [PATCH 2/3] add zhuanlan router --- router.js | 1 + 1 file changed, 1 insertion(+) diff --git a/router.js b/router.js index 32afeb221..21e0bca79 100644 --- a/router.js +++ b/router.js @@ -45,6 +45,7 @@ router.get('/jianshu/user/:id', require('./routes/jianshu/user')); // // 知乎 router.get('/zhihu/collection/:id', require('./routes/zhihu/collection')); router.get('/zhihu/people/activities/:id', require('./routes/zhihu/activities')); +router.get('/zhihu/zhuanlan/:id', require('./routes/zhihu/zhuanlan')); // // 贴吧 router.get('/tieba/forum/:kw', require('./routes/tieba/forum')); From c00bec16f8439d4b85074c1693a1c084ea9391ac Mon Sep 17 00:00:00 2001 From: huruji <594613537@qq.com> Date: Wed, 18 Apr 2018 19:24:01 +0800 Subject: [PATCH 3/3] replace phantom with api, update doc --- docs/README.md | 8 +++++++ routes/zhihu/zhuanlan.js | 50 ++++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/docs/README.md b/docs/README.md index 388ed5c37..c9305c083 100644 --- a/docs/README.md +++ b/docs/README.md @@ -263,6 +263,14 @@ s 参数: id,用户 id,可在用户主页 URL 中找到 +#### 专栏 + +举例: https://rss.now.sh/zhihu/zhuanlan/googledevelopers + +路由: `/zhihu/zhuanlan/:id` + +参数: id,专栏 id,可在专栏主页 URL 中找到 + ### 自如 #### 房源 diff --git a/routes/zhihu/zhuanlan.js b/routes/zhihu/zhuanlan.js index d83c19dcd..a04b42241 100644 --- a/routes/zhihu/zhuanlan.js +++ b/routes/zhihu/zhuanlan.js @@ -1,33 +1,43 @@ -const phantom = require('phantom'); +const axios = require('axios'); const art = require('art-template'); const path = require('path'); -const cheerio = require('cheerio'); +const config = require('../../config'); 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 listRes = await axios({ + method: 'get', + url: `https://zhuanlan.zhihu.com/api/columns/${id}/posts?limit=20`, + headers: { + 'User-Agent': config.ua, + 'Referer': `https://zhuanlan.zhihu.com/${id}`, + } + }); + const infoRes = await axios({ + method: 'get', + url: `https://zhuanlan.zhihu.com/api/columns/${id}`, + headers: { + 'User-Agent': config.ua, + 'Referer': `https://zhuanlan.zhihu.com/${id}`, + } + }); - const $ = cheerio.load(content); - const list = $(".PostListItem"); + const list = listRes.data; + const info = infoRes.data; ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { - title: $('title').text(), - link: `https://www.zhihu.com/collection/${id}`, - description: `知乎专栏-${$('title').text()}`, + title: `知乎专栏-${info.name}`, + link: `https://zhuanlan.zhihu.com/${id}`, + description: info.description, 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')}` + item: list.map((item) => { + return { + title: item.title, + description: item.content, + pubDate: new Date(item.publishedTime).toUTCString(), + link: `https://zhuanlan.zhihu.com${item.url}` }; - }).get(), + }), }); }; \ No newline at end of file