From f4baae0a4a1ae571f0f827e507cbafb2467d7716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=86=E4=B8=BA=E5=90=9B=E6=95=85?= Date: Thu, 23 Jan 2025 06:13:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(route/txks):=20add=20www.txks.org.cn=20(?= =?UTF-8?q?=E5=B7=A5=E4=BF=A1=E9=83=A8=E5=85=A8=E5=9B=BD=E9=80=9A=E4=BF=A1?= =?UTF-8?q?=E4=B8=93=E4=B8=9A=E6=8A=80=E6=9C=AF=E4=BA=BA=E5=91=98=E8=81=8C?= =?UTF-8?q?=E4=B8=9A=E6=B0=B4=E5=B9=B3=E8=80=83=E8=AF=95)=20(#18151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add www.txks.org.cn * fix: fix router name * fix: remove font presetting. * fix: fix namespace. * fix: migrate jsdom to cheerio * fix: fix news-router name --- lib/routes/txks/namespace.ts | 7 +++ lib/routes/txks/news.ts | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 lib/routes/txks/namespace.ts create mode 100644 lib/routes/txks/news.ts diff --git a/lib/routes/txks/namespace.ts b/lib/routes/txks/namespace.ts new file mode 100644 index 00000000000000..39ff09c37ef7b8 --- /dev/null +++ b/lib/routes/txks/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '全国通信专业技术人员职业水平考试', + url: 'www.txks.org.cn', + lang: 'zh-CN', +}; diff --git a/lib/routes/txks/news.ts b/lib/routes/txks/news.ts new file mode 100644 index 00000000000000..455fe249cdb7e3 --- /dev/null +++ b/lib/routes/txks/news.ts @@ -0,0 +1,107 @@ +import type { DataItem, Route } from '@/types'; +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { parseDate } from '@/utils/parse-date'; +import { load } from 'cheerio'; + +const BASE_URL = 'https://www.txks.org.cn/index/work'; + +const removeFontPresetting = (html: string = ''): string => { + const $ = load(html); + $('[style]').each((_, element) => { + const style = $(element).attr('style') || ''; + const cleanedStyle = style.replaceAll(/font-family:[^;]*;?/gi, '').trim(); + $(element).attr('style', cleanedStyle || null); + }); + $('style').each((_, styleElement) => { + const cssText = $(styleElement).html() || ''; + const cleanedCssText = cssText.replaceAll(/font-family:[^;]*;?/gi, ''); + $(styleElement).html(cleanedCssText); + }); + + return $.html(); +}; + +const handler: Route['handler'] = async () => { + // Fetch the index page + const { data: listResponse } = await got(BASE_URL); + const $ = load(listResponse); + + // Select all list items containing news information + const ITEM_SELECTOR = 'ul[class*="newsList"] > li'; + const listItems = $(ITEM_SELECTOR); + + // Map through each list item to extract details + const contentLinkList = listItems.toArray().map((element) => { + const date = $(element).find('label.time').text().trim().slice(1, -1); + const title = $(element).find('a').attr('title')!; + const link = $(element).find('a').attr('href')!; + + const formattedDate = parseDate(date); + return { + date: formattedDate, + title, + link, + }; + }); + + return { + title: '全国通信专业技术人员职业水平考试', + description: '全国通信专业技术人员职业水平考试网站最新动态和消息推送', + link: BASE_URL, + image: 'https://www.txks.org.cn/asset/image/logo/logo.png', + item: (await Promise.all( + contentLinkList.map((item) => + cache.tryGet(item.link, async () => { + const CONTENT_SELECTOR = '#contentTxt'; + const { data: contentResponse } = await got(item.link); + const contentPage = load(contentResponse); + const content = removeFontPresetting(contentPage(CONTENT_SELECTOR).html() || ''); + return { + title: item.title, + pubDate: item.date, + link: item.link, + description: content, + category: ['study'], + guid: item.link, + id: item.link, + image: 'https://www.txks.org.cn/asset/image/logo/logo.png', + content, + updated: item.date, + language: 'zh-CN', + }; + }) + ) + )) as DataItem[], + allowEmpty: true, + language: 'zh-CN', + feedLink: 'https://rsshub.app/txks/news', + id: 'https://rsshub.app/txks/news', + }; +}; + +export const route: Route = { + path: '/news', + name: '通信考试动态', + description: '**注意:** 官方网站限制了国外网络请求,可能需要通过部署在中国大陆内的 RSSHub 实例访问。', + maintainers: ['PrinOrange'], + handler, + categories: ['study'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + supportRadar: true, + }, + radar: [ + { + title: '全国通信专业技术人员职业水平考试动态', + source: ['www.txks.org.cn/index/work', 'www.txks.org.cn'], + target: `/news`, + }, + ], + example: '/txks/news', +};