/
zhuanlan.ts
124 lines (107 loc) · 3.66 KB
/
zhuanlan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Route } from '@/types';
import got from '@/utils/got';
import { header } from './utils';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/zhuanlan/:id',
categories: ['social-media'],
example: '/zhihu/zhuanlan/googledevelopers',
parameters: { id: '专栏 id,可在专栏主页 URL 中找到' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['zhuanlan.zhihu.com/:id'],
},
],
name: '专栏',
maintainers: ['DIYgod'],
handler,
};
async function handler(ctx) {
const id = ctx.req.param('id');
const listRes = await got({
method: 'get',
url: `https://www.zhihu.com/api/v4/columns/${id}/items`,
headers: {
...header,
Referer: `https://zhuanlan.zhihu.com/${id}`,
},
});
const pinnedRes = await got({
method: 'get',
url: `https://www.zhihu.com/api/v4/columns/${id}/pinned-items`,
headers: {
...header,
Referer: `https://zhuanlan.zhihu.com/${id}`,
},
});
listRes.data.data = [...listRes.data.data, ...pinnedRes.data.data];
// 知乎专栏链接存在两种格式, 一种以 '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(url);
const $ = load(infoRes.data);
const title = $('.css-zyehvu').text();
const description = $('.css-1bnklpv').text();
const item = listRes.data.data.map((item) => {
// 当专栏内文章内容不含任何文字时, 返回空字符, 以免直接报错
let description = '';
if (item.content) {
const $ = load(item.content);
description = $.html();
}
$('img').css('max-width', '100%');
let title = '';
let link = '';
let author = '';
let pubDate: Date;
switch (item.type) {
case 'answer':
title = item.question.title;
author = item.question.author ? item.question.author.name : '';
link = `https://www.zhihu.com/question/${item.question.id}/answer/${item.id}`;
pubDate = parseDate(item.created_time * 1000);
break;
case 'article':
title = item.title;
link = item.url;
author = item.author.name;
pubDate = parseDate(item.created * 1000);
break;
case 'zvideo':
// 如果类型是zvideo,id即为视频地址参数
title = item.title;
link = `https://www.zhihu.com/zvideo/${item.id}`;
author = item.author.name;
pubDate = parseDate(item.created_at * 1000);
// 判断是否存在视频简介
description = item.description ? `${item.description} <br> <br> <a href="${link}">视频内容请跳转至原页面观看</a>` : `<a href="${link}">视频内容请跳转至原页面观看</a>`;
break;
default:
throw new Error(`Unknown type: ${item.type}`);
}
return {
title,
link,
description,
pubDate,
author,
};
});
return {
description,
item,
title: `知乎专栏-${title}`,
link: url,
};
}