From b7b527df2b76d9019725ad94310f3caf6ad02f3e Mon Sep 17 00:00:00 2001 From: simoin Date: Mon, 21 Sep 2020 03:58:33 +0800 Subject: [PATCH] fix: gcores image is broken (#5633) fix: image undefined --- lib/routes/gcores/category.js | 81 +++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/lib/routes/gcores/category.js b/lib/routes/gcores/category.js index de144f3d1..ee0e0169f 100644 --- a/lib/routes/gcores/category.js +++ b/lib/routes/gcores/category.js @@ -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 = ``; + } else if (articleData.attributes.thumb) { + cover = ``; + } 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($(``)); + } + }); + + // 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); }) );