From c9910b7d79a391c53e5517f08046bf4c76c385ac Mon Sep 17 00:00:00 2001 From: zvrr Date: Fri, 5 Apr 2024 23:14:39 +0800 Subject: [PATCH] feat(route): add dangyuanwang (#14535) * feat(route): add dangyuanwang * fiexd dangyuanwang doc example path * fiexd dangyuanwang maintainer info * feat(route): update dangyuanwang doc * refactor: migrate to v3 --------- Co-authored-by: zvrr --- lib/routes/12371/namespace.ts | 7 ++++ lib/routes/12371/zxfb.ts | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 lib/routes/12371/namespace.ts create mode 100644 lib/routes/12371/zxfb.ts diff --git a/lib/routes/12371/namespace.ts b/lib/routes/12371/namespace.ts new file mode 100644 index 000000000..1d2b13093 --- /dev/null +++ b/lib/routes/12371/namespace.ts @@ -0,0 +1,7 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '共产党员网', + url: 'www.12371.cn', + categories: ['government'], +}; diff --git a/lib/routes/12371/zxfb.ts b/lib/routes/12371/zxfb.ts new file mode 100644 index 000000000..cb87d363c --- /dev/null +++ b/lib/routes/12371/zxfb.ts @@ -0,0 +1,64 @@ +import got from '@/utils/got'; +import * as cheerio from 'cheerio'; +import { parseDate } from '@/utils/parse-date'; +import timezone from '@/utils/timezone'; +import cache from '@/utils/cache'; + +import { Route } from '@/types'; + +const handler = async (ctx) => { + const { category = 'zxfb' } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 15; + + const rootUrl = 'https://www.12371.cn/'; + const currentUrl = `${rootUrl}${category}/`; + const response = await got(currentUrl); + + const $ = cheerio.load(response.data); + + const pattern = /item=(\[{.*?}]);/; + const newsList = JSON.parse($('script[language="javascript"]').text().match(pattern)?.[1].replaceAll("'", '"') || '[]'); + + const topNewsList = newsList.slice(0, limit).map((item) => ({ + title: item.title, + pubDate: timezone(parseDate(item.brief, 'YYYY-MM-DD HH:mm:ss'), 8), + link: item.link_add, + })); + + const items = await Promise.all( + topNewsList.map((item) => + cache.tryGet(item.link, async () => { + const detailResponse = await got(item.link); + const $ = cheerio.load(detailResponse.data); + + item.description = $('.word').html(); + + return item; + }) + ) + ); + + return { + title: $('title').text(), + link: currentUrl, + item: items, + }; +}; + +export const route: Route = { + path: '/:category?', + example: '/12371/zxfb', + parameters: { category: '新闻分类名,预设 `zxfb`' }, + radar: [ + { + source: ['www.12371.cn/:category'], + }, + ], + name: '最新发布', + maintainers: ['zvrr'], + handler, + url: 'www.12371.cn', + description: `| 最新发布 | + | :------: | + | zxfb |`, +};