From 5ce2cf8a27da75210e13e87c2439d8a11dbf63d9 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 1 Aug 2020 23:42:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20=E4=B8=AD=E5=9B=BD=E6=94=BF?= =?UTF-8?q?=E5=BA=9C=E7=BD=91=E7=94=9F=E7=8C=AA=E4=BF=A1=E6=81=AF=20&=20?= =?UTF-8?q?=E5=95=86=E5=93=81=E4=BB=B7=E6=A0=BC=20(#5305)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/government.md | 18 +++++++++++++ lib/router.js | 1 + lib/routes/gov/shuju/index.js | 51 +++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/routes/gov/shuju/index.js diff --git a/docs/government.md b/docs/government.md index 07a048e7ea..f0c06d075d 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 ac56e016e0..bd96b12f0c 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 0000000000..12aba965bb --- /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, + }; +};