From 51ef0df0c2358e56ded6633ee5350f0a366a5f87 Mon Sep 17 00:00:00 2001 From: Harvie <327917387@qq.com> Date: Thu, 7 May 2026 11:20:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=9B=BD=E9=99=85?= =?UTF-8?q?=E8=83=BD=E6=BA=90=E7=BD=91=20(in-en.com)=20news=20route=20(#21?= =?UTF-8?q?669)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): add 国际能源网 (in-en.com) news route Add RSS route for in-en.com (国际能源网), a major Chinese energy news portal. Supported channels via /in-en/news/:type: - solar — 光伏太阳能 - wind — 风电 - chuneng — 储能 - h2 — 氢能 - chd — 充换电 - newenergy — 新能源综合 - power — 电力 - huanbao — 环保 Features: fetches article list from each subdomain's news page, then retrieves full article body via detail page (cached). Handles Chinese relative timestamps (X分钟/小时/天前). * fix(in-en): add maintainer, fix timezone double-shift, translate comments to English * fix(in-en): correct news paths for h2/chd/huanbao, drop date-only pubDate override * fix(in-en): sort imports, fix numeric separators, add category field, use trueUA * fix(in-en): address review comments - Remove config.trueUA, use default UA - Remove custom parseRelTime, use parseDate directly - Fix title text extraction without cloning element - Remove unnecessary .first() on pubDate - Fix author: handle both em.ly and plain-text source via .prompt > span - Simplify category field (remove redundant length check) - Fix description selector: .article-body → #article - Fix detail author selector: .article-meta .source a → p.source a - Remove try-catch around detail fetch * fix(in-en): fix author including category text and handle relative pubDate formats * fix(in-en): use parseRelativeDate, simplify selectors, drop redundant fallbacks - Replace hand-rolled relative-date logic with parseRelativeDate (covers "刚刚 / N分钟前 / N小时前 / 今天 HH:MM / 昨天 HH:MM / YYYY-MM-DD"). Fixes Invalid Date on the first few entries and resolves the two oxlint findings (no-useless-undefined, numeric-separators-style). - Drop the `.first()` on `.listTxt .prompt i` — each `ul.infoList > li` carries exactly one `` (date), confirmed against https://solar.in-en.com/news/. - Author selector: `.prompt > span:first-of-type`, then strip leading "来源:". Cheerio's `.text()` flattens `…` into the same string, so this matches both shapes shown on https://solar.in-en.com/news/ (plain text vs. wrapped in em.ly). - Category selector: `.prompt > span:not(:first-of-type) em a`, scoped to the second span so author text never leaks in. - Drop `|| undefined` on author and `?? ''` on description (redundant — DataItem already accepts undefined). - Add an HTML-shape comment pointing to the live sample URL so future reviewers can verify the layout assumptions. * fix(in-en): drop dead default + use new-media category - Hono treats `:type` as compulsory (path is `/news/:type`), so users without it never reach the handler — the `?? 'solar'` fallback was unreachable. Replace with a non-null assertion to keep the type narrow. - categories: 'traditional-media' → 'new-media' per review suggestion. --- lib/routes/in-en/index.ts | 116 ++++++++++++++++++++++++++++++++++ lib/routes/in-en/namespace.ts | 7 ++ 2 files changed, 123 insertions(+) create mode 100644 lib/routes/in-en/index.ts create mode 100644 lib/routes/in-en/namespace.ts diff --git a/lib/routes/in-en/index.ts b/lib/routes/in-en/index.ts new file mode 100644 index 000000000..0df99ee6d --- /dev/null +++ b/lib/routes/in-en/index.ts @@ -0,0 +1,116 @@ +import { load } from 'cheerio'; + +import type { DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseRelativeDate } from '@/utils/parse-date'; + +// Subdomain config: name = channel display name, newsPath = news list path +const CATEGORIES: Record = { + solar: { name: '光伏太阳能', newsPath: '/news/' }, + wind: { name: '风电', newsPath: '/windnews/' }, + chuneng: { name: '储能', newsPath: '/news/' }, + h2: { name: '氢能', newsPath: '/hydrogen/' }, + chd: { name: '充换电', newsPath: '/ChargingStation/' }, + newenergy: { name: '新能源', newsPath: '/news/' }, + power: { name: '电力', newsPath: '/news/' }, + huanbao: { name: '环保', newsPath: '/policy/' }, +}; + +export const route: Route = { + path: '/news/:type', + categories: ['new-media'], + example: '/in-en/news/solar', + parameters: { + type: 'Channel type, see table below', + }, + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + name: '新闻', + maintainers: ['Harviewang'], + description: `| 频道 | type 参数 | +| --- | --- | +| 光伏太阳能 | solar | +| 风电 | wind | +| 储能 | chuneng | +| 氢能 | h2 | +| 充换电 | chd | +| 新能源综合 | newenergy | +| 电力 | power | +| 环保 | huanbao |`, + + async handler(ctx) { + const type = ctx.req.param('type')!; + const cat = CATEGORIES[type]; + if (!cat) { + throw new Error(`Unknown channel type: ${type}. Valid values: ${Object.keys(CATEGORIES).join(', ')}`); + } + + const baseUrl = `https://${type}.in-en.com`; + const listUrl = `${baseUrl}${cat.newsPath}`; + + const html = await ofetch(listUrl); + const $ = load(html); + + // Each `ul.infoList > li` carries a single `.prompt` block with this fixed shape: + // {relative or absolute date} + // 来源:{author or {author}} + // {{category}}+ + // See https://solar.in-en.com/news/ for live samples. + const list: DataItem[] = $('ul.infoList > li') + .toArray() + .map((el) => { + const $el = $(el); + const $a = $el.find('.listTxt h5 a'); + const link = $a.attr('href') ?? ''; + const title = $a.attr('title')?.trim() || $a.text().trim(); + + const pubDateRaw = $el.find('.listTxt .prompt > i').text().trim(); + const author = $el.find('.listTxt .prompt > span').first().text().replace('来源:', '').trim(); + const category = $el + .find('.listTxt .prompt > span:not(:first-of-type) em a') + .toArray() + .map((a) => $(a).text().trim()) + .filter(Boolean); + + return { + title, + link, + author, + category, + pubDate: pubDateRaw ? parseRelativeDate(pubDateRaw) : undefined, + } as DataItem; + }) + .filter((item) => Boolean(item.title && item.link)); + + const items = await Promise.all( + list.map((item) => + cache.tryGet(item.link!, async () => { + const detail = await ofetch(item.link!); + const $d = load(detail); + + item.description = $d('#article').html() ?? undefined; + + const detailAuthor = $d('p.source a').text().trim(); + if (detailAuthor) { + item.author = detailAuthor; + } + + return item; + }) + ) + ); + + return { + title: `国际能源网 · ${cat.name}`, + link: listUrl, + item: items as DataItem[], + }; + }, +}; diff --git a/lib/routes/in-en/namespace.ts b/lib/routes/in-en/namespace.ts new file mode 100644 index 000000000..58e144c67 --- /dev/null +++ b/lib/routes/in-en/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '国际能源网', + url: 'www.in-en.com', + lang: 'zh-CN', +};