fix: NOI 全国青少年信息学奥林匹克竞赛新闻 & 获奖名单 & 各省新闻 (#6864)

This commit is contained in:
WenryXu 2021-02-14 02:17:52 +08:00 committed by GitHub
parent 17aba634f8
commit 60d62c0a27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 61 deletions

View File

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

View File

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

View File

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