diff --git a/docs/government.md b/docs/government.md index 07a048e7e..f0c06d075 100644 --- a/docs/government.md +++ b/docs/government.md @@ -131,6 +131,24 @@ pageClass: routes ## 中国政府 +### 数据 + + + +生猪分类可选项目: + +| 白条猪价格 | 生猪屠宰与活体交易 | 仔猪价格 | 生猪出场价与玉米价 | 生猪存栏信息和生猪疫情 | +| ---------- | ------------------ | -------- | ------------------- | ---------------------- | +| baitiaorou | huotijiaoyi | zizhu | chuchangjia_yumijia | cunlan_yiqing | + +价格分类可选项目: + +| 商品价格 | 农产品价格 | 油价 | +| -------- | ----------- | ------ | +| shangpin | nongchanpin | youjia | + + + ### 最新政策 diff --git a/lib/router.js b/lib/router.js index ac56e016e..bd96b12f0 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1208,6 +1208,7 @@ router.get('/gov/province/:name/:category', require('./routes/gov/province')); router.get('/gov/city/:name/:category', require('./routes/gov/city')); router.get('/gov/statecouncil/briefing', require('./routes/gov/statecouncil/briefing')); router.get('/gov/news/:uid', require('./routes/gov/news')); +router.get('/gov/shuju/:caty/:item', require('./routes/gov/shuju')); // 苏州 router.get('/gov/suzhou/news/:uid', require('./routes/gov/suzhou/news')); diff --git a/lib/routes/gov/shuju/index.js b/lib/routes/gov/shuju/index.js new file mode 100644 index 000000000..12aba965b --- /dev/null +++ b/lib/routes/gov/shuju/index.js @@ -0,0 +1,51 @@ +const url = require('url'); +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +const rootUrl = 'http://www.gov.cn'; + +module.exports = async (ctx) => { + const currentUrl = `${rootUrl}/shuju/${ctx.params.caty}/${ctx.params.item}.htm`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + const $ = cheerio.load(response.data); + const list = $('ul li') + .map((_, item) => { + item = $(item); + const a = item.find('a'); + return { + title: a.text(), + link: url.resolve(rootUrl, a.attr('href')), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + content('.font').remove(); + content('.pages_print').remove(); + + item.pubDate = new Date(content('div.pages-date').text().trim()).toUTCString(); + item.description = content('#UCAP-CONTENT').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: $('title').text(), + link: currentUrl, + item: items, + }; +};