diff --git a/docs/government.md b/docs/government.md index a2b1132b62..ceef0b77bf 100644 --- a/docs/government.md +++ b/docs/government.md @@ -184,6 +184,22 @@ pageClass: routes +## 国家统计局 + +### 统计数据 > 最新发布 + + + +::: tip 提示 + +路径处填写对应页面 URL 中 `http://www.stats.gov.cn/` 后的字段。下面是一个例子。 + +若订阅 [统计数据 > 统计标准](http://www.stats.gov.cn/tjsj/tjbz/) 则将对应页面 URL 中 `http://www.stats.gov.cn/` 后的字段 `tjsj/tjbz` 作为路径填入。此时路由为 [`/gov/stats/tjsj/tjbz`](https://rsshub.app/gov/stats/tjsj/tjbz) + +::: + + + ## 国家新闻出版广电总局(弃用) ### 游戏审批结果 diff --git a/lib/v2/gov/maintainer.js b/lib/v2/gov/maintainer.js index 55d9cfc50a..a908eca04c 100644 --- a/lib/v2/gov/maintainer.js +++ b/lib/v2/gov/maintainer.js @@ -33,6 +33,7 @@ module.exports = { '/pbc/gzlw': ['Fatpandac'], '/pbc/tradeAnnouncement': ['nczitzk'], '/pbc/zcyj': ['Fatpandac'], + '/stats/:path+': ['bigfei'], // province '/anhui/kjt/:path?': ['nczitzk'], '/beijing/jw/tzgg': ['nczitzk'], diff --git a/lib/v2/gov/radar.js b/lib/v2/gov/radar.js index b1ba182713..5e46fccc05 100644 --- a/lib/v2/gov/radar.js +++ b/lib/v2/gov/radar.js @@ -688,6 +688,17 @@ module.exports = { }, ], }, + 'stats.gov.cn': { + _name: '国家统计局', + www: [ + { + title: '统计数据 > 最新发布', + docs: 'https://docs.rsshub.app/government.html#guo-jia-tong-ji-ju-tong-ji-shu-ju-zui-xin-fa-bu', + source: ['/*'], + target: (params, url) => `/gov/stats/${new URL(url).href.match(/stats\.gov\.cn\/(.*)/)[1]}`, + }, + ], + }, 'sz.gov.cn': { _name: '深圳政府在线移动门户', hrss: [ diff --git a/lib/v2/gov/router.js b/lib/v2/gov/router.js index dae07bdc8c..9ee21fec16 100644 --- a/lib/v2/gov/router.js +++ b/lib/v2/gov/router.js @@ -25,6 +25,7 @@ module.exports = function (router) { router.get('/pbc/gzlw', require('./pbc/gzlw')); router.get('/pbc/tradeAnnouncement', require('./pbc/tradeAnnouncement')); router.get('/pbc/zcyj', require('./pbc/zcyj')); + router.get(/stats(\/[\w/-]+)?/, require('./stats')); // province router.get(/anhui\/kjt\/([\w\d/-]+)?/, require('./anhui/kjt')); router.get('/beijing/jw/tzgg', require('./beijing/jw/tzgg')); diff --git a/lib/v2/gov/stats/index.js b/lib/v2/gov/stats/index.js new file mode 100644 index 0000000000..a9374bfcd0 --- /dev/null +++ b/lib/v2/gov/stats/index.js @@ -0,0 +1,19 @@ +const { parseContList, parseXilan } = require('./utils'); + +module.exports = async (ctx) => { + const rootUrl = 'http://www.stats.gov.cn'; + const defaultPath = '/tjsj/zxfb/'; + + let pathname = ctx.path.replace(/(^\/stats|\/$)/g, ''); + pathname = pathname === '' ? defaultPath : pathname.endsWith('/') ? pathname : pathname + '/'; + const currentUrl = `${rootUrl}${pathname}`; + + const { list, title } = await parseContList(currentUrl, 'ul.center_list_contlist li a:not([id])', ctx); + const items = await Promise.all(list.map((item) => parseXilan(item, ctx))); + + ctx.state.data = { + title, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/gov/stats/templates/attachments.art b/lib/v2/gov/stats/templates/attachments.art new file mode 100644 index 0000000000..65c3e676d0 --- /dev/null +++ b/lib/v2/gov/stats/templates/attachments.art @@ -0,0 +1,5 @@ +{{ each attachments }} +

+ {{ $value.text }} +

+{{ /each}} diff --git a/lib/v2/gov/stats/utils.js b/lib/v2/gov/stats/utils.js new file mode 100644 index 0000000000..a838d0f050 --- /dev/null +++ b/lib/v2/gov/stats/utils.js @@ -0,0 +1,64 @@ +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const got = require('@/utils/got'); +const timezone = require('@/utils/timezone'); +const { art } = require('@/utils/render'); +const path = require('path'); + +const parseContList = async (url, selector, ctx) => { + const response = await got(url); + const $ = cheerio.load(response.data); + const list = $(selector) + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 12) + .toArray() + .map((item) => { + item = $(item); + return { + title: item.find('.cont_tit03').text(), + link: new URL(item.attr('href'), url).href, + pubDate: parseDate(item.find('.cont_tit02').text(), 'YYYY-MM-DD'), + }; + }) + .filter((item) => item.title); // exclude the empty title + const title = $('#PL_DAOHANG') + .text() + .replace(/(.+)首页/, '国家统计局'); + return { list, title }; +}; + +const parseXilan = async (item, ctx) => + await ctx.cache.tryGet(item.link, async () => { + const response = await got(item.link); + const $ = cheerio.load(response.data); + const title = $('.xilan_titf').text(); + item.author = title.match(/来源:(.*)发布时间/)?.[1].trim() ?? '国家统计局'; + item.pubDate = timezone(parseDate(title.match(/发布时间:(.*)/)?.[1].trim() ?? item.pubDate), +8); + item.description = $('.xilan_con').html(); + + const attachmentTitle = $('.wenzhang_tit').filter(function () { + return $(this).text().trim() === '相关附件'; + }); + if (attachmentTitle.length > 0) { + const attachments = attachmentTitle + .first() + .next('.wenzhang_list') + .find('a') + .toArray() + .map((attachment) => { + attachment = $(attachment); + return { + href: new URL(attachment.attr('href'), item.link).href, + text: attachment.text().trim(), + }; + }); + item.description += art(path.join(__dirname, 'templates/attachments.art'), { + attachments, + }); + } + return item; + }); + +module.exports = { + parseXilan, + parseContList, +};