From 9847673e925b3dae3cda5fb0bf110ab91cf241e3 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 14 Apr 2025 00:56:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=B9=BF=E5=91=8A?= =?UTF-8?q?=E9=97=A8=E6=9C=80=E6=96=B0=E6=96=87=E7=AB=A0=20(#18839)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/adquan/index.ts | 146 ++++++++++++++++++++ lib/routes/adquan/namespace.ts | 9 ++ lib/routes/adquan/templates/description.art | 7 + 3 files changed, 162 insertions(+) create mode 100644 lib/routes/adquan/index.ts create mode 100644 lib/routes/adquan/namespace.ts create mode 100644 lib/routes/adquan/templates/description.art diff --git a/lib/routes/adquan/index.ts b/lib/routes/adquan/index.ts new file mode 100644 index 0000000000..5725762ebc --- /dev/null +++ b/lib/routes/adquan/index.ts @@ -0,0 +1,146 @@ +import { type Data, type DataItem, type Route, ViewType } from '@/types'; + +import { art } from '@/utils/render'; +import cache from '@/utils/cache'; +import { getCurrentPath } from '@/utils/helpers'; +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'; +import path from 'node:path'; + +const __dirname = getCurrentPath(import.meta.url); + +export const handler = async (ctx: Context): Promise => { + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '30', 10); + + const baseUrl: string = 'https://www.adquan.com'; + const targetUrl: string = baseUrl; + + const response = await ofetch(targetUrl); + const $: CheerioAPI = load(response); + const language = $('html').attr('lang') ?? 'zh-CN'; + + let items: DataItem[] = []; + + items = $('div.article_1') + .slice(0, limit) + .toArray() + .map((el): Element => { + const $el: Cheerio = $(el); + + const title: string = $el.find('p.article_2_p').text(); + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + intro: $el.find('div.article_1_fu p').first().text(), + }); + const pubDateStr: string | undefined = $el.find('div.article_1_fu p').last().text(); + const linkUrl: string | undefined = $el.find('a.article_2_href').attr('href'); + const authors: DataItem['author'] = $el.find('div.article_4').text(); + const image: string | undefined = $el.find('img.article_1_img').attr('src'); + const upDatedStr: string | undefined = pubDateStr; + + const processedItem: DataItem = { + title, + description, + pubDate: pubDateStr ? parseDate(pubDateStr) : undefined, + link: linkUrl, + author: authors, + content: { + html: description, + text: description, + }, + image, + banner: image, + updated: upDatedStr ? parseDate(upDatedStr) : 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 = $$('p.infoTitle_left').text(); + const description: string | undefined = art(path.join(__dirname, 'templates/description.art'), { + description: $$('div.articleContent').html(), + }); + const pubDateStr: string | undefined = $$('p.time').text().split(/:/).pop(); + const categoryEls: Element[] = $$('span.article_5').toArray(); + const categories: string[] = [...new Set(categoryEls.map((el) => $$(el).text()).filter(Boolean))]; + const authors: DataItem['author'] = $$('div.infoTitle_right span').text(); + 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.navi_logo').attr('src'), + author: $('meta[name="author"]').attr('content'), + language, + id: targetUrl, + }; +}; + +export const route: Route = { + path: '/', + name: '最新文章', + url: 'www.adquan.com', + maintainers: ['nczitzk'], + handler, + example: '/adquan', + parameters: undefined, + description: undefined, + categories: ['new-media'], + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.adquan.com'], + target: '/', + }, + ], + view: ViewType.Articles, +}; diff --git a/lib/routes/adquan/namespace.ts b/lib/routes/adquan/namespace.ts new file mode 100644 index 0000000000..68736d1fe4 --- /dev/null +++ b/lib/routes/adquan/namespace.ts @@ -0,0 +1,9 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '广告门', + url: 'adquan.com', + categories: ['new-media'], + description: '一个行业的跌宕起伏', + lang: 'zh-CN', +}; diff --git a/lib/routes/adquan/templates/description.art b/lib/routes/adquan/templates/description.art new file mode 100644 index 0000000000..57498ab45a --- /dev/null +++ b/lib/routes/adquan/templates/description.art @@ -0,0 +1,7 @@ +{{ if intro }} +
{{ intro }}
+{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file