diff --git a/lib/v2/bloomberg/react-renderer.js b/lib/v2/bloomberg/react-renderer.js
new file mode 100644
index 000000000..3a174dcc0
--- /dev/null
+++ b/lib/v2/bloomberg/react-renderer.js
@@ -0,0 +1,181 @@
+const art = require('art-template');
+const path = require('path');
+const { processVideo } = require('./utils');
+
+const nodeRenderers = {
+ paragraph: (node, nextNode) => `
${nextNode(node.content)}
`,
+ text: (node) => {
+ const { attributes: attr, value: val } = node;
+ if (attr?.emphasis && attr?.strong) {
+ return `${val}`;
+ } else if (attr?.emphasis) {
+ return `${val}`;
+ } else if (attr?.strong) {
+ return `${val}`;
+ } else {
+ return val;
+ }
+ },
+ 'inline-newsletter': (node, nextNode) => `${nextNode(node.content)}
`,
+ heading: (node, nextNode) => {
+ const nodeData = node.data;
+ if (nodeData.level === 2 || nodeData.level === 3) {
+ return `${nextNode(node.content)}
`;
+ }
+ },
+ link: (node, nextNode) => {
+ const dest = node.data.destination;
+ const web = dest.web;
+ const bbg = dest.bbg;
+ const title = node.data.title;
+ if (web) {
+ return `${nextNode(node.content)}`;
+ }
+
+ if (bbg && bbg.startsWith('bbg://news/stories')) {
+ const o = bbg.split('bbg://news/stories/').pop();
+ const s = 'https://www.bloomberg.com/news/terminal/'.concat(o);
+ return `${nextNode(node.content)}`;
+ }
+ return String(nextNode(node.content));
+ },
+ entity: (node, nextNode) => {
+ const t = node.subType;
+ const linkDest = node.data.link.destination;
+ const web = linkDest.web;
+ if (t === 'person') {
+ return String(nextNode(node.content));
+ }
+ if (t === 'story') {
+ if (web) {
+ return `${nextNode(node.content)}`;
+ }
+ const a = node.data.story.identifiers.suid;
+ const o = 'https://www.bloomberg.com/news/terminal/'.concat(a);
+ return `${nextNode(node.content)}`;
+ }
+ if (t === 'security') {
+ const s = node.data.security.identifiers.parsekey;
+ if (s) {
+ const c = s.split(' ');
+ const href = 'https://www.bloomberg.com/quote/'.concat(c[0], ':').concat(c[1]);
+ return `${nextNode(node.content)}`;
+ }
+ }
+ return String(nextNode(node.content));
+ },
+ br: () => `
`,
+ hr: () => `
`,
+ ad: () => {},
+ blockquote: (node, nextNode) => `${nextNode(node.content)}
`,
+ quote: (node, nextNode) => `${nextNode(node.content)}
`,
+ aside: (node, nextNode) => ``,
+ list: (node, nextNode) => {
+ const t = node.subType;
+ if (t === 'unordered') {
+ return `${nextNode(node.content)}
`;
+ }
+ if (t === 'ordered') {
+ return `${nextNode(node.content)}
`;
+ }
+ },
+ listItem: (node, nextNode) => `${nextNode(node.content)}`,
+ media: (node) => {
+ const t = node.subType;
+ if (t === 'chart' && node.data.attachment) {
+ if (node.data.attachment.creator === 'TOASTER') {
+ const c = node.data.chart;
+ const e = {
+ src: (c && c.fallback) || '',
+ chart: node.data.attachment,
+ id: (c && c.id) || '',
+ alt: (c && c.alt) || '',
+ };
+ const w = e.chart;
+
+ const chart = {
+ source: w.source,
+ footnote: w.footnote,
+ url: w.url,
+ title: w.title,
+ subtitle: w.subtitle,
+ chartId: 'toaster-chart-'.concat(e.id),
+ chartAlt: e.alt,
+ fallback: e.src,
+ };
+ return art(path.join(__dirname, 'templates/chart_media.art'), { chart });
+ }
+ const image = {
+ alt: node.data.attachment?.footnote || '',
+ caption: node.data.attachment?.title + node.data.attachment.subtitle || '',
+ credit: node.data.attachment?.source || '',
+ src: node.data.chart?.fallback || '',
+ };
+ return art(path.join(__dirname, 'templates/image_figure.art'), image);
+ }
+ if (t === 'photo') {
+ const h = node.data;
+ let img = '';
+ if (h.attachment) {
+ const image = { src: h.photo?.src, alt: h.photo?.alt, caption: h.photo?.caption, credit: h.photo?.credit };
+ img = art(path.join(__dirname, 'templates/image_figure.art'), image);
+ }
+ if (h.link && h.link.destination && h.link.destination.web) {
+ const href = h.link.destination.web;
+ return `${img}`;
+ }
+ return img;
+ }
+ if (t === 'video') {
+ const h = node.data;
+ const id = h.attachment && h.attachment.id;
+ return processVideo(id);
+ }
+ if (t === 'audio' && node.data.attachment) {
+ const B = node.data.attachment;
+ const P = B.title;
+ const D = B.url;
+ const M = B.image;
+ if (P && D) {
+ const audio = {
+ src: D,
+ img: M,
+ caption: P,
+ credit: '',
+ };
+ return art(path.join(__dirname, 'templates/audio_media.art'), audio);
+ }
+ }
+ return '';
+ },
+};
+
+const nodeToHtmlString = (node, obj) => {
+ const nextNode = (nodes) => nodeListToHtmlString(nodes);
+ if (!node.type || !nodeRenderers[node.type]) {
+ return `${node.type}`;
+ }
+ return nodeRenderers[node.type](node, nextNode, obj);
+};
+
+const nodeListToHtmlString = (nodes) =>
+ nodes
+ .map((node, index) =>
+ nodeToHtmlString(node, {
+ index,
+ prev: nodes[index - 1]?.type,
+ next: nodes[index + 1]?.type,
+ })
+ )
+ .join('');
+
+const documentToHtmlString = (document) => {
+ if (!document || !document.content) {
+ return '';
+ }
+ return nodeListToHtmlString(document.content);
+};
+
+module.exports = {
+ documentToHtmlString,
+};
diff --git a/lib/v2/bloomberg/templates/chart_media.art b/lib/v2/bloomberg/templates/chart_media.art
new file mode 100644
index 000000000..255fbb7d6
--- /dev/null
+++ b/lib/v2/bloomberg/templates/chart_media.art
@@ -0,0 +1,21 @@
+
+ {{if chart.title}}
+ {{chart.title}}
+ {{/if}}
+ {{if chart.subtitle}}
+ {{chart.subtitle}}
+ {{/if}}
+
+
+ {{if chart.source}}
+
+ {{@ chart.source }}
+ {{if chart.footnote}}
+ {{chart.footnote}}
+ {{/if}}
+
+ {{/if}}
+
diff --git a/lib/v2/bloomberg/utils.js b/lib/v2/bloomberg/utils.js
index 48f592c8e..de8a76c24 100644
--- a/lib/v2/bloomberg/utils.js
+++ b/lib/v2/bloomberg/utils.js
@@ -6,6 +6,8 @@ const { parseDate } = require('@/utils/parse-date');
const got = require('@/utils/got');
const { art } = require('@/utils/render');
+const { documentToHtmlString } = require('./react-renderer');
+
const rootUrl = 'https://www.bloomberg.com/feeds';
const sel = 'script[data-component-props="ArticleBody"], script[data-component-props="FeatureBody"]';
const apiEndpoints = {
@@ -27,7 +29,7 @@ const apiEndpoints = {
},
newsletters: {
url: 'https://www.bloomberg.com/news/newsletters/',
- sel,
+ sel: 'script#__NEXT_DATA__',
},
'photo-essays': {
url: 'https://www.bloomberg.com/javelin/api/photo-essay_transporter/',
@@ -104,9 +106,9 @@ const parseArticle = (item, ctx) =>
}
}
- // Blocked by PX3, return the default
+ // Blocked by PX3, or 404 by api, return the default
const redirectUrls = res.redirectUrls.map(String);
- if (redirectUrls.some((r) => new URL(r).pathname === '/tosv2.html')) {
+ if (redirectUrls.some((r) => new URL(r).pathname === '/tosv2.html') || res.statusCode === 404) {
return {
title: item.title,
link: item.link,
@@ -123,6 +125,8 @@ const parseArticle = (item, ctx) =>
return parsePhotoEssaysPage(res, api, item);
case 'features/': // single features page
return parseFeaturePage(res, api, item);
+ case 'newsletters':
+ return parseNewsletterPage(res, api, item);
default:
return parseOtherPage(res, api, item);
}
@@ -131,6 +135,27 @@ const parseArticle = (item, ctx) =>
return item;
});
+const parseNewsletterPage = async (res, api, item) => {
+ const newsletter_json = JSON.parse(cheerio.load(res.data)(api.sel).html()).props.pageProps;
+ const story_json = newsletter_json.story;
+ const media_img = story_json.ledeImageUrl || Object.values(story_json.imageAttachments ?? {})[0]?.baseUrl;
+
+ const rss_item = {
+ title: story_json.headline || item.title,
+ link: story_json.url || item.link,
+ guid: `bloomberg:${story_json.id}`,
+ description: processHeadline(story_json) + (await processLedeMedia(story_json)) + documentToHtmlString(story_json.body || ''),
+ pubDate: parseDate(story_json.publishedAt) || item.pubDate,
+ author: story_json.authors?.map((a) => a.name).join(', ') ?? [],
+ category: story_json.mostRelevantTags ?? [],
+ media: {
+ content: { url: media_img },
+ thumbnails: { url: media_img },
+ },
+ };
+ return rss_item;
+};
+
const parseAudioPage = async (res, api, item) => {
const audio_json = JSON.parse(cheerio.load(res.data)(api.sel).html()).props.pageProps;
const episode = audio_json.episode;
@@ -258,7 +283,7 @@ const parseOtherPage = async function (res, api, item) {
};
const processHeadline = (story_json) => {
- const dek = story_json.dek || '';
+ const dek = story_json.dek || story_json.summary || '';
const abs = story_json.abstract?.map((a) => `${a}`).join('');
return abs ? dek + `` : dek;
};
@@ -276,6 +301,13 @@ const processLedeMedia = async (story_json) => {
video: kind === 'video' && (await processVideo(story_json.ledeAttachment.bmmrId)),
};
return art(path.join(__dirname, 'templates/lede_media.art'), { media });
+ } else if (story_json.lede) {
+ const lede = story_json.lede;
+ const image = {
+ src: lede.url,
+ alt: lede.alt || lede.title,
+ };
+ return art(path.join(__dirname, 'templates/image_figure.art'), image);
} else if (story_json.imageAttachments) {
const attachment = Object.values(story_json.imageAttachments)[0];
if (attachment) {
@@ -404,4 +436,5 @@ module.exports = {
asyncPoolAll,
parseNewsList,
parseArticle,
+ processVideo,
};