This commit is contained in:
DIYgod
2021-01-25 03:40:44 +08:00
parent 6c0349152d
commit 3f9154fb72
4 changed files with 27 additions and 116 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ Site
### BBC
<RouteEn author="HenryQW" example="/bbc/world-asia" path="/bbc/:channel?" :paramsDesc="['channel, default to `top stories`']">
<RouteEn author="HenryQW DIYgod" example="/bbc/world-asia" path="/bbc/:channel?" :paramsDesc="['channel, default to `top stories`']">
Provides a better reading experience (full text articles) over the official ones.
+1 -1
View File
@@ -34,7 +34,7 @@ pageClass: routes
### BBC 英文
<Route author="HenryQW" example="/bbc/world-asia" path="/bbc/:channel" :paramsDesc="['频道,缺省为热门']">
<Route author="HenryQW DIYgod" example="/bbc/world-asia" path="/bbc/:channel" :paramsDesc="['频道,缺省为热门']">
通过提取文章全文,以提供比官方源更佳的阅读体验。
+12 -4
View File
@@ -47,7 +47,7 @@ module.exports = async (ctx) => {
}
const items = await Promise.all(
feed.items.splice(0, 10).map(async (item) => {
feed.items.splice(0, 30).map(async (item) => {
const response = await got({
method: 'get',
url: item.link,
@@ -58,13 +58,21 @@ module.exports = async (ctx) => {
let description;
if (response.request.options.url.pathname.startsWith('/news/av')) {
description = utils.ProcessAVFeed($, item.link);
description = item.content;
} else {
description = utils.ProcessFeed($, item.link);
description = utils.ProcessFeed($);
}
let section = 'sport';
const urlSplit = item.link.split('/');
const sectionSplit = urlSplit[urlSplit.length - 1].split('-');
if (sectionSplit.length > 1) {
section = sectionSplit[0];
}
section = section[0].toUpperCase() + section.slice(1);
const single = {
title: item.title,
title: `[${section}] ${item.title}`,
description,
pubDate: item.pubDate,
link: item.link,
+13 -110
View File
@@ -1,83 +1,6 @@
const cheerio = require('cheerio');
const ProcessImage = ($, e, c, noscript = false) => {
const img = $(e).find(c);
if (img.length > 0) {
let message;
// special handling for img wrapped in <noscript>
if (noscript) {
// eslint-disable-next-line no-unused-vars
const i = cheerio.load(img[0].children[0].data)('img')[0];
message = `<figure><img alt='${i.attribs.alt}' src='${i.attribs.src}'><br><figcaption>`;
} else {
if (img[0].attribs.src) {
// handle cover image and lazy-loading images
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'] ? img[0].attribs['data-alt'] : ''}' src='${img[0].attribs['data-src'].replace(/(?<=\/news\/).*?(?=\/cpsprodpb)/, '600')}'><br><figcaption>`;
}
// add image caption
const figcaption = $(e).find('.media-caption__text');
if (figcaption.length > 0) {
message += `${$(figcaption[0]).text().trim()}`;
}
// add image copyright
const copyright = $(e).find('.story-image-copyright');
if (copyright.length > 0) {
message += ` ©${$(copyright[0]).text().trim()}`;
}
}
message += '</figcaption></figure>';
$(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) => {
const ProcessFeed = ($) => {
// by default treat it as a hybrid news with video and story-body__inner
let content = $('div.story-body__inner');
let content = $('#main-content article');
if (content.length === 0) {
// it's a video news with video and story-body
@@ -90,45 +13,25 @@ const ProcessFeed = ($, link) => {
}
// remove useless DOMs
content.find('div.bbccom_slot, .off-screen, .embed-report-link').each((i, e) => {
$(e).remove();
content.find('header, section, [data-testid="bbc-logo-wrapper"]').remove();
content.find('noscript').each((i, e) => {
$(e).parent().html($(e).html());
});
if ($('#comp-media-player').length > 0) {
// there is a cover video
let message = `<br><a href='${link}'>使用浏览器播视频/view video in browser</a>`;
const cover = $('#comp-media-player').find('img.player-with-placeholder__image');
if (cover.length > 0) {
// there is a cover image
message = `<br><img src='${cover[0].attribs.src}'>` + message;
content.find('img').each((i, e) => {
if (!$(e).attr('src') && $(e).attr('srcSet')) {
const srcs = $(e).attr('srcSet').split(', ');
const lastSrc = srcs[srcs.length - 1];
$(e).attr('src', lastSrc.split(' ')[0]);
}
$(message).insertAfter(content[0].lastChild);
}
// resize all images and videos
content.find('figure').each((i, e) => {
ProcessImage($, e, '.image-and-copyright-container > img');
// handle lazy-loading images
ProcessImage($, e, '.js-delayed-image-load');
ProcessImage($, e, 'div > div > div > img');
ProcessImage($, e, 'div > div > div > noscript', true);
ProcessVideo($, e, 'figure.media-with-caption > div.player-with-placeholder');
$(e).remove();
});
content.find('[data-component="media-block"] figcaption').prepend('<span>View video in browser: </span>');
return content.html();
};
module.exports = {
ProcessFeed,
ProcessAVFeed,
};