diff --git a/lib/routes/indienova/article.js b/lib/routes/indienova/article.js index 190f11a2de..fdc6f1e036 100644 --- a/lib/routes/indienova/article.js +++ b/lib/routes/indienova/article.js @@ -1,5 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const url = require('url'); module.exports = async (ctx) => { const response = await got({ @@ -7,30 +8,48 @@ module.exports = async (ctx) => { url: 'https://www.indienova.com/indie-game-news/', }); - const data = response.data; - const $ = cheerio.load(data); - const list = $('.article-panel'); + const host = 'https://indienova.com'; + + const $ = cheerio.load(response.data); + const list = $('.article-panel h4').get(); + + const items = await Promise.all( + list.map(async (item) => { + const $$ = cheerio.load(item); + const itemUrl = url.resolve(host, $$('a').attr('href')); + const title = $$('a').text(); + + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const resp = await got.get(itemUrl); + + const $$$ = cheerio.load(resp.data); + + $('img').each((index, elem) => { + const $elem = $(elem); + $elem.attr('referrerpolicy', 'no-referrer'); + }); + const description = $$$('.indienova-single-post').html(); + + const single = { + title: title, + description: description, + pubDate: '', + link: itemUrl, + author: '', + }; + ctx.cache.set(itemUrl, JSON.stringify(single)); + return Promise.resolve(single); + }) + ); ctx.state.data = { title: 'INDIENOVA', link: 'https://www.indienova.com/indie-game-news/', description: '独立游戏资讯 | indienova 独立游戏', - item: - list && - list - .map((index, item) => { - item = $(item); - return { - title: item.find('h4').text(), - description: item.find('p').text() + '', - link: - 'https://www.indienova.com' + - item - .find('.article-image') - .find('a') - .attr('href'), - }; - }) - .get(), + item: items, }; };