/
zxfb.ts
64 lines (53 loc) · 1.79 KB
/
zxfb.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
import got from '@/utils/got';
import * as cheerio from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import cache from '@/utils/cache';
import { Route } from '@/types';
const handler = async (ctx) => {
const { category = 'zxfb' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 15;
const rootUrl = 'https://www.12371.cn/';
const currentUrl = `${rootUrl}${category}/`;
const response = await got(currentUrl);
const $ = cheerio.load(response.data);
const pattern = /item=(\[{.*?}]);/;
const newsList = JSON.parse($('script[language="javascript"]').text().match(pattern)?.[1].replaceAll("'", '"') || '[]');
const topNewsList = newsList.slice(0, limit).map((item) => ({
title: item.title,
pubDate: timezone(parseDate(item.brief, 'YYYY-MM-DD HH:mm:ss'), 8),
link: item.link_add,
}));
const items = await Promise.all(
topNewsList.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got(item.link);
const $ = cheerio.load(detailResponse.data);
item.description = $('.word').html();
return item;
})
)
);
return {
title: $('title').text(),
link: currentUrl,
item: items,
};
};
export const route: Route = {
path: '/:category?',
example: '/12371/zxfb',
parameters: { category: '新闻分类名,预设 `zxfb`' },
radar: [
{
source: ['www.12371.cn/:category'],
},
],
name: '最新发布',
maintainers: ['zvrr'],
handler,
url: 'www.12371.cn',
description: `| 最新发布 |
| :------: |
| zxfb |`,
};