From 2bc358296ee84f21ffa2ecf5bebf80d7b1da1213 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 30 Sep 2024 01:18:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E4=B8=AD=E5=8D=8E?= =?UTF-8?q?=E4=BA=BA=E6=B0=91=E5=85=B1=E5=92=8C=E5=9B=BD=E5=86=9C=E4=B8=9A?= =?UTF-8?q?=E5=86=9C=E6=9D=91=E9=83=A8=E7=94=9F=E7=8C=AA=E4=B8=93=E9=A2=98?= =?UTF-8?q?=E9=87=8D=E8=A6=81=E6=94=BF=E7=AD=96=20(#16961)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/gov/moa/moa.ts | 6 +- lib/routes/gov/moa/szcpxx.ts | 115 +++++++++++++++++++++++++++++++++++ lib/routes/gov/moa/zdscxx.ts | 94 +++++++++++++++++++++------- 3 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 lib/routes/gov/moa/szcpxx.ts diff --git a/lib/routes/gov/moa/moa.ts b/lib/routes/gov/moa/moa.ts index 2033b52bc..e1e2e2f4d 100644 --- a/lib/routes/gov/moa/moa.ts +++ b/lib/routes/gov/moa/moa.ts @@ -8,13 +8,13 @@ const hostUrl = 'http://www.moa.gov.cn/'; const hostUrlObj = new URL(hostUrl); // 用于在下面判断 host export const route: Route = { - path: '/moa/:suburl{.+}', + path: '/moa/suburl/:suburl{.+}', categories: ['government'], - example: '/gov/moa/gk/zcjd/', + example: '/gov/moa/suburl/gk/zcjd/', radar: [ { source: ['moa.gov.cn/'], - target: '/moa/:suburl', + target: '/moa/suburl/:suburl', }, ], parameters: { suburl: '下级目录,请使用最下级的目录' }, diff --git a/lib/routes/gov/moa/szcpxx.ts b/lib/routes/gov/moa/szcpxx.ts new file mode 100644 index 000000000..b8dff425a --- /dev/null +++ b/lib/routes/gov/moa/szcpxx.ts @@ -0,0 +1,115 @@ +import { Route } from '@/types'; + +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'; + +export const handler = async (ctx) => { + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 6; + + const rootUrl = 'http://www.moa.gov.cn'; + const currentUrl = new URL(`ztzl/szcpxx/zyzc/index.htm`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = load(response); + + const language = $('html').prop('lang'); + + let items = $('div.ztst_list_contBox_inner ul li') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const a = item.find('a.content'); + + return { + title: a.prop('title'), + pubDate: parseDate(item.find('div.pubTime').text().split(/:/).pop(), 'YYYY.MM.DD'), + link: new URL(a.prop('href'), currentUrl).href, + language, + }; + }); + + items = await Promise.all( + items.map((item) => + cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const $$ = load(detailResponse); + + const title = $$('h2.xxgk_title, h1.bjjMTitle').text(); + const description = $$('div.gsj_htmlcon_bot, div.TRS_Editor').html(); + const guid = `moa-${$$('meta[name="contentid"]').prop('content')}`; + + item.title = title; + item.description = description; + item.pubDate = timezone(parseDate($$('meta[name="PubDate"]').prop('content')), +8); + item.category = [ + ...new Set([ + $$('meta[name="SiteName"]').prop('content'), + $$('meta[name="ColumnName"]').prop('content'), + $$('meta[name="ColumnType"]').prop('content'), + $$('meta[name="ContentSource"]').prop('content'), + $$('meta[name="Keywords"]').prop('content'), + ]), + ].filter(Boolean); + item.author = $$('meta[name="Author"]').prop('content') || $$('meta[name="source"]').prop('content'); + item.guid = guid; + item.id = guid; + item.content = { + html: description, + text: $$('div.gsj_htmlcon_bot, div.TRS_Editor').text(), + }; + item.language = language; + + return item; + }) + ) + ); + + const title = `${$('title').text()} - ${$('li.now').text()}`; + const image = new URL($('img.leftLogo').prop('src'), currentUrl).href; + + return { + title, + description: title, + link: currentUrl, + item: items, + allowEmpty: true, + image, + author: '中华人民共和国农业农村部', + language, + }; +}; + +export const route: Route = { + path: '/moa/szcpxx', + name: '中华人民共和国农业农村部生猪专题重要政策', + url: 'www.moa.gov.cn', + maintainers: ['nczitzk'], + handler, + example: '/gov/moa/szcpxx', + parameters: undefined, + description: undefined, + categories: ['government'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.moa.gov.cn/ztzl/szcpxx/zyzc/index.htm'], + target: '/moa/szcpxx', + }, + ], +}; diff --git a/lib/routes/gov/moa/zdscxx.ts b/lib/routes/gov/moa/zdscxx.ts index 24284138e..0e1b88596 100644 --- a/lib/routes/gov/moa/zdscxx.ts +++ b/lib/routes/gov/moa/zdscxx.ts @@ -1,18 +1,12 @@ import { Route } from '@/types'; + import cache from '@/utils/cache'; import got from '@/utils/got'; import { load } from 'cheerio'; import { parseDate } from '@/utils/parse-date'; -export const route: Route = { - path: ['/moa/sjzxfb/:category{.+}?', '/moa/zdscxx/:category{.+}?'], - name: 'Unknown', - maintainers: [], - handler, -}; - -async function handler(ctx) { - const category = ctx.req.param('category'); +export const handler = async (ctx) => { + const { category } = ctx.req.param(); const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 5; const domain = 'moa.gov.cn'; @@ -36,7 +30,7 @@ async function handler(ctx) { }); const filters = filterResponse.result.reduce((filters, f) => { - filters[f.name] = f.data.map((d) => d.name); + filters[f.name.trim()] = f.data.map((d) => d.name.trim()); return filters; }, {}); @@ -92,20 +86,76 @@ async function handler(ctx) { const $ = load(frameResponse); const title = $('title').text(); - const description = '数据'; - const icon = new URL('favicon.ico', rootUrl).href; return { - item: items, - title: `${title} - ${description} - ${category}`, + title: `${title}${category ? ` - ${category}` : ''}`, + description: '数据', link: currentUrl, - description, - language: 'zh', - image: $('h1.logo a img').prop('src'), - icon, - logo: icon, - subtitle: category, - author: title, + item: items, allowEmpty: true, + image: $('h1.logo a img').prop('src'), + author: title, }; -} +}; + +export const route: Route = { + path: '/moa/zdscxx/:category{.+}?', + name: '中华人民共和国农业农村部数据', + url: 'www.moa.gov.cn', + maintainers: ['nczitzk'], + handler, + example: '/gov/moa/zdscxx', + parameters: { category: '分类,默认为全部,见下表' }, + description: `:::tip + 若订阅 [中华人民共和国农业农村部数据](http://zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp) 的 \`价格指数\` 报告主题。此时路由为 [\`/gov/moa/zdscxx/价格指数\`](https://rsshub.app/gov/moa/zdscxx/价格指数)。 + + 若订阅 \`央视网\` 报告来源 的 \`蔬菜生产\` 报告主题。此时路由为 [\`/gov/moa/zdscxx/央视网/蔬菜生产\`](https://rsshub.app/gov/moa/zdscxx/央视网/蔬菜生产)。 + ::: + + | 价格指数 | 供需形势 | 分析报告周报 | 分析报告日报 | 日历信息 | 蔬菜生产 | + | -------- | -------- | ------------ | ------------ | -------- | -------- | + `, + categories: ['government'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + title: '价格指数', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/价格指数', + }, + { + title: '供需形势', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/供需形势', + }, + { + title: '分析报告周报', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/分析报告周报', + }, + { + title: '分析报告日报', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/分析报告日报', + }, + { + title: '日历信息', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/日历信息', + }, + { + title: '蔬菜生产', + source: ['zdscxx.moa.gov.cn:8080/nyb/pc/messageList.jsp'], + target: '/gov/moa/zdscxx/蔬菜生产', + }, + ], +};