From c7319828d9271c8b7ed15c6b64817fe346d76c3c Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Thu, 30 May 2024 03:34:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=8A=E6=B5=B7?= =?UTF-8?q?=E5=B8=82=E5=8F=91=E5=B1=95=E5=92=8C=E6=94=B9=E9=9D=A9=E5=A7=94?= =?UTF-8?q?=E5=91=98=E4=BC=9A=20(#15759)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/gov/sh/fgw/index.ts | 155 ++++++++++++++++++ .../gov/sh/fgw/templates/description.art | 17 ++ lib/routes/gov/sh/namespace.ts | 8 + lib/routes/gov/{shanghai => sh}/rsj/ksxm.ts | 4 +- .../{shanghai => sh}/rsj/templates/ksxm.art | 0 .../{shanghai => sh}/wgj/templates/wgj.art | 0 lib/routes/gov/{shanghai => sh}/wgj/wgj.ts | 6 +- .../gov/{shanghai => sh}/wsjkw/yqtb/index.ts | 4 +- lib/routes/gov/{shanghai => sh}/yjj/index.ts | 6 +- 9 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 lib/routes/gov/sh/fgw/index.ts create mode 100644 lib/routes/gov/sh/fgw/templates/description.art create mode 100644 lib/routes/gov/sh/namespace.ts rename lib/routes/gov/{shanghai => sh}/rsj/ksxm.ts (96%) rename lib/routes/gov/{shanghai => sh}/rsj/templates/ksxm.art (100%) rename lib/routes/gov/{shanghai => sh}/wgj/templates/wgj.art (100%) rename lib/routes/gov/{shanghai => sh}/wgj/wgj.ts (97%) rename lib/routes/gov/{shanghai => sh}/wsjkw/yqtb/index.ts (95%) rename lib/routes/gov/{shanghai => sh}/yjj/index.ts (89%) diff --git a/lib/routes/gov/sh/fgw/index.ts b/lib/routes/gov/sh/fgw/index.ts new file mode 100644 index 00000000000000..15e28473354ff1 --- /dev/null +++ b/lib/routes/gov/sh/fgw/index.ts @@ -0,0 +1,155 @@ +import { Route } from '@/types'; +import { getCurrentPath } from '@/utils/helpers'; +const __dirname = getCurrentPath(import.meta.url); + +import cache from '@/utils/cache'; +import got from '@/utils/got'; +import { load } from 'cheerio'; +import timezone from '@/utils/timezone'; +import { parseDate } from '@/utils/parse-date'; +import { art } from '@/utils/render'; +import path from 'node:path'; + +export const handler = async (ctx) => { + const { category = 'fgw_zxxxgk' } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20; + + const rootUrl = 'https://fgw.sh.gov.cn'; + const currentUrl = new URL(`${category}/index.html`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = load(response); + + const language = $('html').prop('lang'); + + let items = $('ul.nowrapli li') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + return { + title: item.find('a').prop('title'), + pubDate: parseDate(item.find('span.time').text()), + link: new URL(item.find('a').prop('href'), rootUrl).href, + language, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + if (!item.link.endsWith('.html')) { + item.enclosure_url = item.link; + item.enclosure_type = item.link ? `application/${item.link.split(/\./).pop()}` : undefined; + item.enclosure_title = item.title; + + return item; + } + + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + const title = $$('meta[name="ArticleTitle"]').prop('content'); + const image = $$('div.pdf-content img').first().prop('src'); + const description = art(path.join(__dirname, 'templates/description.art'), { + images: image + ? [ + { + src: image, + alt: title, + }, + ] + : undefined, + description: $$('div#ivs_content').html(), + }); + + item.title = title; + item.description = description; + item.pubDate = timezone(parseDate($$('meta[name="PubDate"]').prop('content')), +8); + item.category = [...new Set([$$('meta[name="ColumnName"]').prop('content'), $$('meta[name="ColumnKeywords"]').prop('content')])].filter(Boolean); + item.author = $$('meta[name="ContentSource"]').prop('content'); + item.content = { + html: description, + text: $$('div#ivs_content').text(), + }; + item.image = image; + item.banner = image; + item.language = language; + + const enclosureUrl = $$('div.pdf-content a, div.xgfj a').first().prop('href'); + + item.enclosure_url = enclosureUrl ? new URL(enclosureUrl, rootUrl).href : undefined; + item.enclosure_type = enclosureUrl ? `application/${enclosureUrl.split(/\./).pop()}` : undefined; + item.enclosure_title = title; + + return item; + }) + ) + ); + + const author = $('meta[name="SiteName"]').prop('content'); + const image = $('span.logo-icon img').prop('src'); + + return { + title: `${author} - ${$('meta[name="ColumnName"]').prop('content')}`, + description: $('meta[name="ColumnDescription"]').prop('content'), + link: currentUrl, + item: items, + allowEmpty: true, + image, + author, + language, + }; +}; + +export const route: Route = { + path: '/sh/fgw/:category{.+}?', + name: '上海市发展和改革委员会', + url: 'fgw.sh.gov.cn', + maintainers: ['nczitzk'], + handler, + example: '/gov/sh/fgw/fgw_zxxxgk', + parameters: { category: '分类,默认为 `fgw_zxxxgk`,即最新信息公开,可在对应分类页 URL 中找到' }, + description: `:::tip + 若订阅 [最新信息公开](https://fgw.sh.gov.cn/fgw_zxxxgk/index.html),网址为 \`https://fgw.sh.gov.cn/fgw_zxxxgk/index.html\`。截取 \`https://fgw.sh.gov.cn/\` 到末尾 \`/index.html\` 的部分 \`fgw_zxxxgk\` 作为参数填入,此时路由为 [\`/gov/sh/fgw/fgw_zxxxgk\`](https://rsshub.app/gov/sh/fgw/fgw_zxxxgk)。 + ::: + + | 最新信息公开 | 要闻动态 | + | ------------ | ---------- | + | fgw_zxxxgk | fgw_fzggdt | + `, + categories: ['government'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['fgw.sh.gov.cn/:category'], + target: (params) => { + const category = params.category.replace(/\/index\.html/, ''); + + return `/gov/sh/fgw${category ? `/${category}` : ''}`; + }, + }, + { + title: '最新信息公开', + source: ['fgw.sh.gov.cn/fgw_zxxxgk/index.html'], + target: '/sh/fgw/fgw_zxxxgk', + }, + { + title: '要闻动态', + source: ['fgw.sh.gov.cn/fgw_fzggdt/index.html'], + target: '/sh/fgw/fgw_fzggdt', + }, + ], +}; diff --git a/lib/routes/gov/sh/fgw/templates/description.art b/lib/routes/gov/sh/fgw/templates/description.art new file mode 100644 index 00000000000000..dfab19230c1108 --- /dev/null +++ b/lib/routes/gov/sh/fgw/templates/description.art @@ -0,0 +1,17 @@ +{{ if images }} + {{ each images image }} + {{ if image?.src }} +
+ {{ image.alt }} +
+ {{ /if }} + {{ /each }} +{{ /if }} + +{{ if description }} + {{@ description }} +{{ /if }} \ No newline at end of file diff --git a/lib/routes/gov/sh/namespace.ts b/lib/routes/gov/sh/namespace.ts new file mode 100644 index 00000000000000..19f5da575d01fb --- /dev/null +++ b/lib/routes/gov/sh/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: '上海市人民政府', + url: 'sh.gov.cn', + categories: ['government'], + description: '', +}; diff --git a/lib/routes/gov/shanghai/rsj/ksxm.ts b/lib/routes/gov/sh/rsj/ksxm.ts similarity index 96% rename from lib/routes/gov/shanghai/rsj/ksxm.ts rename to lib/routes/gov/sh/rsj/ksxm.ts index 5b9425158b63c9..c3394751296615 100644 --- a/lib/routes/gov/shanghai/rsj/ksxm.ts +++ b/lib/routes/gov/sh/rsj/ksxm.ts @@ -11,9 +11,9 @@ import path from 'node:path'; const rootUrl = 'http://www.rsj.sh.gov.cn'; export const route: Route = { - path: '/shanghai/rsj/ksxm', + path: '/sh/rsj/ksxm', categories: ['government'], - example: '/gov/shanghai/rsj/ksxm', + example: '/gov/sh/rsj/ksxm', parameters: {}, features: { requireConfig: false, diff --git a/lib/routes/gov/shanghai/rsj/templates/ksxm.art b/lib/routes/gov/sh/rsj/templates/ksxm.art similarity index 100% rename from lib/routes/gov/shanghai/rsj/templates/ksxm.art rename to lib/routes/gov/sh/rsj/templates/ksxm.art diff --git a/lib/routes/gov/shanghai/wgj/templates/wgj.art b/lib/routes/gov/sh/wgj/templates/wgj.art similarity index 100% rename from lib/routes/gov/shanghai/wgj/templates/wgj.art rename to lib/routes/gov/sh/wgj/templates/wgj.art diff --git a/lib/routes/gov/shanghai/wgj/wgj.ts b/lib/routes/gov/sh/wgj/wgj.ts similarity index 97% rename from lib/routes/gov/shanghai/wgj/wgj.ts rename to lib/routes/gov/sh/wgj/wgj.ts index 523aabf3e50cec..9b65d9a17459c4 100644 --- a/lib/routes/gov/shanghai/wgj/wgj.ts +++ b/lib/routes/gov/sh/wgj/wgj.ts @@ -10,9 +10,9 @@ import { art } from '@/utils/render'; import path from 'node:path'; export const route: Route = { - path: '/shanghai/wgj/:page?', + path: '/sh/wgj/:page?', categories: ['government'], - example: '/gov/shanghai/wgj', + example: '/gov/sh/wgj', parameters: { page: '页数,默认第 1 页' }, features: { requireConfig: false, @@ -25,7 +25,7 @@ export const route: Route = { radar: [ { source: ['wsbs.wgj.sh.gov.cn/'], - target: '/shanghai/wgj', + target: '/sh/wgj', }, ], name: '上海市文旅局审批公告', diff --git a/lib/routes/gov/shanghai/wsjkw/yqtb/index.ts b/lib/routes/gov/sh/wsjkw/yqtb/index.ts similarity index 95% rename from lib/routes/gov/shanghai/wsjkw/yqtb/index.ts rename to lib/routes/gov/sh/wsjkw/yqtb/index.ts index cd97daaa477a4d..316282ada28d44 100644 --- a/lib/routes/gov/shanghai/wsjkw/yqtb/index.ts +++ b/lib/routes/gov/sh/wsjkw/yqtb/index.ts @@ -4,9 +4,9 @@ import got from '@/utils/got'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { - path: '/shanghai/wsjkw/yqtb', + path: '/sh/wsjkw/yqtb', categories: ['government'], - example: '/gov/shanghai/wsjkw/yqtb', + example: '/gov/sh/wsjkw/yqtb', parameters: {}, features: { requireConfig: false, diff --git a/lib/routes/gov/shanghai/yjj/index.ts b/lib/routes/gov/sh/yjj/index.ts similarity index 89% rename from lib/routes/gov/shanghai/yjj/index.ts rename to lib/routes/gov/sh/yjj/index.ts index d7725c06e4148c..f1e7349a28b364 100644 --- a/lib/routes/gov/shanghai/yjj/index.ts +++ b/lib/routes/gov/sh/yjj/index.ts @@ -7,17 +7,17 @@ import timezone from '@/utils/timezone'; import { parseDate } from '@/utils/parse-date'; export const route: Route = { - path: '/shanghai/yjj/*', + path: '/sh/yjj/*', name: 'Unknown', maintainers: [], handler, }; async function handler(ctx) { - const params = getSubPath(ctx) === '/shanghai/yjj' ? '/shanghai/yjj/zx-ylqx' : getSubPath(ctx); + const params = getSubPath(ctx) === '/sh/yjj' ? '/sh/yjj/zx-ylqx' : getSubPath(ctx); const rootUrl = 'https://yjj.sh.gov.cn'; - const currentUrl = `${rootUrl}${params.replace(/^\/shanghai\/yjj/, '')}/index.html`; + const currentUrl = `${rootUrl}${params.replace(/^\/sh\/yjj/, '')}/index.html`; const response = await got({ method: 'get',