diff --git a/lib/routes/noi/index.js b/lib/routes/noi/index.js index a083e1288b..acbede035f 100644 --- a/lib/routes/noi/index.js +++ b/lib/routes/noi/index.js @@ -1,20 +1,18 @@ const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: `http://www.noi.cn/GetNews.dt?cmd=list&place=1&psize=20`, - }); + const response = await got('http://noi.cn/xw/index.shtml', { responseType: 'buffer' }); + const $ = cheerio.load(response.data); + const postList = $('.news-item').get(); + postList.shift(); 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(); + postList.map(async (item) => { + const title = $(item).find('a').text(); + const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const guid = link; + const pubDate = new Date($(item).find('small').text()).toUTCString(); const single = { title: title, @@ -24,23 +22,25 @@ module.exports = async (ctx) => { description: '', }; - const description_key = 'noi' + guid; - const description_value = await ctx.cache.get(description_key); + const key = guid; + const cache = await ctx.cache.get(key); - if (description_value) { - single.description = description_value; + if (cache) { + const value = JSON.parse(cache); + + single.description = value.description; } 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, + const temp = await got(link, { responseType: 'buffer' }); + temp.data = iconv.decode(temp.data, 'utf-8'); + single.description = $(temp.data).find('.right-con').find('.panel-body').html(); + + ctx.cache.set(key, { + description: single.description, }); - 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=1', item: result }; + ctx.state.data = { title: '新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/xw/index.shtml', item: result }; }; diff --git a/lib/routes/noi/province-news.js b/lib/routes/noi/province-news.js index ae1e572c3e..cb28e32205 100644 --- a/lib/routes/noi/province-news.js +++ b/lib/routes/noi/province-news.js @@ -1,20 +1,18 @@ const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: `http://www.noi.cn/GetNews.dt?cmd=list&place=99&psize=20`, - }); + const response = await got('http://noi.cn/gs/xw/index.shtml', { responseType: 'buffer' }); + const $ = cheerio.load(response.data); + const postList = $('.news-item').get(); + postList.shift(); 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(); + postList.map(async (item) => { + const title = $(item).find('a').text(); + const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const guid = link; + const pubDate = new Date($(item).find('small').text()).toUTCString(); const single = { title: title, @@ -24,23 +22,25 @@ module.exports = async (ctx) => { description: '', }; - const description_key = 'noi' + guid; - const description_value = await ctx.cache.get(description_key); + const key = guid; + const cache = await ctx.cache.get(key); - if (description_value) { - single.description = description_value; + if (cache) { + const value = JSON.parse(cache); + + single.description = value.description; } 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, + const temp = await got(link, { responseType: 'buffer' }); + temp.data = iconv.decode(temp.data, 'utf-8'); + single.description = $(temp.data).find('.right-con').find('.panel-body').html(); + + ctx.cache.set(key, { + description: single.description, }); - 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 }; + ctx.state.data = { title: '各省新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/xw/index.shtml', item: result }; }; diff --git a/lib/routes/noi/winners-list.js b/lib/routes/noi/winners-list.js index ef8b0e8f7a..d239fb8a8d 100644 --- a/lib/routes/noi/winners-list.js +++ b/lib/routes/noi/winners-list.js @@ -1,20 +1,18 @@ const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got({ - method: 'get', - url: `http://www.noi.cn/GetNews.dt?cmd=list&place=8&psize=20`, - }); + const response = await got('http://noi.cn/hjmd/mdgs/index.shtml', { responseType: 'buffer' }); + const $ = cheerio.load(response.data); + const postList = $('.news-item').get(); + postList.shift(); 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(); + postList.map(async (item) => { + const title = $(item).find('a').text(); + const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const guid = link; + const pubDate = new Date($(item).find('small').text()).toUTCString(); const single = { title: title, @@ -24,23 +22,25 @@ module.exports = async (ctx) => { description: '', }; - const description_key = 'noi' + guid; - const description_value = await ctx.cache.get(description_key); + const key = guid; + const cache = await ctx.cache.get(key); - if (description_value) { - single.description = description_value; + if (cache) { + const value = JSON.parse(cache); + + single.description = value.description; } 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, + const temp = await got(link, { responseType: 'buffer' }); + temp.data = iconv.decode(temp.data, 'utf-8'); + single.description = $(temp.data).find('.right-con').find('.panel-body').html(); + + ctx.cache.set(key, { + description: single.description, }); - 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 }; + ctx.state.data = { title: '获奖名单 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/hjmd/mdgs/index.shtml', item: result }; };