/
index.ts
110 lines (92 loc) 路 3.16 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
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 InvalidParameterError from '@/errors/types/invalid-parameter';
const categories = {
jxrb: '鍢夊叴鏃ユ姤',
nhwb: '鍗楁箹鏅氭姤',
};
export const route: Route = {
path: '/:category?/:id?',
name: 'Unknown',
maintainers: [],
handler,
};
async function handler(ctx) {
const category = ctx.req.param('category') ?? 'jxrb';
const id = ctx.req.param('id');
if (!Object.keys(categories).includes(category)) {
throw new InvalidParameterError('Invalid category');
}
const rootUrl = `https://${category}.cnjxol.com`;
const currentUrl = `${rootUrl}/${category}Paper/pc/layout`;
const response = await got({
method: 'get',
url: currentUrl,
});
let $ = load(response.data);
const dateMatch = $('a')
.first()
.attr('href')
.match(/\d{6}\/\d{2}/)[0];
let items = [];
if (id) {
const pageUrl = `${currentUrl}/${dateMatch}/node_${id}.html`;
const pageResponse = await got({
method: 'get',
url: pageUrl,
});
$ = load(pageResponse.data);
items = $('#articlelist .clearfix a')
.toArray()
.map((a) => `${currentUrl}/${$(a).attr('href')}`.replaceAll('layout/../../../', ''));
} else {
await Promise.all(
$('#list li a')
.toArray()
.map(async (p) => {
const pageResponse = await got({
method: 'get',
url: `${currentUrl}/${$(p).attr('href')}`,
});
const page = load(pageResponse.data);
items.push(
...page('#articlelist .clearfix a')
.toArray()
.map((a) => `${currentUrl}/${page(a).attr('href')}`.replaceAll('layout/../../../', ''))
);
})
);
}
items = await Promise.all(
items.map((item) =>
cache.tryGet(item, async () => {
const detailResponse = await got({
method: 'get',
url: item,
});
const content = load(detailResponse.data);
return {
link: item,
title: content('#Title').text(),
pubDate: parseDate(content('date').text()),
description: art(path.join(__dirname, 'templates/description.art'), {
attachment: content('.attachment').html(),
content: content('founder-content').html(),
}),
};
})
)
);
return {
title: `${categories[category]}${id ? ` - ${$('#layout').text()}` : ''}`,
link: currentUrl,
item: items,
};
}