fix(route): 支持更多的知乎专栏url格式 和 Bug Fix (#7011)

This commit is contained in:
小虎故洞
2021-03-01 23:53:23 -05:00
committed by GitHub
parent e9f07055a7
commit 05ea0aa3ae
2 changed files with 35 additions and 5 deletions
+6
View File
@@ -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: [
{
+29 -5
View File
@@ -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} <br> <br> <a href="${link}">视频内容请跳转至原页面观看</a>`;
}
else {
description = `<a href="${link}">视频内容请跳转至原页面观看</a>`;
}
}
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,
};
};