diff --git a/lib/routes/blogs/wangyin.js b/lib/routes/blogs/wangyin.js index d747d5cebf..60fd4ac90e 100644 --- a/lib/routes/blogs/wangyin.js +++ b/lib/routes/blogs/wangyin.js @@ -3,27 +3,45 @@ const cheerio = require('cheerio'); module.exports = async (ctx) => { const url = 'https://www.yinwang.org'; - const response = await got({ - method: 'get', - url, - }); + const response = await got({ method: 'get', url }); const $ = cheerio.load(response.data); - const resultItem = $("li[class='list-group-item title']") - .map((index, elem) => { - elem = $(elem); + + const list = $("li[class='list-group-item title']") + .map((i, e) => { + const element = $(e); + const title = element.find('a').text(); + const link = url + element.find('a').attr('href'); return { - title: elem.find('a').text(), - description: elem.find('a').text(), - link: url + elem.find('a').attr('href'), + title: title, + description: '', + link: link, author: '王垠', }; }) .get(); + const result = await Promise.all( + list.map(async (item) => { + const link = item.link; + + const cache = await ctx.cache.get(link); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const itemReponse = await got.get(link); + const itemElement = cheerio.load(itemReponse.data); + item.description = itemElement('.inner').html(); + + ctx.cache.set(link, JSON.stringify(item)); + return Promise.resolve(item); + }) + ); + ctx.state.data = { - title: '王垠的博客-当然我在扯淡', + title: '王垠的博客 - 当然我在扯淡', link: url, - item: resultItem, + item: result, }; };