From c3e09482979d88e44f930e3f95c04fb927df5a1c Mon Sep 17 00:00:00 2001 From: zytomorrow Date: Sat, 27 Nov 2021 15:20:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20add=20=E5=BE=B7=E9=98=B3?= =?UTF-8?q?=E8=80=83=E8=AF=95=E4=B8=AD=E5=BF=83=20(#7901)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/government.md | 12 +++++++++ lib/router.js | 3 +++ lib/routes/dykszx/news.js | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 lib/routes/dykszx/news.js diff --git a/docs/government.md b/docs/government.md index 9f4d44ab22..698b631b9c 100644 --- a/docs/government.md +++ b/docs/government.md @@ -713,3 +713,15 @@ pageClass: routes ### 审查调查 + +## 德阳考试中心 + +### 考试新闻 + + + +| 新闻中心 | 公务员考试 | 事业单位 | (职)业资格、职称考试 | 其他 | +| :------: | :------: | :------: |:------: |:------: | +| all | gwy | sydw | zyzc | other | + + diff --git a/lib/router.js b/lib/router.js index ed878e94ca..deba3abdcf 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4224,4 +4224,7 @@ router.get('/odaily/activity', require('./routes/odaily/activity')); // Fashion Network router.get('/fashionnetwork/news/:sectors?/:categories?/:language?', require('./routes/fashionnetwork/news.js')); +// dykszx +router.get('/dykszx/news/:type?', require('./routes/dykszx/news')); + module.exports = router; diff --git a/lib/routes/dykszx/news.js b/lib/routes/dykszx/news.js new file mode 100644 index 0000000000..604a24050f --- /dev/null +++ b/lib/routes/dykszx/news.js @@ -0,0 +1,57 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); +const host = 'https://www.dykszx.com'; + +const getContent = (href, caches) => { + const newsPage = `${host}${href}`; + return caches.tryGet(newsPage, async () => { + const response = await got.get(newsPage); + const data = response.data; + const $ = cheerio.load(data); + const newsTime = $('body > div:nth-child(3) > div.page.w > div.shuxing.w') + .text() + .trim() + .match(/时间:(.*?)点击/g)[0]; + // 移除二维码 + $('.sjlook').remove(); + const content = $('#show-body').html(); + return { newsTime, content, newsPage }; + }); +}; + +const newsTypeObj = { + all: { selector: '#nrs > li > b', name: '新闻中心' }, + gwy: { selector: 'body > div:nth-child(3) > div:nth-child(8) > ul > li', name: '公务员考试' }, + sydw: { selector: 'body > div:nth-child(3) > div:nth-child(9) > ul > li', name: '事业单位考试' }, + zyzc: { selector: 'body > div:nth-child(3) > div:nth-child(10) > ul > li', name: '执(职)业资格、职称考试' }, + other: { selector: 'body > div:nth-child(3) > div:nth-child(11) > ul > li', name: '其他考试' }, +}; + +module.exports = async (ctx) => { + const newsType = ctx.params.type || 'all'; + const response = await got.get(host); + const data = response.data; + const $ = cheerio.load(data); + const newsList = $(newsTypeObj[newsType].selector).toArray(); + + const newsDetail = await Promise.all( + newsList.map(async (item) => { + const href = item.children[0].attribs.href; + const newsContent = await getContent(href, ctx.cache); + return { + title: item.children[0].children[0].data, + description: newsContent.content, + link: newsContent.newsPage, + pubDate: timezone(parseDate(newsContent.newsTime, '时间:YYYY-MM-DD HH:mm:ss'), +8), + }; + }) + ); + ctx.state.data = { + title: `德阳人事考试网 - ${newsTypeObj[newsType].name}`, + link: host, + description: '德阳人事考试网 考试新闻发布', + item: newsDetail, + }; +};