From 43dd1ebcf74b4a2c071cfbbbc0923f5641a64520 Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 21 Feb 2019 07:24:26 +0000 Subject: [PATCH] feat: handle images and videos (#1594) --- lib/routes/idownloadblog/index.js | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/routes/idownloadblog/index.js b/lib/routes/idownloadblog/index.js index 9b4b05343..8fe33bcc6 100644 --- a/lib/routes/idownloadblog/index.js +++ b/lib/routes/idownloadblog/index.js @@ -5,6 +5,28 @@ const parser = new Parser(); module.exports = async (ctx) => { const feed = await parser.parseURL('https://www.idownloadblog.com/feed'); + + const ProcessFeed = (data) => { + const $ = cheerio.load(data); + + const content = $('div.post-content'); + + content.find('figure.wp-block-image ,figure.wp-block-embed-youtube, .wp-block-image > figure').each((i, e) => { + let media = $(e).find('noscript')[0].children[0].data; + + if ($(e).find('figcaption').length > 0) { + const caption = $($(e).find('figcaption')[0]).html(); + + media = `
${caption}
`; + } + + $(media).insertAfter(e); + $(e).remove(); + }); + + return content.html(); + }; + const items = await Promise.all( feed.items.map(async (item) => { const response = await axios({ @@ -12,8 +34,7 @@ module.exports = async (ctx) => { url: item.link, }); - const $ = cheerio.load(response.data); - const description = $('div.post-content').html(); + const description = ProcessFeed(response.data); const single = { title: item.title,