From daa0eb0280b07d9ac8dc6629c7c3e0eaacf34fb0 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 17 Apr 2018 00:24:22 +0800 Subject: [PATCH] zhihu activities --- README.md | 1 + docs/README.md | 8 +++++ router.js | 1 + routes/zhihu/activities.js | 70 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 routes/zhihu/activities.js diff --git a/README.md b/README.md index b1c1e88f7..a524bdde7 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 作者 - 知乎 - 收藏夹 + - 用户动态 - 自如 - 房源 - 快递 diff --git a/docs/README.md b/docs/README.md index 6ff4211e7..5a4f5da62 100644 --- a/docs/README.md +++ b/docs/README.md @@ -255,6 +255,14 @@ s 参数: id,收藏夹 id,可在收藏夹页面 URL 中找到 +#### 用户动态 + +举例: https://rss.prprpr.me/zhihu/people/activities/diygod + +路由: `/zhihu/people/activities/:id` + +参数: id,用户 id,可在用户主页 URL 中找到 + ### 自如 #### 房源 diff --git a/router.js b/router.js index 5df40fb0b..7e2c6f3eb 100644 --- a/router.js +++ b/router.js @@ -35,5 +35,6 @@ 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')); module.exports = router; \ No newline at end of file diff --git a/routes/zhihu/activities.js b/routes/zhihu/activities.js new file mode 100644 index 000000000..5caeaf33c --- /dev/null +++ b/routes/zhihu/activities.js @@ -0,0 +1,70 @@ +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 response = await axios({ + method: 'get', + url: `https://www.zhihu.com/people/${id}/activities`, + headers: { + 'User-Agent': config.ua, + 'Referer': `https://www.zhihu.com/people/${id}/activities`, + 'authorization': 'oauth c3cef7c66a1843f8b3a9e6a1e3160e20', + 'X-API-VERSION': '3.0.40' + } + }); + + const $ = cheerio.load(response.data); + const data = $('#data').data('state').entities; + + const name = data.users[id].name; + const list = Object.values(data.activities).sort((a, b) => b.createdTime - a.createdTime); + + ctx.body = art(path.resolve(__dirname, '../../views/rss.art'), { + title: `${name}的知乎动态`, + link: `https://www.zhihu.com/people/${id}/activities`, + description: data.users[id].headline || data.users[id].description, + lastBuildDate: new Date().toUTCString(), + item: list && list.map((item) => { + const type = item.target.schema; + const detail = data[`${type}s`][item.target.id]; + let title; + let description; + let url; + + switch (type) { + case 'answer': + title = detail.question.title; + description = detail.content; + url = `https://www.zhihu.com/question/${detail.question.id}/answer/${detail.id}`; + break; + case 'article': + title = detail.title; + description = detail.content; + url = `https://zhuanlan.zhihu.com/p/${detail.id}`; + break; + case 'pin': + title = detail.excerptTitle.length > 17 ? detail.excerptTitle.slice(0, 17) + '...' : detail.excerptTitle; + description = detail.excerptTitle; + url = `https://www.zhihu.com/pin/${detail.id}`; + break; + case 'question': + title = detail.title; + description = detail.excerpt; + url = `https://www.zhihu.com/question/${detail.id}`; + break; + } + + return { + title: `${item.actionText}: ${title}`, + description: description, + pubDate: new Date(item.createdTime * 1000).toUTCString(), + link: url + }; + }), + }); +}; \ No newline at end of file