From ec8694002c5df5ad359a19746d0e5ed8cd8b1107 Mon Sep 17 00:00:00 2001 From: SunShinenny Date: Thu, 19 Sep 2019 14:45:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=B2=B3=E5=8C=97?= =?UTF-8?q?=E7=9C=81=E9=80=80=E5=BD=B9=E5=A3=AB=E5=85=B5=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E5=B1=80=E5=85=AC=E5=BC=80=E4=BF=A1=E6=81=AF=20(#3089)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/government.md | 10 +++++ lib/router.js | 3 ++ lib/routes/gov/veterans/hebei.js | 73 ++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 lib/routes/gov/veterans/hebei.js diff --git a/docs/government.md b/docs/government.md index ff6383b06..75af423ae 100644 --- a/docs/government.md +++ b/docs/government.md @@ -51,6 +51,16 @@ pageClass: routes +### 河北省退役军人事务厅 + + + +| 省内信息 | 厅内信息 | 市县信息 | +| :------: | :------: | :------: | +| ywgz | tnxx | sxxx | + + + ### 江苏省人民政府 diff --git a/lib/router.js b/lib/router.js index c91417bc5..514c1b53d 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1419,6 +1419,9 @@ router.get('/gov/veterans/bnxx', require('./routes/gov/veterans/bnxx')); router.get('/gov/veterans/zcjd', require('./routes/gov/veterans/zcjd')); router.get('/gov/veterans/index', require('./routes/gov/veterans/index')); +// 河北省退伍士兵信息 +router.get('/gov/veterans/hebei/:type', require('./routes/gov/veterans/hebei')); + // Dilbert Comic Strip router.get('/dilbert/strip', require('./routes/dilbert/strip')); diff --git a/lib/routes/gov/veterans/hebei.js b/lib/routes/gov/veterans/hebei.js new file mode 100644 index 000000000..da4f6e35f --- /dev/null +++ b/lib/routes/gov/veterans/hebei.js @@ -0,0 +1,73 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +async function load(link, ctx) { + return await ctx.cache.tryGet(link, async () => { + // 开始加载页面 + const response = await got.get(link); + const $ = cheerio.load(response.data); + // 获取标题 + const title = $('body > div.container.mt40 > div > div > div.m-lg.text-center > div.p-sm').text(); + // 获取正文内容 + const introduce = $('body > div.container.mt40 > div > div > div.m-lg.info-style-content').html(); + // eslint-disable-next-line no-useless-escape + // 获取标题中的推送时间和作者等信息 + const temp = $('body > div.container.mt40 > div > div > div.m-lg.text-center > div.m-b-sm') + .text() + .replace(/[\r\n]/g, ''); + // 获取标题中的时间 + const dateTime = temp.substring(temp.indexOf('发布时间') + 5, temp.indexOf('信息来源')).trim() + ':00'; + const pubDate = new Date(new Date(dateTime).getTime()).toUTCString(); + // 获取标题中的作者 + const author = temp.substring(temp.indexOf('信息来源') + 5, temp.indexOf('阅读次数')).trim(); + + return { + title: title, + description: introduce, + link: link, + pubDate: pubDate, + author: author, + }; + }); +} + +module.exports = async (ctx) => { + const type = ctx.params.type; + const checkType = { + sxxx: '市县信息', + tnxx: '厅内信息', + ywgz: '省内信息', + }; + const host = `http://tyjrswt.hebei.gov.cn/gk/${type}/`; + const response = await got({ + method: 'get', + url: host, + }); + const data = response.data; + + const $ = cheerio.load(data); + const list = $(`#${type}_list > li > a`).get(); + + const process = await Promise.all( + list.map(async (item) => { + let itemUrl = $(item).attr('href'); + if (itemUrl.indexOf('./') === 0) { + itemUrl = 'http://tyjrswt.hebei.gov.cn' + itemUrl.substring(2); + } else { + itemUrl = 'http://tyjrswt.hebei.gov.cn' + itemUrl; + } + const single = { + title: $(item).text(), + link: itemUrl, + guid: itemUrl, + }; + const other = await load(String(itemUrl), ctx); + return Promise.resolve(Object.assign({}, single, other)); + }) + ); + ctx.state.data = { + title: `河北省退伍军人事务厅 - ${checkType[type]}`, + link: host, + description: `河北省退伍军人事务厅 - ${checkType[type]} 更新提示`, + item: process, + }; +};