/
index.ts
98 lines (82 loc) · 2.85 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
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';
export const route: Route = {
path: '/:category?',
categories: ['new-media'],
example: '/fortunechina',
parameters: { category: '分类,见下表,默认为首页' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['fortunechina.com/:category', 'fortunechina.com/'],
},
],
name: '分类',
maintainers: ['nczitzk'],
handler,
description: `| 商业 | 领导力 | 科技 | 研究 |
| ------- | --------- | ---- | ------ |
| shangye | lindgaoli | keji | report |`,
};
async function handler(ctx) {
const category = ctx.req.param('category') ?? '';
const rootUrl = 'https://www.fortunechina.com';
const currentUrl = `${rootUrl}${category ? `/${category}` : ''}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response.data);
let items = $('.main')
.find('h3 a')
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit')) : 15)
.toArray()
.map((item) => {
item = $(item);
const link = item.attr('href');
return {
title: item.text(),
link: link.indexOf('http') === 0 ? link : `${currentUrl}/${item.attr('href')}`,
};
});
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 spans = content('.mod-info span').text();
let matches = spans.match(/(\d{4}-\d{2}-\d{2})/);
if (matches) {
item.pubDate = parseDate(matches[1]);
} else {
matches = spans.match(/(\d+小时前)/);
if (matches) {
item.pubDate = parseRelativeDate(matches[1]);
}
}
item.author = content('.name').text();
content('.mod-info, .title, .eval-zan, .eval-pic, .sae-more, .ugo-kol, .word-text .word-box .word-cn').remove();
item.description = content('#articleContent, .eval-desc').html();
return item;
})
)
);
return {
title: category ? $('title').text() : '财富中文网',
link: currentUrl,
item: items,
};
}