From 60d62c0a275c308ea45451b60fd61322786c6daf Mon Sep 17 00:00:00 2001 From: WenryXu Date: Sun, 14 Feb 2021 02:17:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20NOI=20=E5=85=A8=E5=9B=BD=E9=9D=92?= =?UTF-8?q?=E5=B0=91=E5=B9=B4=E4=BF=A1=E6=81=AF=E5=AD=A6=E5=A5=A5=E6=9E=97?= =?UTF-8?q?=E5=8C=B9=E5=85=8B=E7=AB=9E=E8=B5=9B=E6=96=B0=E9=97=BB=20&=20?= =?UTF-8?q?=E8=8E=B7=E5=A5=96=E5=90=8D=E5=8D=95=20&=20=E5=90=84=E7=9C=81?= =?UTF-8?q?=E6=96=B0=E9=97=BB=20(#6864)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/noi/index.js | 34 +++++++++++++------------------ lib/routes/noi/province-news.js | 36 ++++++++++++++------------------- lib/routes/noi/winners-list.js | 35 ++++++++++++++------------------ 3 files changed, 44 insertions(+), 61 deletions(-) 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 }; };