From 156f26388f82d6de8f1917eea7dcd84670fb5c78 Mon Sep 17 00:00:00 2001 From: WenryXu Date: Thu, 14 Nov 2019 11:31:40 +0800 Subject: [PATCH] feat: add noi.cn winners list and province news (#3419) --- docs/other.md | 12 +++++++ lib/router.js | 3 ++ lib/routes/noi/index.js | 2 +- lib/routes/noi/province-news.js | 46 ++++++++++++++++++++++++++ lib/routes/noi/rg-news.js | 58 +++++++++++++++++++++++++++++++++ lib/routes/noi/winners-list.js | 46 ++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 lib/routes/noi/province-news.js create mode 100644 lib/routes/noi/rg-news.js create mode 100644 lib/routes/noi/winners-list.js diff --git a/docs/other.md b/docs/other.md index 208ef9934..5fa994aca 100644 --- a/docs/other.md +++ b/docs/other.md @@ -71,6 +71,18 @@ pageClass: routes +### 获奖名单 + + + +### 各省新闻 + + + +### 报名新闻 + + + ## ONE · 一个 ### 图片文字问答 diff --git a/lib/router.js b/lib/router.js index e7da804bc..9476e67bd 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1921,6 +1921,9 @@ router.get('/pork-price', require('./routes/pork-price')); // NOI 全国青少年信息学奥林匹克竞赛 router.get('/noi', require('./routes/noi')); +router.get('/noi/winners-list', require('./routes/noi/winners-list')); +router.get('/noi/province-news', require('./routes/noi/province-news')); +router.get('/noi/rg-news', require('./routes/noi/rg-news')); // 中国工业化和信息部 router.get('/gov/miit/zcwj', require('./routes/gov/miit/zcwj')); diff --git a/lib/routes/noi/index.js b/lib/routes/noi/index.js index 52abb0a84..a083e1288 100644 --- a/lib/routes/noi/index.js +++ b/lib/routes/noi/index.js @@ -42,5 +42,5 @@ module.exports = async (ctx) => { return Promise.resolve(single); }) ); - ctx.state.data = { title: 'NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/', item: result }; + ctx.state.data = { title: '新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/articles.html?type=1', item: result }; }; diff --git a/lib/routes/noi/province-news.js b/lib/routes/noi/province-news.js new file mode 100644 index 000000000..ae1e572c3 --- /dev/null +++ b/lib/routes/noi/province-news.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const response = await got({ + method: 'get', + url: `http://www.noi.cn/GetNews.dt?cmd=list&place=99&psize=20`, + }); + const result = await Promise.all( + Object.keys(response.data).map(async (key) => { + const item = response.data[key]; + if (item.id === undefined) { + return true; + } + const title = (item.title + ' ' + item.title2).trim(); + const link = 'http://www.noi.cn/newsview.html?id=' + item.id + '&hash=' + item.hash; + const guid = item.id + '_' + item.mtime; + const pubDate = new Date(item.mtime).toUTCString(); + + const single = { + title: title, + link: link, + guid: guid, + pubDate: pubDate, + description: '', + }; + + const description_key = 'noi' + guid; + const description_value = await ctx.cache.get(description_key); + + if (description_value) { + single.description = description_value; + } else { + const url = 'http://www.noi.cn/GetNews.dt?cmd=read&newsid=' + item.id + '&hash=' + item.hash; + const temp = await got({ + method: 'get', + url: url, + }); + single.description = temp.data.cont; + ctx.cache.set(description_key, single.description); + } + + return Promise.resolve(single); + }) + ); + ctx.state.data = { title: '各省新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/articles.html?type=99', item: result }; +}; diff --git a/lib/routes/noi/rg-news.js b/lib/routes/noi/rg-news.js new file mode 100644 index 000000000..6604c6662 --- /dev/null +++ b/lib/routes/noi/rg-news.js @@ -0,0 +1,58 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); + +module.exports = async (ctx) => { + const response = await got('http://rg.noi.cn/admin/sysSet/newsIndex.php', { responseType: 'buffer' }); + response.data = iconv.decode(response.data, 'gbk'); + const $ = cheerio.load(response.data); + const postList = $('.list-group .list-group-item').get(); + postList.shift(); + const result = await Promise.all( + postList.map(async (item) => { + const title = $(item) + .find('a') + .text(); + const link = $(item) + .find('a') + .attr('href') + .replace('..', 'http://rg.noi.cn/admin'); + const guid = link; + const pubDate = new Date( + $(item) + .find('.badge') + .text() + ).toUTCString(); + + const single = { + title: title, + link: link, + guid: guid, + pubDate: pubDate, + description: '', + }; + + const key = guid; + const cache = await ctx.cache.get(key); + + if (cache) { + const value = JSON.parse(cache); + + single.description = value.description; + } else { + const temp = await got(link, { responseType: 'buffer' }); + temp.data = iconv.decode(temp.data, 'gbk'); + single.description = $(temp.data) + .find('.panel-body') + .html(); + + ctx.cache.set(key, { + description: single.description, + }); + } + + return Promise.resolve(single); + }) + ); + ctx.state.data = { title: '新闻 - NOI 网上报名系统', link: 'http://rg.noi.cn/', item: result }; +}; diff --git a/lib/routes/noi/winners-list.js b/lib/routes/noi/winners-list.js new file mode 100644 index 000000000..ef8b0e8f7 --- /dev/null +++ b/lib/routes/noi/winners-list.js @@ -0,0 +1,46 @@ +const got = require('@/utils/got'); + +module.exports = async (ctx) => { + const response = await got({ + method: 'get', + url: `http://www.noi.cn/GetNews.dt?cmd=list&place=8&psize=20`, + }); + const result = await Promise.all( + Object.keys(response.data).map(async (key) => { + const item = response.data[key]; + if (item.id === undefined) { + return true; + } + const title = (item.title + ' ' + item.title2).trim(); + const link = 'http://www.noi.cn/newsview.html?id=' + item.id + '&hash=' + item.hash; + const guid = item.id + '_' + item.mtime; + const pubDate = new Date(item.mtime).toUTCString(); + + const single = { + title: title, + link: link, + guid: guid, + pubDate: pubDate, + description: '', + }; + + const description_key = 'noi' + guid; + const description_value = await ctx.cache.get(description_key); + + if (description_value) { + single.description = description_value; + } else { + const url = 'http://www.noi.cn/GetNews.dt?cmd=read&newsid=' + item.id + '&hash=' + item.hash; + const temp = await got({ + method: 'get', + url: url, + }); + single.description = temp.data.cont; + ctx.cache.set(description_key, single.description); + } + + return Promise.resolve(single); + }) + ); + ctx.state.data = { title: '获奖名单 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/articles.html?type=8', item: result }; +};