/
index.ts
83 lines (75 loc) · 2.34 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
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
const rootUrl = 'https://cpc.ey.gov.tw';
const typeMap = {
xwg: {
name: '新闻稿',
url: '/Page/A3412E2A5A7B398F',
},
xfzx: {
name: '消费资讯',
url: '/Page/E414CC218269CCE8',
},
};
export const route: Route = {
path: '/:type?',
categories: ['government'],
example: '/cpcey/xwg',
parameters: { type: '默认为 `xwg`' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '消费资讯',
maintainers: ['Fatpandac'],
handler,
description: `| 新闻稿 | 消费资讯 |
| :----: | :------: |
| xwg | xfzx |`,
};
async function handler(ctx) {
const type = ctx.req.param('type') ?? 'xwg';
const url = rootUrl + typeMap[type].url;
const response = await got.get(url);
const $ = load(response.data);
const list = $('div.words > ul > li')
.map((_, item) => {
const date = $(item).find('span').text();
const dateArr = date.split('-');
const dateStr = Number.parseInt(dateArr[0]) + 1911 + '/' + dateArr[1] + '/' + dateArr[2];
return {
link: rootUrl + $(item).find('a').attr('href'),
title: $(item).find('a').attr('title'),
pubDate: parseDate(dateStr, 'YYYY/MM/DD'),
};
})
.get();
const items = await Promise.all(
list.map(async (item) => {
let desc = '';
const isOtherWebPage = !item.link.includes('.html');
if (isOtherWebPage) {
desc = await cache.tryGet(item.link, async () => {
const response = await got.get(item.link);
const $ = load(response.data);
const desc = $('div.words > div.graybg.ail > div').html();
return desc;
});
}
item.description = desc;
return item;
})
);
return {
title: `行政院消费者保护会-${typeMap[type].name}`,
link: url,
item: items,
};
}