fix: gcores image is broken (#5633)

fix: image undefined
This commit is contained in:
simoin 2020-09-21 03:58:33 +08:00 committed by GitHub
parent f41c4c15e0
commit b7b527df2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 28 deletions

View File

@ -9,45 +9,70 @@ module.exports = async (ctx) => {
url: url,
});
const data = res.data;
let $ = cheerio.load(data);
const list = $('.original.am_card.original-normal');
const count = [];
const $ = cheerio.load(data);
const feedTitle = $('title').text();
for (let i = 0; i < Math.min(list.length, 10); i++) {
count.push(i);
}
const list = $('.original.am_card.original-normal')
.map(function () {
const item = {
url: $(this).find('a.am_card_content.original_content').attr('href'),
title: $(this).find('h3.am_card_title').text(),
};
return item;
})
.get();
const out = await Promise.all(
count.map(async (i) => {
const item = list[i];
let itemUrl = $(item).find('a.am_card_content.original_content').attr('href');
itemUrl = 'https://www.gcores.com' + itemUrl;
const cache = await ctx.cache.get(itemUrl);
list.map(async (item) => {
const articleUrl = `https://www.gcores.com${item.url}`;
const cache = await ctx.cache.get(articleUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const title = $(item).find('h3.am_card_title').text();
let itemRes;
try {
itemRes = await got({
method: 'get',
url: itemUrl,
});
} catch (e) {
return Promise.resolve();
}
const itemPage = itemRes.data;
$ = cheerio.load(itemPage);
const itemRes = await got({
method: 'get',
url: articleUrl,
});
const itemPage = itemRes.data;
const $ = cheerio.load(itemPage);
let articleData = await got(`https://www.gcores.com/gapi/v1${item.url}?include=media`);
articleData = articleData.data.data;
let cover;
if (articleData.attributes.cover) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.cover}" />`;
} else if (articleData.attributes.thumb) {
cover = `<img src="https://image.gcores.com/${articleData.attributes.thumb}" />`;
} else {
cover = '';
}
// replace figure with img
const articleContent = JSON.parse(articleData.attributes.content);
const imgs = Object.values(articleContent.entityMap)
.filter((e) => e.type === 'IMAGE')
.map((e) => e.data.path);
$('figure').each((i, elem) => {
if (imgs[i]) {
$(elem).replaceWith($(`<img src="https://image.gcores.com/${imgs[i]}" />`));
}
});
// remove editor toolbar img
$('.md-editor-toolbar').replaceWith('');
// remove hidden tip block
$('.story_hidden').replaceWith('');
const cover = $('img.newsPage_cover');
const content = $('.story.story-show').html();
const single = {
title: title,
title: item.title,
description: cover + content,
link: itemUrl,
guid: itemUrl,
link: articleUrl,
guid: articleUrl,
};
ctx.cache.set(itemUrl, JSON.stringify(single));
ctx.cache.set(articleUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);