From 7e0ca8aaee7113a73de4632b15ca80f457112470 Mon Sep 17 00:00:00 2001 From: Cloud Date: Sun, 16 Jun 2019 20:30:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20indienova=20=E5=85=A8=E6=96=87=20(#2417?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 时间和作者不好搞 留给有缘人去弄吧 --- lib/routes/indienova/article.js | 59 ++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 20 deletions(-) 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, }; };