diff --git a/lib/routes/noi/index.js b/lib/routes/noi/index.js index acbede035..72e12ba38 100644 --- a/lib/routes/noi/index.js +++ b/lib/routes/noi/index.js @@ -1,16 +1,17 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got('http://noi.cn/xw/index.shtml', { responseType: 'buffer' }); + const response = await got({ + method: 'get', + url: `http://www.noi.cn/xw/index.shtml`, + }); const $ = cheerio.load(response.data); - const postList = $('.news-item').get(); - postList.shift(); + const postList = $('.news-all').find('.news-item').get(); const result = await Promise.all( postList.map(async (item) => { const title = $(item).find('a').text(); - const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const link = 'http://www.noi.cn' + $(item).find('a').attr('href'); const guid = link; const pubDate = new Date($(item).find('small').text()).toUTCString(); @@ -22,25 +23,18 @@ module.exports = async (ctx) => { description: '', }; - const key = guid; - const cache = await ctx.cache.get(key); + const description_key = 'noi' + guid; + const description_value = await ctx.cache.get(description_key); - if (cache) { - const value = JSON.parse(cache); - - single.description = value.description; + if (description_value) { + single.description = description_value; } else { - 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, - }); + const temp = await got(link); + single.description = $(temp.data).find('.news-cont').html(); + ctx.cache.set(description_key, single.description); } - return Promise.resolve(single); }) ); - ctx.state.data = { title: '新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/xw/index.shtml', 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 index cb28e3220..4c275a6d8 100644 --- a/lib/routes/noi/province-news.js +++ b/lib/routes/noi/province-news.js @@ -1,18 +1,19 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got('http://noi.cn/gs/xw/index.shtml', { responseType: 'buffer' }); + const response = await got({ + method: 'get', + url: `http://www.noi.cn/gs/xw/index.shtml`, + }); const $ = cheerio.load(response.data); - const postList = $('.news-item').get(); - postList.shift(); + const postList = $('.news-all').find('.news-item').get(); const result = await Promise.all( postList.map(async (item) => { const title = $(item).find('a').text(); - const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const link = 'http://www.noi.cn' + $(item).find('a').attr('href'); const guid = link; - const pubDate = new Date($(item).find('small').text()).toUTCString(); + const pubDate = new Date($(item).find('small').text().slice(-19)).toUTCString(); const single = { title: title, @@ -22,25 +23,18 @@ module.exports = async (ctx) => { description: '', }; - const key = guid; - const cache = await ctx.cache.get(key); + const description_key = 'noi' + guid; + const description_value = await ctx.cache.get(description_key); - if (cache) { - const value = JSON.parse(cache); - - single.description = value.description; + if (description_value) { + single.description = description_value; } else { - 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, - }); + const temp = await got(link); + single.description = $(temp.data).find('.news-cont').html(); + ctx.cache.set(description_key, single.description); } - return Promise.resolve(single); }) ); - ctx.state.data = { title: '各省新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/xw/index.shtml', item: result }; + ctx.state.data = { title: '各省新闻 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/articles.html?type=99', item: result }; }; diff --git a/lib/routes/noi/winners-list.js b/lib/routes/noi/winners-list.js index d239fb8a8..f8ae67faf 100644 --- a/lib/routes/noi/winners-list.js +++ b/lib/routes/noi/winners-list.js @@ -1,16 +1,18 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const iconv = require('iconv-lite'); module.exports = async (ctx) => { - const response = await got('http://noi.cn/hjmd/mdgs/index.shtml', { responseType: 'buffer' }); + const response = await got({ + method: 'get', + url: `http://www.noi.cn/hjmd/mdgs/`, + }); + const $ = cheerio.load(response.data); - const postList = $('.news-item').get(); - postList.shift(); + const postList = $('.news-all').find('.news-item').get(); const result = await Promise.all( postList.map(async (item) => { const title = $(item).find('a').text(); - const link = 'http://noi.cn' + $(item).find('a').attr('href'); + const link = 'http://www.noi.cn' + $(item).find('a').attr('href'); const guid = link; const pubDate = new Date($(item).find('small').text()).toUTCString(); @@ -22,25 +24,18 @@ module.exports = async (ctx) => { description: '', }; - const key = guid; - const cache = await ctx.cache.get(key); + const description_key = 'noi' + guid; + const description_value = await ctx.cache.get(description_key); - if (cache) { - const value = JSON.parse(cache); - - single.description = value.description; + if (description_value) { + single.description = description_value; } else { - 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, - }); + const temp = await got(link); + single.description = $(temp.data).find('.news-cont').html(); + ctx.cache.set(description_key, single.description); } - return Promise.resolve(single); }) ); - ctx.state.data = { title: '获奖名单 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://noi.cn/hjmd/mdgs/index.shtml', item: result }; + ctx.state.data = { title: '获奖名单 - NOI 全国青少年信息学奥林匹克竞赛', link: 'http://www.noi.cn/articles.html?type=8', item: result }; };