diff --git a/routes/3dm/download.js b/routes/3dm/download.js index a7ee293c4..fefddfd06 100644 --- a/routes/3dm/download.js +++ b/routes/3dm/download.js @@ -14,29 +14,27 @@ module.exports = async (ctx) => { const data = response.data; const $ = cheerio.load(data); - const list = $('.ZQ_Left .Llis_4 li'); - const items = []; + const list = $('.ZQ_Left .Llis_4 li').get(); - for (let i = 0; i < list.length; i++) { - let item = $(list[i]); - const url = item.find('.bt').attr('href'); + const items = await Promise.all( + list.map(async (i) => { + const item = $(i); + const url = item.find('.bt').attr('href'); - const value = await ctx.cache.get(url); - if (value) { - item = JSON.parse(value); - } else { - item = { + const cache = await ctx.cache.get(url); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const single = { title: item.find('.bt').text(), pubDate: item.find('p').text(), link: url, guid: url, }; - - ctx.cache.set(url, JSON.stringify(item), 24 * 60 * 60); - } - - items.push(item); - } + ctx.cache.set(url, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); ctx.state.data = { title: $('title') diff --git a/routes/3dm/news.js b/routes/3dm/news.js index 4d583a002..155d89721 100644 --- a/routes/3dm/news.js +++ b/routes/3dm/news.js @@ -15,17 +15,17 @@ module.exports = async (ctx) => { const $ = cheerio.load(data); const list = $('.ZQ_Left .lis'); - const items = []; - for (let i = 0; i < list.length; i++) { - let item = $(list[i]); - const url = item.find('.bt').attr('href'); + const items = await Promise.all( + list.map(async (i) => { + const item = $(i); + const url = item.find('.bt').attr('href'); - const value = await ctx.cache.get(url); - if (value) { - item = JSON.parse(value); - } else { - item = { + const cache = await ctx.cache.get(url); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const single = { title: item.find('.bt').text(), pubDate: item.find('.time p').text(), description: item.find('.miaoshu').text(), @@ -34,10 +34,10 @@ module.exports = async (ctx) => { }; ctx.cache.set(url, JSON.stringify(item), 24 * 60 * 60); - } - items.push(item); - } + return Promise.resolve(single); + }) + ); ctx.state.data = { title: $('title') diff --git a/routes/3dm/news_center.js b/routes/3dm/news_center.js index d6e101075..416d5f704 100644 --- a/routes/3dm/news_center.js +++ b/routes/3dm/news_center.js @@ -10,44 +10,44 @@ module.exports = async (ctx) => { }); const data = res.data; const $ = cheerio.load(data); - const list = $('.news_list .list li'); - const out = []; + const list = $('.news_list .list li') + .slice(0, 10) + .get(); + const out = await Promise.all( + list.map(async (i) => { + const item = $(i); + const itemUrl = $(item) + .find('.selectarcpost') + .attr('href'); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const title = $(item) + .find('.bt') + .text(); + const content = $(item) + .find('p') + .text(); + const pageInfo = $(item) + .find('.time') + .text(); + const regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/; + const regRes = regex.exec(pageInfo); + const time = regRes === null ? new Date() : new Date(regRes[0]); + time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); + const single = { + title: title, + description: content, + pubDate: time.toUTCString(), + link: itemUrl, + guid: itemUrl, + }; - for (let i = 0; i < Math.min(list.length, 10); i++) { - const $ = cheerio.load(data); - const item = list[i]; - const itemUrl = $(item) - .find('.selectarcpost') - .attr('href'); - const cache = await ctx.cache.get(itemUrl); - if (cache) { - out.push(JSON.parse(cache)); - continue; - } - const title = $(item) - .find('.bt') - .text(); - const content = $(item) - .find('p') - .text(); - const pageInfo = $(item) - .find('.time') - .text(); - const regex = /\d{4}-\d{2}-\d{2} \d{2}:\d{2}/; - const regRes = regex.exec(pageInfo); - const time = regRes === null ? new Date() : new Date(regRes[0]); - time.setTime(time.getTime() + (sourceTimezoneOffset - time.getTimezoneOffset() / 60) * 60 * 60 * 1000); - const single = { - title: title, - description: content, - pubDate: time.toUTCString(), - link: itemUrl, - guid: itemUrl, - }; - out.push(single); - - ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); - } + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); ctx.state.data = { title: $('title') .text()