From e2a7209ed25f8c989689d20180a8a9659b3ff85f Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 27 Dec 2025 22:43:12 +0800 Subject: [PATCH] feat(route): add Semiconductor Industry Association (#20760) --- lib/routes/semiconductors/index.ts | 189 ++++++++++++++++++ lib/routes/semiconductors/namespace.ts | 9 + .../semiconductors/templates/description.art | 27 +++ 3 files changed, 225 insertions(+) create mode 100644 lib/routes/semiconductors/index.ts create mode 100644 lib/routes/semiconductors/namespace.ts create mode 100644 lib/routes/semiconductors/templates/description.art diff --git a/lib/routes/semiconductors/index.ts b/lib/routes/semiconductors/index.ts new file mode 100644 index 000000000..54f0baffd --- /dev/null +++ b/lib/routes/semiconductors/index.ts @@ -0,0 +1,189 @@ +import path from 'node:path'; + +import type { Cheerio, CheerioAPI } from 'cheerio'; +import { load } from 'cheerio'; +import type { Element } from 'domhandler'; +import type { Context } from 'hono'; + +import type { Data, DataItem, Route } from '@/types'; +import { ViewType } from '@/types'; +import cache from '@/utils/cache'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; + +export const handler = async (ctx: Context): Promise => { + const { category = 'news-events/latest-news' } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '12', 10); + + const baseUrl: string = 'https://www.semiconductors.org'; + const targetUrl: string = new URL(category.endsWith('/') ? category : `${category}/`, baseUrl).href; + + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'en'; + + let items: DataItem[] = []; + + items = $('div.col-sm-8') + .slice(0, limit) + .toArray() + .map((el): Element => { + const $el: Cheerio = $(el); + const $aEl: Cheerio = $el.find('a').first(); + + const title: string = $aEl.text(); + const image: string | undefined = $el + .prev() + .find('img') + .attr('src') + ?.replace(/-\d+x\d+\./, '.'); + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + intro: $aEl.next().text(), + }); + const pubDateStr: string | undefined = $el.find('div.resource-item-meta').text().split(/:\s+/).pop(); + const linkUrl: string | undefined = $aEl.attr('href'); + const categoryEls: Element[] = $el.find('div.resource-item-category span.ric').toArray(); + const categories: string[] = [...new Set(categoryEls.map((el) => $(el).text()).filter(Boolean))]; + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr, 'DD/MM/YY') : undefined, + link: linkUrl ? new URL(linkUrl, baseUrl).href : undefined, + category: categories, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr, 'DD/MM/YY') : undefined, + language, + }; + + return processedItem; + }); + + items = await Promise.all( + items.map((item) => { + if (!item.link) { + return item; + } + + return cache.tryGet(item.link, async (): Promise => { + const detailResponse = await ofetch(item.link); + const $$: CheerioAPI = load(detailResponse); + + const title: string = $$('h1').text(); + + $$('h1').remove(); + $$('main#main').contents().first().remove(); + $$('main#main p').first().remove(); + $$('div.newshr').remove(); + + const image: string | undefined = $$('meta[property="og:image"]') + .attr('content') + ?.replace(/-scaled\./, '.'); + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + description: $$('main#main').html(), + }); + const pubDateStr: string | undefined = $$('meta[property="article:published_time"]').attr('content'); + const authorEls: Element[] = $$('meta[name="author"]').toArray(); + const authors: DataItem['author'] = authorEls.map((authorEl) => { + const $$authorEl: Cheerio = $$(authorEl); + + return { + name: $$authorEl.attr('content'), + url: undefined, + avatar: undefined, + }; + }); + const upDatedStr: string | undefined = $$('meta[property="article:modified_time"]').attr('content'); + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate, + author: authors, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr) : item.updated, + language, + }; + + return { + ...item, + ...processedItem, + }; + }); + }) + ); + + return { + title: $('title').text(), + description: $('meta[property="og:description"]').attr('content'), + link: targetUrl, + item: items, + allowEmpty: true, + image: $('meta[property="og:image"]').attr('content'), + author: $('meta[property="og:site_name"]').attr('content'), + language, + id: $('meta[property="og:url"]').attr('content'), + }; +}; + +export const route: Route = { + path: '/:category{.+}?', + name: 'Latest News', + url: 'www.semiconductors.org', + maintainers: ['nczitzk'], + handler, + example: '/semiconductors/news-events/latest-news', + parameters: { + category: { + description: 'Category, `news-events/latest-news` by default', + }, + }, + description: `:::tip +To subscribe to [Latest News](https://www.semiconductors.org/news-events/latest-news/), where the source URL is \`https://www.semiconductors.org/news-events/latest-news/\`, extract the certain parts from this URL to be used as parameters, resulting in the route as [\`/semiconductors/news-events/latest-news\`](https://rsshub.app/semiconductors/news-events/latest-news). +:::`, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.semiconductors.org/:category'], + target: '/:category', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/semiconductors/namespace.ts b/lib/routes/semiconductors/namespace.ts new file mode 100644 index 000000000..60007bdfa --- /dev/null +++ b/lib/routes/semiconductors/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'Semiconductor Industry Association', + url: 'semiconductors.org', + categories: ['new-media'], + description: '', + lang: 'en', +}; diff --git a/lib/routes/semiconductors/templates/description.art b/lib/routes/semiconductors/templates/description.art new file mode 100644 index 000000000..bfb1a0ff6 --- /dev/null +++ b/lib/routes/semiconductors/templates/description.art @@ -0,0 +1,27 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} + +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file