/
hot.ts
83 lines (70 loc) · 2.26 KB
/
hot.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
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import { rootUrl, getSearchParams } from './utils';
export const route: Route = {
path: '/hot',
categories: ['finance'],
example: '/cls/hot',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['cls.cn/'],
},
],
name: '热门文章排行榜',
maintainers: ['5upernova-heng', 'nczitzk'],
handler,
url: 'cls.cn/',
};
async function handler(ctx) {
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 50;
const apiUrl = `${rootUrl}/v2/article/hot/list`;
const response = await got({
method: 'get',
url: apiUrl,
searchParams: getSearchParams(),
});
let items = response.data.data.slice(0, limit).map((item) => ({
title: item.title || item.brief,
link: `${rootUrl}/detail/${item.id}`,
pubDate: parseDate(item.ctime * 1000),
}));
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
const nextData = JSON.parse(content('script#__NEXT_DATA__').text());
const articleDetail = nextData.props.initialState.detail.articleDetail;
item.author = articleDetail.author?.name ?? item.author ?? '';
item.description = art(path.join(__dirname, 'templates/depth.art'), {
articleDetail,
});
return item;
})
)
);
return {
title: '财联社 - 热门文章排行榜',
link: rootUrl,
item: items,
};
}