feat: indienova 全文 (#2417)

时间和作者不好搞 留给有缘人去弄吧
This commit is contained in:
Cloud
2019-06-16 20:30:23 +08:00
committed by DIYgod
parent 542135c6b1
commit 7e0ca8aaee
+39 -20
View File
@@ -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() + '<img referrerpolicy="no-referrer" src="' + item.find('img').attr('src') + '">',
link:
'https://www.indienova.com' +
item
.find('.article-image')
.find('a')
.attr('href'),
};
})
.get(),
item: items,
};
};