feat(route): add cffex/announcement (#16258)

* feat(route): add cffex/announcement

* style(route/cffex): modify code style
This commit is contained in:
Chen Xiangcheng 2024-07-26 18:42:46 +08:00 committed by GitHub
parent 627a848ba0
commit 1728ea5b54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,73 @@
import { DataItem, Route } from '@/types';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import cache from '@/utils/cache';
export const route: Route = {
path: '/announcement',
name: '交易所公告',
url: 'www.cffex.com.cn',
maintainers: ['ChenXiangcheng1'],
example: '/cffex/announcement',
parameters: {},
description: '',
categories: ['government'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['cffex.com.cn'],
target: '/announcement',
},
],
handler,
};
async function handler(): Promise<{ title: string; link: string; item: DataItem[] }> {
const baseUrl = 'http://www.cffex.com.cn';
const homeUrl = `${baseUrl}/jystz`;
const response = await ofetch(homeUrl);
// 使用 Cheerio 选择器解析 HTML
const $ = load(response);
const list = $('div.notice_list li')
.toArray()
.map((item) => {
item = $(item); // (Element) -> LoadedCheerio
const titleEle = $(item).find('a').first();
const dateEle = $(item).find('a').eq(1);
return {
title: titleEle.text().trim(),
link: `${baseUrl}${titleEle.attr('href')}`,
pubDate: timezone(parseDate(dateEle.text(), 'YYYY-MM-DD'), +8),
};
});
// (Promise) -> Promise
const items = await Promise.all(
// (Promise|null) -> Promise|null
list.map((item) =>
cache.tryGet(item.link, async () => {
const response = await ofetch(item.link);
const $ = load(response);
item.description = $('div.jysggnr div.nan p').eq(1)?.html();
return item;
})
)
);
return {
title: '中国金融期货交易所 - 交易所公告',
link: homeUrl,
item: items,
};
}

View File

@ -0,0 +1,6 @@
import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '中国金融期货交易所',
url: 'cffex.com.cn',
};