feat(route): add 证券时报网 (#18551)

* feat(route): add 证券时报网

* fix(route): update path format for article list route

---------
This commit is contained in:
Ethan Shen
2025-03-10 02:39:03 +08:00
committed by GitHub
parent 8617eba2a3
commit 65754737c6
2 changed files with 389 additions and 140 deletions
+245 -140
View File
@@ -1,159 +1,264 @@
import { Route, ViewType } from '@/types';
import { type Data, type DataItem, type Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import timezone from '@/utils/timezone';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { type CheerioAPI, type Cheerio, type Element, load } from 'cheerio';
import { type Context } from 'hono';
export const handler = async (ctx: Context): Promise<Data> => {
const { id = 'yw' } = ctx.req.param();
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10);
const baseUrl: string = 'https://www.stcn.com';
const targetUrl: string = new URL(`article/list/${id}.html`, baseUrl).href;
const response = await ofetch(targetUrl);
const $: CheerioAPI = load(response);
const language = $('html').attr('lang') ?? 'zh-CN';
let items: DataItem[] = [];
items = $('ul.infinite-list li')
.slice(0, limit)
.toArray()
.map((el): Element => {
const $el: Cheerio<Element> = $(el);
const $aEl: Cheerio<Element> = $el.find('div.tt a');
const title: string = $aEl.text();
const description: string = $el.find('div.text').html();
const pubDateStr: string | undefined = $el.find('div.info span').last().text().trim();
const linkUrl: string | undefined = $aEl.attr('href');
const categoryEls: Element[] = $el.find('div.tags span').toArray();
const categories: string[] = [...new Set(categoryEls.map((el) => $(el).text()).filter(Boolean))];
const authors: DataItem['author'] = $el.find('div.info span').first().text();
const image: string | undefined = $el.find('div.side a img').attr('src');
const upDatedStr: string | undefined = pubDateStr;
const processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? timezone(parseDate(pubDateStr, ['HH:mm', 'MM-DD HH:mm', 'YYYY-MM-DD HH:mm']), +8) : undefined,
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
image,
banner: image,
updated: upDatedStr ? timezone(parseDate(upDatedStr, ['HH:mm', 'MM-DD HH:mm', 'YYYY-MM-DD HH:mm']), +8) : undefined,
language,
};
return processedItem;
});
items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}
return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link);
const $$: CheerioAPI = load(detailResponse);
const title: string = $$('div.detail-title').text();
const description: string = $$('div.detail-content').html() ?? '';
const pubDateStr: string | undefined = $$('div.detail-info span').last().text().trim();
const categories: string[] = $$('meta[name="keywords"]').attr('content')?.split(/,/) ?? [];
const authors: DataItem['author'] = $$('div.detail-info span').first().text().split(/:/).pop();
const upDatedStr: string | undefined = pubDateStr;
const processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? timezone(parseDate(pubDateStr), +8) : item.pubDate,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
updated: upDatedStr ? timezone(parseDate(upDatedStr), +8) : item.updated,
language,
};
return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);
return {
title: $('title').text(),
description: $('meta[name="description"]').attr('content'),
link: targetUrl,
item: items,
allowEmpty: true,
image: $('img.stcn-logo').attr('src'),
author: $('meta[name="keywords"]').attr('content')?.split(/,/)[0],
language,
id: targetUrl,
};
};
export const route: Route = {
path: '/:id?',
categories: ['finance', 'popular'],
view: ViewType.Articles,
example: '/stcn/yw',
path: '/article/list/:id?',
name: '列表',
url: 'www.stcn.com',
maintainers: ['nczitzk'],
handler,
example: '/stcn/article/list/yw',
parameters: {
id: {
description: '栏目 id',
category: {
description: '分类,默认为 `yw`,即要闻,可在对应分类页 URL 中找到',
options: [
{ value: 'kx', label: '快讯' },
{ value: 'yw', label: '要闻' },
{ value: 'gs', label: '股市' },
{ value: 'company', label: '公司' },
{ value: 'data', label: '数据' },
{ value: 'fund', label: '基金' },
{ value: 'finance', label: '金融' },
{ value: 'comment', label: '评论' },
{ value: 'cj', label: '产经' },
{ value: 'ct', label: '创投' },
{ value: 'kcb', label: '科创板' },
{ value: 'xsb', label: '新三板' },
{ value: 'tj', label: '投教' },
{ value: 'zk', label: 'ESG' },
{ value: 'gd', label: '滚动' },
{ value: 'gsyl', label: '股市一览' },
{ value: 'djjd', label: '独家解读' },
{ value: 'gsxw', label: '公司新闻' },
{ value: 'gsdt', label: '公司动态' },
{ value: 'djsj', label: '独家数据' },
{ value: 'kd', label: '看点数据' },
{ value: 'zj', label: '资金流向' },
{ value: 'sj_kcb', label: '科创板' },
{ value: 'hq', label: '行情总貌' },
{ value: 'zl', label: '专栏' },
{ value: 'author', label: '作者' },
{ value: 'cjhy', label: '行业' },
{ value: 'cjqc', label: '汽车' },
{ value: 'tjkt', label: '投教课堂' },
{ value: 'zczs', label: '政策知识' },
{ value: 'tjdt', label: '投教动态' },
{ value: 'zthd', label: '专题活动' },
{
label: '要闻',
value: 'yw',
},
{
label: '股市',
value: 'gs',
},
{
label: '公司',
value: 'company',
},
{
label: '基金',
value: 'fund',
},
{
label: '金融',
value: 'finance',
},
{
label: '评论',
value: 'comment',
},
{
label: '产经',
value: 'cj',
},
{
label: '科创板',
value: 'kcb',
},
{
label: '新三板',
value: 'xsb',
},
{
label: 'ESG',
value: 'zk',
},
{
label: '滚动',
value: 'gd',
},
],
default: 'yw',
},
},
description: `:::tip
若订阅 [要闻](https://www.stcn.com/article/list/yw.html),网址为 \`https://www.stcn.com/article/list/yw.html\`,请截取 \`https://www.stcn.com/article/list/\` 到末尾 \`.html\` 的部分 \`yw\` 作为 \`id\` 参数填入,此时目标路由为 [\`/stcn/article/list/yw\`](https://rsshub.app/stcn/article/list/yw)。
:::
| 要闻 | 股市 | 公司 | 基金 | 金融 | 评论 |
| ---- | ---- | ------- | ---- | ------- | ------- |
| yw | gs | company | fund | finance | comment |
| 产经 | 科创板 | 新三板 | ESG | 滚动 |
| ---- | ------ | ------ | --- | ---- |
| cj | kcb | xsb | zk | gd |
`,
categories: ['finance'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: '栏目',
maintainers: ['nczitzk'],
handler,
description: `| 快讯 | 要闻 | 股市 | 公司 | 数据 |
| ---- | ---- | ---- | ------- | ---- |
| kx | yw | gs | company | data |
radar: [
{
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/:id'],
target: (params, url) => {
const urlObj: URL = new URL(url);
const id: string | undefined = urlObj.searchParams.get('type') ?? params.id;
| 基金 | 金融 | 评论 | 产经 | 创投 |
| ---- | ------- | ------- | ---- | ---- |
| fund | finance | comment | cj | ct |
| 科创板 | 新三板 | 投教 | ESG | 滚动 |
| ------ | ------ | ---- | --- | ---- |
| kcb | xsb | tj | zk | gd |
| 股市一览 | 独家解读 |
| -------- | -------- |
| gsyl | djjd |
| 公司新闻 | 公司动态 |
| -------- | -------- |
| gsxw | gsdt |
| 独家数据 | 看点数据 | 资金流向 | 科创板 | 行情总貌 |
| -------- | -------- | -------- | ------- | -------- |
| djsj | kd | zj | sj\_kcb | hq |
| 专栏 | 作者 |
| ---- | ------ |
| zl | author |
| 行业 | 汽车 |
| ---- | ---- |
| cjhy | cjqc |
| 投教课堂 | 政策知识 | 投教动态 | 专题活动 |
| -------- | -------- | -------- | -------- |
| tjkt | zczs | tjdt | zthd |`,
return `/stcn/article/list${id ? `/${id}` : ''}`;
},
},
{
title: '要闻',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/yw.html'],
target: '/article/list/yw',
},
{
title: '股市',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/gs.html'],
target: '/article/list/gs',
},
{
title: '公司',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/company.html'],
target: '/article/list/company',
},
{
title: '基金',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/fund.html'],
target: '/article/list/fund',
},
{
title: '金融',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/finance.html'],
target: '/article/list/finance',
},
{
title: '评论',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/comment.html'],
target: '/article/list/comment',
},
{
title: '产经',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/cj.html'],
target: '/article/list/cj',
},
{
title: '科创板',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/kcb.html'],
target: '/article/list/kcb',
},
{
title: '新三板',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/xsb.html'],
target: '/article/list/xsb',
},
{
title: 'ESG',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/zk.html'],
target: '/article/list/zk',
},
{
title: '滚动',
source: ['www.stcn.com/article/list.html', 'www.stcn.com/article/list/gd.html'],
target: '/article/list/gd',
},
],
view: ViewType.Articles,
};
async function handler(ctx) {
const id = ctx.req.param('id') ?? 'yw';
const rootUrl = 'https://www.stcn.com';
const currentUrl = `${rootUrl}/article/list/${id}.html`;
const apiUrl = `${rootUrl}/article/list.html?type=${id}`;
const response = await got({
method: 'get',
url: apiUrl,
});
const $ = load(response.data);
let items = $('.t, .tt, .title')
.find('a')
.toArray()
.map((item) => {
item = $(item);
const link = item.attr('href');
return {
title: item.text().replaceAll(/(^【|】$)/g, ''),
link: link.startsWith('http') ? link : `${rootUrl}${link}`,
};
});
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
if (/\.html$/.test(item.link)) {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = load(detailResponse.data);
item.title = content('.detail-title').text();
item.author = content('.detail-info span').first().text().split(':').pop();
item.pubDate = timezone(parseDate(content('.detail-info span').last().text()), +8);
item.category = content('.detail-content-tags div')
.toArray()
.map((t) => content(t).text());
item.description = content('.detail-content').html();
}
return item;
})
)
);
return {
title: `证券时报网 - ${$('.breadcrumb a').last().text()}`,
link: currentUrl,
item: items,
};
}
+144
View File
@@ -0,0 +1,144 @@
import { type Data, type DataItem, type Route, ViewType } from '@/types';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import { type CheerioAPI, load } from 'cheerio';
import { type Context } from 'hono';
export const handler = async (ctx: Context): Promise<Data> => {
const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10);
const baseUrl: string = 'https://www.stcn.com';
const targetUrl: string = new URL('article/list/kx.html', baseUrl).href;
const apiUrl: string = new URL('article/list.html', baseUrl).href;
const targetResponse = await ofetch(targetUrl);
const response = await ofetch(apiUrl, {
headers: {
'x-requested-with': 'XMLHttpRequest',
},
query: {
type: 'kx',
},
});
const $: CheerioAPI = load(targetResponse);
const language = $('html').attr('lang') ?? 'zh-CN';
let items: DataItem[] = [];
items = response.data.slice(0, limit).map((item): DataItem => {
const title: string = item.title;
const description: string = item.content;
const pubDate: number | string = item.time;
const linkUrl: string | undefined = item.url;
const categories: string[] = item.tags ? item.tags.map((c) => c.name) : [];
const authors: DataItem['author'] = item.source;
const image: string | undefined = item.share?.image;
const updated: number | string = pubDate;
const processedItem: DataItem = {
title,
description,
pubDate: pubDate ? parseDate(pubDate, 'X') : undefined,
link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined,
category: categories,
author: authors,
content: {
html: description,
text: item.content ?? description,
},
image,
banner: image,
updated: updated ? parseDate(updated, 'X') : undefined,
language,
};
return processedItem;
});
items = (
await Promise.all(
items.map((item) => {
if (!item.link) {
return item;
}
return cache.tryGet(item.link, async (): Promise<DataItem> => {
const detailResponse = await ofetch(item.link);
const $$: CheerioAPI = load(detailResponse);
const title: string = $$('div.detail-title').text();
const description: string = $$('div.detail-content').html() ?? '';
const pubDateStr: string | undefined = $$('div.detail-info span').last().text().trim();
const categories: string[] = [...new Set([...(item.category as string[]), ...($$('meta[name="keywords"]').attr('content')?.split(/,/) ?? [])])];
const authors: DataItem['author'] = $$('div.detail-info span').first().text().split(/:/).pop();
const upDatedStr: string | undefined = pubDateStr;
const processedItem: DataItem = {
title,
description,
pubDate: pubDateStr ? timezone(parseDate(pubDateStr), +8) : item.pubDate,
category: categories,
author: authors,
content: {
html: description,
text: description,
},
updated: upDatedStr ? timezone(parseDate(upDatedStr), +8) : item.updated,
language,
};
return {
...item,
...processedItem,
};
});
})
)
).filter((_): _ is DataItem => true);
return {
title: $('title').text(),
description: $('meta[name="description"]').attr('content'),
link: targetUrl,
item: items,
allowEmpty: true,
image: $('img.stcn-logo').attr('src'),
author: $('meta[name="keywords"]').attr('content')?.split(/,/)[0],
language,
id: targetUrl,
};
};
export const route: Route = {
path: '/article/list/kx',
name: '快讯',
url: 'www.stcn.com',
maintainers: ['nczitzk'],
handler,
example: '/stcn/article/list/kx',
parameters: undefined,
description: undefined,
categories: ['finance'],
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.stcn.com/article/list/kx.html'],
target: '/article/list/kx',
},
],
view: ViewType.Articles,
};