fix(bloomberg): avoid collection timeouts

This commit is contained in:
2026-11-24 09:41:26 +01:00
parent d129af0d56
commit 1981018ae1
11 changed files with 641 additions and 73 deletions

View File

@ -58,14 +58,60 @@ function isArticleURL(value) {
} catch { return false }
}
async function withPublicationDates(articles, loadHtml) {
const result = []
for (const article of articles) {
let published_at = null
try { if (isArticleURL(article.url)) published_at = extractPublicationDate(await loadHtml(article.url)) } catch { /* Date stays unknown. */ }
result.push({ ...article, published_at })
// Only associate metadata with an article identified in this already-loaded
// document. Section-level publication dates must never be copied onto its links.
function withDatesFromLoadedHtml(articles, html, loadedURL, parsed) {
const $ = parsed || cheerio.load(html)
const dates = new Map()
function key(value) {
try {
if (typeof value !== 'string' || !value.trim()) return null
const url = new URL(value, loadedURL)
url.search = ''
url.hash = ''
return isArticleURL(url.href) ? url.href : null
} catch { return null }
}
return result
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)
if (date) dates.set(pageKey, date)
}
for (const anchor of $('a[data-component="story-link"]').toArray()) {
const articleKey = key($(anchor).attr('href'))
if (!articleKey || !wanted.has(articleKey) || dates.has(articleKey)) continue
let scope = $(anchor)
const card = scope.closest('article')
if (card.length) {
const links = new Set(card.find('a[data-component="story-link"]').toArray()
.map(link => key($(link).attr('href'))).filter(Boolean))
if (links.size === 1 && links.has(articleKey)) scope = card
}
for (const [selector, attribute] of [['meta[name="iso-8601-publish-date"]', 'content'], ['time[datetime]', 'datetime']]) {
for (const element of scope.find(selector).toArray()) {
const date = reliableISO($(element).attr(attribute))
if (date) { dates.set(articleKey, date); break }
}
if (dates.has(articleKey)) break
}
}
let visited = 0
function collect(node, depth = 0) {
if (!node || typeof node !== 'object' || depth > 32 || ++visited > 2000) return
const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']]
if (types.some(type => ['Article', 'NewsArticle', 'ReportageNewsArticle'].includes(type))) {
const identity = node.url || node.mainEntityOfPage
const articleKey = key(typeof identity === 'string' ? identity : identity?.['@id'])
const date = reliableISO(node.datePublished)
if (articleKey && wanted.has(articleKey) && !dates.has(articleKey) && date) dates.set(articleKey, date)
}
for (const child of Object.values(node)) collect(child, depth + 1)
}
for (const script of $('script[type="application/ld+json"]').toArray()) {
try { collect(JSON.parse($(script).text())) } catch { /* Unknown date, no raw metadata logging. */ }
}
return articles.map(article => ({ ...article, published_at: dates.get(key(article.url)) || null }))
}
module.exports = { extractPublicationDate, withPublicationDates }
module.exports = { extractPublicationDate, withDatesFromLoadedHtml }