进一步适配 BBC 不同栏目 (#857)

This commit is contained in:
Henry Wang 2018-10-11 04:06:19 +01:00 committed by DIYgod
parent 5ec3edde05
commit ff58256bec
2 changed files with 59 additions and 6 deletions

View File

@ -43,7 +43,13 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response.data);
const description = utils.ProcessFeed($, item.link).html();
let description;
if (response.request.path.startsWith('/news/av')) {
description = utils.ProcessAVFeed($, item.link);
} else {
description = utils.ProcessFeed($, item.link);
}
const single = {
title: item.title,

View File

@ -6,9 +6,9 @@ const ProcessImage = ($, e, c) => {
// handle cover image and lazy-loading images
if (img[0].attribs.src) {
message = `<figure><img alt='${img[0].attribs.alt}' src='${img[0].attribs.src.replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
message = `<figure><img alt='${img[0].attribs.alt ? img[0].attribs.alt : ''}' src='${img[0].attribs.src.replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
} else {
message = `<figure><img alt='${img[0].attribs['data-alt']}' src='${img[0].attribs['data-src'].replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
message = `<figure><img alt='${img[0].attribs['data-alt'] ? img[0].attribs['data-alt'] : ''}' src='${img[0].attribs['data-src'].replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
}
// add image caption
@ -32,6 +32,45 @@ const ProcessImage = ($, e, c) => {
$(message).insertAfter(e);
}
};
const ProcessVideo = ($, e, c) => {
const video = $(e).find(c);
video.each((i, f) => {
const cover = $(f).children('img.player-with-placeholder__image');
if (cover.length > 0) {
$(cover[0]).insertBefore(e);
}
const link = $(f).find('figure.js-media-player-unprocessed');
if (link.length > 0) {
const url = JSON.parse(link[0].attribs['data-playable']).settings.externalEmbedUrl;
const message = `<br><a href='${url}'>使用浏览器播视频/view video in browser</a>`;
$(message).insertAfter(e);
}
});
};
const ProcessAVFeed = ($) => {
const content = $('div.vxp-media__summary');
const video = $('div.vxp-media__player');
video.each((i, e) => {
const cover = $(e).children('img.vxp-media__placeholder-image');
if (cover.length > 0) {
$(cover[0]).insertBefore(content[0].firstChild);
}
const link = $(e).find('figure.js-media-player-unprocessed');
if (link.length > 0) {
const url = JSON.parse(link[0].attribs['data-playable']).settings.externalEmbedUrl;
const message = `<br><a href='${url}'>使用浏览器播视频/view video in browser</a>`;
$(message).insertAfter(content[0].firstChild);
}
});
return content.html();
};
const ProcessFeed = ($, link) => {
// by default treat it as a hybrid news with video and story-body__inner
let content = $('div.story-body__inner');
@ -41,6 +80,11 @@ const ProcessFeed = ($, link) => {
content = $('div.story-body');
}
// remove useless DOMs
content.find('div.bbccom_slot, .off-screen, .embed-report-link').each((i, e) => {
$(e).remove();
});
if ($('#comp-media-player').length > 0) {
// there is a cover video
@ -56,19 +100,22 @@ const ProcessFeed = ($, link) => {
$(message).insertAfter(content[0].lastChild);
}
// resize all images
// resize all images and videos
content.find('figure').each((i, e) => {
ProcessImage($, e, 'img');
ProcessImage($, e, '.image-and-copyright-container > img');
// handle lazy-loading images
ProcessImage($, e, '.js-delayed-image-load');
ProcessVideo($, e, 'figure.media-with-caption > div.player-with-placeholder', link);
$(e).remove();
});
return content;
return content.html();
};
module.exports = {
ProcessFeed,
ProcessAVFeed,
};