From 6a48dd9b4fc8ed3d14048825eb8e9ff347cdf588 Mon Sep 17 00:00:00 2001 From: cnk Date: Sat, 25 Jan 2025 01:58:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(routes/ce/district):=20add=20route=20'?= =?UTF-8?q?=E4=B8=AD=E5=9B=BD=E7=BB=8F=E6=B5=8E=E7=BD=91=E5=9C=B0=E6=96=B9?= =?UTF-8?q?=E7=BB=8F=E6=B5=8E'=20(#18192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/ce/district.ts | 91 ++++++++++++++++++++++++++++++++++++++ lib/routes/ce/namespace.ts | 8 ++++ 2 files changed, 99 insertions(+) create mode 100644 lib/routes/ce/district.ts create mode 100644 lib/routes/ce/namespace.ts diff --git a/lib/routes/ce/district.ts b/lib/routes/ce/district.ts new file mode 100644 index 000000000..1f2b8180e --- /dev/null +++ b/lib/routes/ce/district.ts @@ -0,0 +1,91 @@ +import { Route, ViewType } from '@/types'; +import cache from '@/utils/cache'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; +import { load } from 'cheerio'; +import { ofetch } from 'ofetch'; + +export const route: Route = { + path: '/district/:category?', + name: '地方经济', + url: 'district.ce.cn', + maintainers: ['cscnk52'], + handler, + example: '/ce/district', + parameters: { category: '栏目标识,默认为 roll(即时新闻)' }, + description: `| 即时新闻 | 经济动态 | 独家视角 | 专题 | 数说地方 | 地方播报 | 专稿 | 港澳台 | +|----------|----------|----------|------|----------|----------|------|--------| +| roll | jjdt | poll | ch | ssdf | dfbb | zg | gat |`, + categories: ['traditional-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + supportRadar: true, + }, + radar: [ + { + source: ['district.ce.cn/newarea/:category/index.shtml'], + target: '/district/:category?', + }, + { + source: ['district.ce.cn/newarea/:category'], + target: '/district/:category?', + }, + { + source: ['district.ce.cn'], + target: '/district', + }, + ], + view: ViewType.Articles, +}; + +async function handler(ctx) { + const rootUrl = 'http://district.ce.cn/'; + const { category = 'roll' } = ctx.req.param(); + const url = `${rootUrl}newarea/${category}/index.shtml`; + const GB2312Response = await ofetch(url, { responseType: 'arrayBuffer' }); + + // originally site use gb2312 encoding + const response = new TextDecoder('gb2312').decode(new Uint8Array(GB2312Response)); + const $ = load(response); + + const bigTitle = $('div.channl a').eq(1).attr('title'); + + const list = $('div.sec_left li') + .toArray() + .map((e) => { + const element = $(e); + const title = element.find('a').text().trim(); + const link = new URL(element.find('a').attr('href'), url).href; + return { + title, + link, + }; + }); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link, async () => { + const GB2312Response = await ofetch(item.link, { responseType: 'arrayBuffer' }); + const response = new TextDecoder('gb2312').decode(new Uint8Array(GB2312Response)); + const $ = load(response); + + const pubDateText = $('span#articleTime').text().trim(); + item.pubDate = timezone(parseDate(pubDateText, 'YYYY年MM月DD日 HH:mm'), +8); + + item.description = $('div.TRS_Editor').html(); + return item; + }) + ) + ); + + return { + title: `中国经济网地方经济 - ${bigTitle}`, + link: url, + item: items, + }; +} diff --git a/lib/routes/ce/namespace.ts b/lib/routes/ce/namespace.ts new file mode 100644 index 000000000..94dfbe1af --- /dev/null +++ b/lib/routes/ce/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '中国经济网', + url: 'www.ce.cn', + categories: ['traditional-media'], + lang: 'zh-CN', +};