feat: add noi.cn winners list and province news (#3419)

This commit is contained in:
WenryXu 2019-11-14 11:31:40 +08:00 committed by DIYgod
parent d88b3bc2c0
commit 156f26388f
6 changed files with 166 additions and 1 deletions

View File

@ -71,6 +71,18 @@ pageClass: routes
<Route author="WenryXu" example="/noi" path="/noi"/>
### 获奖名单
<Route author="WenryXu" example="/noi/winners-list" path="/noi/winners-list"/>
### 各省新闻
<Route author="WenryXu" example="/noi/province-news" path="/noi/province-news"/>
### 报名新闻
<Route author="WenryXu" example="/noi/rg-news" path="/noi/rg-news"/>
## ONE · 一个
### 图片文字问答

View File

@ -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'));

View File

@ -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 };
};

View File

@ -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 };
};

58
lib/routes/noi/rg-news.js Normal file
View File

@ -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 };
};

View File

@ -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 };
};