/
index.ts
157 lines (143 loc) · 4.24 KB
/
index.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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';
const ids = {
recommend: {
url: 'content/recommend',
title: '推荐',
},
hkstock: {
url: 'content/hkstock',
title: '港股',
},
meigu: {
url: 'content/meigu',
title: '美股',
},
agu: {
url: 'content/agu',
title: '沪深',
},
ct: {
url: 'content/ct',
title: '创投',
},
esg: {
url: 'content/esg',
title: 'ESG',
},
aqs: {
url: 'content/aqs',
title: '券商',
},
ajj: {
url: 'content/ajj',
title: '基金',
},
focus: {
url: 'focus',
title: '要闻',
},
announcement: {
url: 'announcement',
title: '公告',
},
research: {
url: 'research',
title: '研究',
},
shares: {
url: 'shares',
title: '新股',
},
bazaar: {
url: 'bazaar',
title: '市场',
},
company: {
url: 'company',
title: '公司',
},
};
export const route: Route = {
path: '/:id?/:category?',
categories: ['finance'],
example: '/zhitongcaijing',
parameters: { id: '栏目 id,可在对应栏目页 URL 中找到,默认为 recommend,即推荐', category: '分类 id,可在对应栏目子分类页 URL 中找到,默认为全部' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '推荐',
maintainers: ['nczitzk'],
handler,
description: `| id | 栏目 |
| ------------ | ---- |
| recommend | 推荐 |
| hkstock | 港股 |
| meigu | 美股 |
| agu | 沪深 |
| ct | 创投 |
| esg | ESG |
| aqs | 券商 |
| ajj | 基金 |
| focus | 要闻 |
| announcement | 公告 |
| research | 研究 |
| shares | 新股 |
| bazaar | 市场 |
| company | 公司 |`,
};
async function handler(ctx) {
const id = ctx.req.param('id') ?? 'recommend';
const category = ctx.req.param('category') ?? '';
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 20;
const rootUrl = 'https://www.zhitongcaijing.com';
const currentUrl = `${rootUrl}/${ids[id].url}.html${category === '' ? '' : `?category_key=${category}`}`;
const apiUrl = `${rootUrl}/${ids[id].url}.html?data_type=1&page=1${category === '' ? '' : `&category_key=${category}`}`;
const response = await got({
method: 'get',
url: apiUrl,
});
let items = response.data.data.slice(0, limit).map((item) => ({
title: item.title,
link: `${rootUrl}${item.url}`,
description: item.digest,
author: item.author_info.author_name,
pubDate: parseDate((item.create_time ?? Number.parseInt(item.original_time)) * 1000),
category: [...(item.keywords?.split(',') ?? []), item.category_name ?? item.type_tag],
}));
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);
content('#subscribe-vip-box').remove();
content('#news-content').remove();
item.description = art(path.join(__dirname, 'templates/description.art'), {
digest: content('.digetst-box').html() || content('.telegram-origin-contentn').html(),
description: content('.news-body-content').html(),
});
return item;
})
)
);
return {
title: `智通财经 - ${ids[id].title}`,
link: currentUrl,
item: items,
};
}