From afcc8ffef456c889b230ac612da60704ef249cb3 Mon Sep 17 00:00:00 2001 From: Hans362 Date: Sun, 13 Dec 2020 00:27:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=80=82=E9=85=8D=20NOI=20=E5=85=A8?= =?UTF-8?q?=E5=9B=BD=E9=9D=92=E5=B0=91=E5=B9=B4=E4=BF=A1=E6=81=AF=E5=AD=A6?= =?UTF-8?q?=E5=A5=A5=E6=9E=97=E5=8C=B9=E5=85=8B=E7=AB=9E=E8=B5=9B=E6=96=B0?= =?UTF-8?q?=E7=89=88=E7=BD=91=E7=AB=99=20(#6426)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/noi/index.js | 48 ++++++++++++++++----------------- lib/routes/noi/province-news.js | 48 ++++++++++++++++----------------- lib/routes/noi/winners-list.js | 48 ++++++++++++++++----------------- 3 files changed, 72 insertions(+), 72 deletions(-) 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 }; };