feat(bloomberg): enrich article content for summaries (#36)
This commit is contained in:
@ -16,7 +16,7 @@ function reliableISO(value) {
|
||||
return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : null
|
||||
}
|
||||
|
||||
function extractPublicationDate(html) {
|
||||
function extractPublicationDate(html, expectedURL) {
|
||||
const $ = cheerio.load(html)
|
||||
for (const selector of ['meta[name="iso-8601-publish-date"]', 'time[datetime]']) {
|
||||
for (const element of $(selector).toArray()) {
|
||||
@ -30,7 +30,19 @@ function extractPublicationDate(html) {
|
||||
if (!Array.isArray(node)) {
|
||||
const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']]
|
||||
if (types.some(type => ['Article', 'NewsArticle', 'ReportageNewsArticle'].includes(type))) {
|
||||
const date = reliableISO(node.datePublished)
|
||||
let associated = !expectedURL
|
||||
if (expectedURL) {
|
||||
const identity = node.url || node.mainEntityOfPage || node['@id']
|
||||
const value = typeof identity === 'string' ? identity : identity?.['@id']
|
||||
try {
|
||||
if (typeof value === 'string') {
|
||||
const actual = new URL(value, expectedURL); actual.search = ''; actual.hash = ''
|
||||
const expected = new URL(expectedURL); expected.search = ''; expected.hash = ''
|
||||
associated = isArticleURL(actual.href) && actual.href === expected.href
|
||||
}
|
||||
} catch { /* No trustworthy article identity. */ }
|
||||
}
|
||||
const date = associated ? reliableISO(node.datePublished) : null
|
||||
if (date) return date
|
||||
}
|
||||
}
|
||||
@ -75,7 +87,7 @@ function withDatesFromLoadedHtml(articles, html, loadedURL, parsed) {
|
||||
const wanted = new Set(articles.map(article => key(article.url)).filter(Boolean))
|
||||
const pageKey = key(loadedURL)
|
||||
if (pageKey && wanted.has(pageKey)) {
|
||||
const date = extractPublicationDate(html)
|
||||
const date = extractPublicationDate(html, pageKey)
|
||||
if (date) dates.set(pageKey, date)
|
||||
}
|
||||
for (const anchor of $('a[data-component="story-link"]').toArray()) {
|
||||
|
||||
Reference in New Issue
Block a user