fix(scraper): verify complete article evidence (#40)
This commit is contained in:
@ -7,15 +7,56 @@ const CONCURRENCY = 2
|
||||
const ARTICLE_TIMEOUT_MS = 30000
|
||||
const TOTAL_TIMEOUT_MS = 90000
|
||||
|
||||
// Identity is established from primary page metadata or an associated Article
|
||||
// object, never from an arbitrary anchor or the presence of editorial classes.
|
||||
function articleEvidence ($, expectedURL) {
|
||||
const expected = canonicalURL(expectedURL)
|
||||
const key = value => {
|
||||
if (typeof value !== 'string' || !value.trim()) return null
|
||||
try { return canonicalURL(new URL(value, expectedURL).href) } catch { return null }
|
||||
}
|
||||
const primary = $('link[rel="canonical"], meta[property="og:url"]').toArray().map(n => $(n).attr(n.tagName === 'link' ? 'href' : 'content')).filter(Boolean)
|
||||
let associated = Boolean(expected && primary.some(v => key(v) === expected))
|
||||
let mismatch = Boolean(expected && primary.some(v => key(v) !== expected))
|
||||
let blocked = false; let fullBody = null; let articleIdentities = 0; let matching = false
|
||||
for (const script of $('script[type="application/ld+json"]').toArray()) {
|
||||
try {
|
||||
const stack = [[JSON.parse($(script).text()), 0]]; let visited = 0
|
||||
while (stack.length && ++visited <= 2000) {
|
||||
const [node, depth] = stack.pop()
|
||||
if (!node || typeof node !== 'object' || depth > 32) continue
|
||||
const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']]
|
||||
if (types.some(t => ['Article', 'NewsArticle', 'ReportageNewsArticle'].includes(t))) {
|
||||
const identity = node.url || node.mainEntityOfPage || node['@id']
|
||||
const value = typeof identity === 'string' ? identity : identity?.['@id']
|
||||
if (value) articleIdentities++
|
||||
if (expected && key(value) === expected) {
|
||||
associated = matching = true
|
||||
if (node.isAccessibleForFree === false || String(node.isAccessibleForFree).toLowerCase() === 'false') blocked = true
|
||||
if (typeof node.articleBody === 'string') fullBody = node.articleBody
|
||||
}
|
||||
}
|
||||
for (const child of Object.values(node)) if (child && typeof child === 'object') stack.push([child, depth + 1])
|
||||
}
|
||||
} catch { /* Unusable metadata cannot prove completeness. */ }
|
||||
}
|
||||
if (!primary.length && articleIdentities && !matching) mismatch = true
|
||||
return { associated, mismatch, blocked, fullBody }
|
||||
}
|
||||
|
||||
function extractArticle (html, articleURL) {
|
||||
const empty = { content: '', published_at: null, content_truncated: false, content_state: 'unavailable' }
|
||||
if (typeof html !== 'string' || Buffer.byteLength(html) > MAX_HTML_BYTES) return empty
|
||||
const $ = cheerio.load(html)
|
||||
const evidence = articleEvidence($, articleURL)
|
||||
if (evidence.mismatch) return empty
|
||||
const roots = $('article.articleLayout_basic__gcpQk div.body-content, article div.FeatureContent_body__NTf8M')
|
||||
const root = roots.length === 1 ? roots.first() : $()
|
||||
const excludedRoot = 'aside, nav, footer, [hidden], [aria-hidden="true"], [data-component="related-stories"]'
|
||||
if (root.is(excludedRoot) || root.parents(excludedRoot).length) return empty
|
||||
const paragraphs = []
|
||||
root.find('p.ArticleBodyText_articleBodyContent__17wqE, p.typography_articleBody___5jDr, h1, h2, h3, h4, h5, h6, li, blockquote, pre, figcaption, tr').each((_, node) => {
|
||||
const excluded = 'script, style, noscript, nav, aside, footer, button, form, svg, [hidden], [aria-hidden="true"], .placeholder, .disclaimer, [data-component="placeholder"], [data-component="disclaimer"], [data-component="related-stories"]'
|
||||
const excluded = 'script, style, noscript, nav, aside, footer, button, form, svg, [hidden], [aria-hidden="true"], [class*="placeholder" i], [class*="skeleton" i], .disclaimer, [data-component="placeholder"], [data-component="disclaimer"], [data-component="related-stories"]'
|
||||
if ($(node).parentsUntil(root).addBack().is(excluded)) return
|
||||
// A nested paragraph is already represented by its enclosing editorial block.
|
||||
if ($(node).parentsUntil(root, 'li, blockquote, pre, figcaption, tr').length) return
|
||||
@ -29,8 +70,12 @@ function extractArticle (html, articleURL) {
|
||||
const full = paragraphs.join('\n\n')
|
||||
// Keep the full editorial extraction; resource limits reject an extraction
|
||||
// rather than turning a prefix into a supposedly complete article.
|
||||
const blocked = $('[class*="paywall"], [data-test="paywall"], [data-testid="paywall"], [data-content-state="incomplete"], [data-content-truncated="true"]').length > 0
|
||||
return { content: full, published_at: extractPublicationDate(html, articleURL), content_truncated: false, content_state: full ? (blocked ? 'incomplete' : 'complete') : 'unavailable' }
|
||||
const accessMarkers = $('[class*="giftarticle" i], [class*="paywall" i], [id*="captcha" i], [class*="captcha" i], [id^="cf-chl"], [id^="challenge"], script[src*="challenge-platform"], [data-test="paywall"], [data-testid="paywall"], [data-content-state="incomplete"], [data-content-state="partial"], [data-content-truncated="true"]').length > 0
|
||||
const missing = root.find('[class*="placeholder" i], [class*="skeleton" i], [data-component="placeholder"]').addBack('[class*="placeholder" i], [class*="skeleton" i]').length > 0
|
||||
const normalized = value => value.normalize('NFC').replace(/\s+/gu, ' ').trim()
|
||||
const bodyConflict = evidence.fullBody !== null && normalized(evidence.fullBody) !== normalized(full)
|
||||
const blocked = evidence.blocked || accessMarkers || missing || bodyConflict || !evidence.associated
|
||||
return { content: full, published_at: evidence.associated ? extractPublicationDate(html, articleURL) : null, content_truncated: false, content_state: full ? (blocked ? 'incomplete' : 'complete') : 'unavailable' }
|
||||
}
|
||||
function canonicalURL (value) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user