From 5ea89171950d51b5cf943b5587bb67f0d91fdc04 Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Mon, 21 Oct 2024 01:41:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=B8=AD=E5=8D=8E=E4=BA=BA?= =?UTF-8?q?=E6=B0=91=E5=85=B1=E5=92=8C=E5=9B=BD=E5=9B=BD=E5=AE=B6=E5=8F=91?= =?UTF-8?q?=E5=B1=95=E5=92=8C=E6=94=B9=E9=9D=A9=E5=A7=94=E5=91=98=E4=BC=9A?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC=E7=9B=91=E6=B5=8B=E4=B8=AD=E5=BF=83=20(#1720?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/gov/jgjcndrc/index.ts | 161 ++++++++++++++++++------------- 1 file changed, 95 insertions(+), 66 deletions(-) diff --git a/lib/routes/gov/jgjcndrc/index.ts b/lib/routes/gov/jgjcndrc/index.ts index 36b54c93d02968..535c44be5bf7d1 100644 --- a/lib/routes/gov/jgjcndrc/index.ts +++ b/lib/routes/gov/jgjcndrc/index.ts @@ -1,96 +1,125 @@ 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 route: Route = { - path: '/jgjcndrc/:id?', - categories: ['government'], - example: '/gov/jgjcndrc', - parameters: { id: '栏目 id,见下表,默认为 692,即通知公告' }, - features: { - requireConfig: false, - requirePuppeteer: false, - antiCrawler: false, - supportBT: false, - supportPodcast: false, - supportScihub: false, - }, - name: '价格监测中心', - maintainers: ['nczitzk'], - handler, - description: `| 通知公告 | 中心工作动态 | 地方工作动态 | 监测信息 | 分析预测 | 调查报告 | - | -------- | ------------ | ------------ | -------- | -------- | -------- | - | 692 | 693 | 694 | 695 | 696 | 697 | - - | 价格指数 | 地方价格监测 | 价格监测报告制度 | 监测法规 | 媒体聚焦 | - | -------- | ------------ | ---------------- | -------- | -------- | - | 698 | 699 | 700 | 701 | 753 | - - #### 监测信息 +export const handler = async (ctx) => { + const { columnId = '1832739866673426433', subColumnId } = ctx.req.param(); + const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 30; - | 国内外市场价格监测情况周报 | 主要粮油副食品日报 | 生猪出厂价与玉米价格周报 | 国际市场石油价格每日 动态 | - | -------------------------- | ------------------ | ------------------------ | ------------------------- | - | 749 | 703 | 704 | 705 | + const rootUrl = 'https://www.jgjcndrc.org.cn'; + const currentUrl = new URL(`list?clmId=${columnId}${subColumnId ? `&sclmId=${subColumnId}` : ''}`, rootUrl).href; + const apiColumnUrl = new URL(`prod-api/portal/artPageByColumn/${subColumnId ?? columnId}`, rootUrl).href; - | 非学科类培训服务价格 | 监测周期价格动态 | 月度监测行情表 | 猪料、鸡料、蛋料比价 | - | -------------------- | ---------------- | -------------- | -------------------- | - | 821 | 706 | 707 | 708 |`, -}; + const { data: currentResponse } = await got(currentUrl); -async function handler(ctx) { - const { id = 'sytzgg' } = ctx.req.param(); - const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 30; + const $ = load(currentResponse); - const rootUrl = 'http://www.jgjcndrc.org.cn'; - const currentUrl = new URL(`list.aspx?clmId=${id}`, rootUrl).href; + const language = $('html').prop('lang'); - const { data: response } = await got(currentUrl); + const { data: response } = await got(apiColumnUrl, { + searchParams: { + pageNum: 1, + pageSize: limit, + }, + }); - const $ = load(response); + let items = response.data.slice(0, limit).map((item) => { + const guid = `jgjcndrc-${item.articleId}`; - let items = $('ul.list_02 li.li a[title]') - .slice(0, limit) - .toArray() - .map((item) => { - item = $(item); - - return { - title: item.text(), - link: new URL(item.prop('href'), rootUrl).href, - pubDate: parseDate(item.prev().text()), - }; - }); + return { + title: item.articleTitle, + pubDate: parseDate(item.pubDate), + link: new URL(`prod-api/portal/article/${item.articleId}`, rootUrl).href, + guid, + id: guid, + language, + }; + }); items = await Promise.all( items.map((item) => cache.tryGet(item.link, async () => { const { data: detailResponse } = await got(item.link); - const content = load(detailResponse); - - item.title = content('div.txt_title1').text(); - item.description = content('div#zoom').html(); - item.pubDate = parseDate(content('div.txt_subtitle1').text().trim()); + const data = detailResponse.data; + + const title = data.articleTitle; + const description = data.articleContent; + const guid = `jgjcndrc-${data.articleId}`; + + item.title = title; + item.description = description; + item.pubDate = timezone(parseDate(data.pubDate), +8); + item.link = data.linkUrl || new URL(data.articleUrl, rootUrl).href; + item.category = data.parentColumns.map((c) => c.columnName); + item.guid = guid; + item.id = guid; + item.content = { + html: description, + text: description, + }; + item.language = language; return item; }) ) ); - const author = $('title').text(); - const subtitle = $('li.L').first().text(); - const image = new URL($('img.logo2').prop('src'), rootUrl).href; + const image = new URL($('header img').last().prop('src'), rootUrl).href; return { - item: items, - title: `${author} - ${subtitle}`, + title: `${$('title').text()}${$('div.tit').text() ? ` - ${$('div.tit').text()}` : ''}`, + description: $('meta[name="description"]').prop('content'), link: currentUrl, - description: author, - language: $('html').prop('lang'), + item: items, + allowEmpty: true, image, - subtitle, - author, + author: $('header h1').text(), + language, }; -} +}; + +export const route: Route = { + path: '/jgjcndrc/:columnId?/:subColumnId?', + name: '中华人民共和国国家发展和改革委员会价格监测中心', + url: 'www.jgjcndrc.org.cn', + maintainers: ['nczitzk'], + handler, + example: '/gov/jgjcndrc/1832739866673426433', + parameters: { + columnId: '栏目 id,默认为 `1832739866673426433`,即通知公告,可在对应栏目页 URL 中找到', + subColumnId: '子栏目 id,默认为空,可在对应子栏目页 URL 中找到', + }, + description: `:::tip + 若订阅 [通知公告](https://www.jgjcndrc.org.cn/list?clmId=1832739866673426433),网址为 \`https://www.jgjcndrc.org.cn/list?clmId=1832739866673426433\`。截取 \`clmId\` 的参数部分 \`1832739866673426433\` 作为参数填入,此时路由为 [\`/gov/jgjcndrc/1832739866673426433\`](https://rsshub.app/gov/jgjcndrc/1832739866673426433)。 + + 若订阅 [国内外市场价格监测情况周报](https://www.jgjcndrc.org.cn/list?clmId=1832298113994649601&sclmId=1832751799531220993),网址为 \`https://www.jgjcndrc.org.cn/list?clmId=1832298113994649601&sclmId=1832751799531220993\`。截取 \`clmId\` 和 \`sclmId\` 的参数部分 \`1832298113994649601\` 和 \`1832751799531220993\` 作为参数填入,此时路由为 [\`/gov/jgjcndrc/1832298113994649601/1832751799531220993\`](https://rsshub.app/gov/jgjcndrc/1832298113994649601/1832751799531220993)。 + :::`, + categories: ['government'], + + features: { + requireConfig: false, + requirePuppeteer: false, + antiCrawler: false, + supportRadar: true, + supportBT: false, + supportPodcast: false, + supportScihub: false, + }, + radar: [ + { + source: ['www.jgjcndrc.org.cn/list'], + target: (_, url) => { + url = new URL(url); + const columnId = url.searchParams.get('clmId'); + const subColumnId = url.searchParams.get('sclmId'); + + return `/jgjcndrc${columnId ? `/${columnId}${subColumnId ? `/${subColumnId}` : ''}` : ''}`; + }, + }, + ], +};