feat(route): add 国际能源网 (in-en.com) news route (#21669)
* 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 `<i>` (date), confirmed against https://solar.in-en.com/news/. - Author selector: `.prompt > span:first-of-type`, then strip leading "来源:". Cheerio's `.text()` flattens `<em class="ly">…</em>` 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.
This commit is contained in:
@@ -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<string, { name: string; newsPath: string }> = {
|
||||
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:
|
||||
// <i>{relative or absolute date}</i>
|
||||
// <span>来源:{author or <em class="ly">{author}</em>}</span>
|
||||
// <span><em>{<a>{category}</a>}+</em></span>
|
||||
// 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[],
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: '国际能源网',
|
||||
url: 'www.in-en.com',
|
||||
lang: 'zh-CN',
|
||||
};
|
||||
Reference in New Issue
Block a user