From 05ea0aa3aed2203fe7148aa22a2d158fd5331847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=99=8E=E6=95=85=E6=B4=9E?= <55782476+TigerCubDen@users.noreply.github.com> Date: Tue, 2 Mar 2021 12:53:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E6=94=AF=E6=8C=81=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E7=9A=84=E7=9F=A5=E4=B9=8E=E4=B8=93=E6=A0=8Furl?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=20=E5=92=8C=20Bug=20Fix=20(#7011)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/radar-rules.js | 6 ++++++ lib/routes/zhihu/zhuanlan.js | 34 +++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) 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, }; };