From 47fcb053da4f0057c3b5168579b5e64fded6b060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alves?= Date: Fri, 19 Jun 2026 07:04:52 +0000 Subject: [PATCH] fix(writer): revista_imprensa publisher og:image before WebDAV (BIT-25590) Synthetic revista lead URLs return 404; probe publisher homepage for og:image so drafts succeed when the federation WebDAV archive is down or misconfigured. Co-authored-by: Cursor --- writer-pipeline/src/imprensa-ingest.test.ts | 9 +++++ writer-pipeline/src/revista-imprensa.ts | 11 ++++++ writer-pipeline/src/wp-publish.ts | 39 +++++++++++---------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/writer-pipeline/src/imprensa-ingest.test.ts b/writer-pipeline/src/imprensa-ingest.test.ts index f477d23..f45b6a0 100644 --- a/writer-pipeline/src/imprensa-ingest.test.ts +++ b/writer-pipeline/src/imprensa-ingest.test.ts @@ -11,6 +11,7 @@ import { parseRevistaIndexText, publishedAtFromPtDate, pdfDateFromRevistaUrl, + publisherHomeUrlForRevistaLead, resolvePublisherUrl, } from './revista-imprensa.js'; @@ -58,6 +59,14 @@ test('resolvePublisherUrl maps Record and A Bola', () => { assert.equal(resolvePublisherUrl('Bola (A)'), 'https://www.abola.pt/'); }); +test('publisherHomeUrlForRevistaLead maps synthetic lead URL to publisher origin', () => { + assert.equal( + publisherHomeUrlForRevistaLead('https://www.abola.pt/revista-imprensa/18-06-2026/3'), + 'https://www.abola.pt/', + ); + assert.equal(publisherHomeUrlForRevistaLead('https://www.record.pt/modalidades/andebol/detalhe/x'), null); +}); + test('leadUrlForRevistaEntry uses publisher host with stable path', () => { const url = leadUrlForRevistaEntry('Record', 'https://portal.fpa.pt/media/x.pdf', { index: 18, diff --git a/writer-pipeline/src/revista-imprensa.ts b/writer-pipeline/src/revista-imprensa.ts index 02eaa74..4eba81b 100644 --- a/writer-pipeline/src/revista-imprensa.ts +++ b/writer-pipeline/src/revista-imprensa.ts @@ -42,6 +42,17 @@ export function resolvePublisherUrl(sourceLabel: string): string { return 'https://portal.fpa.pt/media/'; } +/** Synthetic revista lead URLs are not real article pages — probe publisher origin for og:image (BIT-25590). */ +export function publisherHomeUrlForRevistaLead(itemUrl: string): string | null { + try { + const u = new URL(itemUrl.trim()); + if (!u.pathname.includes('/revista-imprensa/')) return null; + return `${u.origin}/`; + } catch { + return null; + } +} + export function leadUrlForRevistaEntry( sourceLabel: string, pdfCanonicalUrl: string, diff --git a/writer-pipeline/src/wp-publish.ts b/writer-pipeline/src/wp-publish.ts index 4f31c30..ed66717 100644 --- a/writer-pipeline/src/wp-publish.ts +++ b/writer-pipeline/src/wp-publish.ts @@ -8,6 +8,7 @@ import { resolveFeaturedFromWebDavArchive, type WebDavArchiveConfig, } from './webdav-archive.js'; +import { publisherHomeUrlForRevistaLead } from './revista-imprensa.js'; export interface WpPublishConfig { wpBase: string; @@ -67,6 +68,18 @@ function allowedMediaUrl(href: string): boolean { } } +async function featuredImageFromPageHtml( + pageUrl: string, + creditFallback?: string, +): Promise<{ url: string; credit?: string } | null> { + const raw = await fetchArticleHtmlForMeta(pageUrl); + if (!raw) return null; + const parsed = extractFeaturedImageFromHtml(raw); + const abs = parsed.url ? absolutizeImageUrl(parsed.url, pageUrl) : ''; + if (!abs || !allowedMediaUrl(abs)) return null; + return { url: abs, credit: parsed.credit || creditFallback }; +} + /** * Resolves a rights-cleared image URL for the article (ingest hints, then live HTML). * Throws if none — drafts must not ship without a featured image (BIT-22288). @@ -90,27 +103,17 @@ export async function resolveFeaturedImageForDraft(item: AggregatedItem): Promis webDavEnvError = e instanceof Error ? e.message : String(e); } - const raw = await fetchArticleHtmlForMeta(item.url); - if (!raw) { - if (webDavCfg) { - return resolveFeaturedFromWebDavArchive(webDavCfg, item); + if (item.sourceKind === 'revista_imprensa') { + const publisherHome = publisherHomeUrlForRevistaLead(item.url); + if (publisherHome) { + const fromPublisher = await featuredImageFromPageHtml(publisherHome, item.featuredImageCredit); + if (fromPublisher) return fromPublisher; } - if (webDavEnvError) { - throw new Error( - `Mandatory featured image: cannot fetch article HTML for ${item.url}; WebDAV env invalid: ${webDavEnvError} (BIT-22292).`, - ); - } - throw new Error( - `Mandatory featured image: cannot fetch article HTML for ${item.url}. Optionally set WRITER_WEBDAV_ENABLED=1 and WRITER_WEBDAV_* to use the federation photo archive (BIT-22292).`, - ); - } - const parsed = extractFeaturedImageFromHtml(raw); - const abs = parsed.url ? absolutizeImageUrl(parsed.url, item.url) : ''; - if (abs && allowedMediaUrl(abs)) { - const credit = parsed.credit || item.featuredImageCredit; - return { url: abs, credit }; } + const fromArticle = await featuredImageFromPageHtml(item.url, item.featuredImageCredit); + if (fromArticle) return fromArticle; + if (webDavCfg) { return resolveFeaturedFromWebDavArchive(webDavCfg, item); } -- 2.47.3