diff --git a/assets/radar-rules.js b/assets/radar-rules.js index f760a9cf3e..24768a7ace 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -387,6 +387,12 @@ source: '/pub/weekly', target: '/zhihu/weekly', }, + { + title: '专栏', + docs: 'https://docs.rsshub.app/social-media.html#zhi-hu', + source: '/column/:id', + target: '/zhihu/zhuanlan/:id', + }, ], zhuanlan: [ { diff --git a/lib/routes/zhihu/zhuanlan.js b/lib/routes/zhihu/zhuanlan.js index e5b7640d9c..cf7e009771 100644 --- a/lib/routes/zhihu/zhuanlan.js +++ b/lib/routes/zhihu/zhuanlan.js @@ -3,7 +3,7 @@ const utils = require('./utils'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const id = ctx.params.id; + const id = String(ctx.params.id); const listRes = await got({ method: 'get', @@ -14,13 +14,24 @@ module.exports = async (ctx) => { }, }); - const infoRes = await got.get(`https://zhuanlan.zhihu.com/${id}`); + // 知乎专栏链接存在两种格式, 一种以 'zhuanlan.' 开头, 另一种新增的以 'c_' 结尾 + let url = `https://zhuanlan.zhihu.com/${id}`; + if (id.search("c_") === 0) { + url = `https://www.zhihu.com/column/${id}`; + } + + const infoRes = await got.get(url); const $ = cheerio.load(infoRes.data); const title = $('.css-zyehvu').text(); const description = $('.css-1bnklpv').text(); const item = listRes.data.data.map((item) => { - const $ = cheerio.load(item.content); + // 当专栏内文章内容不含任何文字时, 返回空字符, 以免直接报错 + let description = ''; + if (item.content) { + const $ = cheerio.load(item.content); + description = $.html(); + } $('img').css('max-width', '100%'); let title = ''; @@ -38,11 +49,24 @@ module.exports = async (ctx) => { link = item.url; author = item.author.name; pubDate = new Date(item.created * 1000); + } else if (item.type === 'zvideo') { + // 官方 api 没有提供视频类的源 url, 故返回专栏页面 url + title = item.title; + link = url; + author = item.author.name; + pubDate = new Date(item.created_at * 1000); + // 判断是否存在视频简介 + if (item.description) { + description = `${item.description}

视频内容请跳转至原页面观看`; + } + else { + description = `视频内容请跳转至原页面观看`; + } } return { title, link, - description: $.html(), + description, pubDate, author, }; @@ -52,6 +76,6 @@ module.exports = async (ctx) => { description, item, title: `知乎专栏-${title}`, - link: `https://zhuanlan.zhihu.com/${id}`, + link: url, }; };