From ded3c71ed63c977ff390d0f12fd597744db92b2e Mon Sep 17 00:00:00 2001 From: Tiago Neves Date: Tue, 13 Oct 2026 06:19:06 +0100 Subject: [PATCH] fix(writer-pipeline): decode literal \u2013 in revista PDF URLs (BIT-31118) FPA media embeds Revista-de-Imprensa filenames with a literal JSON \u2013 escape; new URL turned the backslash into /u2013 and the fetch 404'd. Decode \uXXXX before parsing and accept en-dash / hashed date filenames. Paperclip-Issue: BIT-31118 Paperclip-ERRSIG: 97ac27c4c5df Paperclip-ERRCLUSTER: ddda2b9e3d39 Paperclip-Run: 3f65156a-de49-43a1-876d-75f6285b4bc4 --- writer-pipeline/src/imprensa-ingest.test.ts | 28 +++++++++++++++++++++ writer-pipeline/src/revista-imprensa.ts | 22 ++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/writer-pipeline/src/imprensa-ingest.test.ts b/writer-pipeline/src/imprensa-ingest.test.ts index f45b6a0..4c60da6 100644 --- a/writer-pipeline/src/imprensa-ingest.test.ts +++ b/writer-pipeline/src/imprensa-ingest.test.ts @@ -5,6 +5,7 @@ import { titleFromArticleUrl, } from './html-listing.js'; import { + decodeJsonUnicodeEscapes, extractRevistaPdfUrls, isHandballRevistaLead, leadUrlForRevistaEntry, @@ -88,6 +89,21 @@ test('extractRevistaPdfUrls finds absolute and relative PDF hrefs', () => { assert.ok(urls[0].includes('18.06.2026')); }); +test('decodeJsonUnicodeEscapes turns literal \\u2013 into en-dash (BIT-31118)', () => { + assert.equal(decodeJsonUnicodeEscapes('Revista-de-Imprensa-\\u2013-12.10.2026.pdf'), 'Revista-de-Imprensa-\u2013-12.10.2026.pdf'); +}); + +test('extractRevistaPdfUrls decodes literal \\u2013 so URL is not /u2013 (BIT-31118)', () => { + const html = ` + "pdf":"https://portal.fpa.pt/wp-content/uploads/2026/10/Revista-de-Imprensa-\\u2013-12.10.2026.pdf" + `; + const urls = extractRevistaPdfUrls(html, 'https://portal.fpa.pt/media/'); + assert.equal(urls.length, 1); + assert.ok(!urls[0].includes('/u2013'), urls[0]); + assert.ok(urls[0].includes('%E2%80%93') || urls[0].includes('\u2013'), urls[0]); + assert.ok(urls[0].includes('12.10.2026')); +}); + test('publishedAtFromPtDate parses DD/MM/YYYY', () => { const iso = publishedAtFromPtDate('17/06/2026', '2026-01-01T00:00:00.000Z'); assert.equal(iso.slice(0, 10), '2026-06-17'); @@ -98,6 +114,18 @@ test('pdfDateFromRevistaUrl parses filename date', () => { pdfDateFromRevistaUrl('https://portal.fpa.pt/wp-content/uploads/2026/06/Revista-de-Imprensa-18.06.2026.pdf'), '2026-06-18', ); + assert.equal( + pdfDateFromRevistaUrl( + 'https://portal.fpa.pt/wp-content/uploads/2026/10/Revista-de-Imprensa-%E2%80%93-12.10.2026.pdf', + ), + '2026-10-12', + ); + assert.equal( + pdfDateFromRevistaUrl( + 'https://portal.fpa.pt/wp-content/uploads/2026/10/Revista-de-Imprensa-12.10.2026-9a3f277b.pdf', + ), + '2026-10-12', + ); }); test('isHandballRevistaLead detects andebol keywords', () => { diff --git a/writer-pipeline/src/revista-imprensa.ts b/writer-pipeline/src/revista-imprensa.ts index 4eba81b..37611dc 100644 --- a/writer-pipeline/src/revista-imprensa.ts +++ b/writer-pipeline/src/revista-imprensa.ts @@ -100,13 +100,25 @@ export function isHandballRevistaLead(title: string, source: string): boolean { return /andebol|handball|\behf\b|liga dos campeões de andebol|fpa\b/.test(hay); } +/** + * FPA media JSON sometimes embeds PDF filenames with a *literal* `\u2013` + * (en-dash) escape. `new URL` treats `\` as a path separator and turns that + * into `/u2013`, which 404s. Decode JSON-style `\uXXXX` before URL parsing + * (BIT-31118 / ERRSIG 97ac27c4c5df). + */ +export function decodeJsonUnicodeEscapes(raw: string): string { + return raw.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex: string) => + String.fromCharCode(parseInt(hex, 16)), + ); +} + export function extractRevistaPdfUrls(html: string, pageUrl: string): string[] { const seen = new Set(); const out: string[] = []; const add = (raw: string) => { try { - const href = new URL(raw, pageUrl).href; + const href = new URL(decodeJsonUnicodeEscapes(raw), pageUrl).href; if (!seen.has(href)) { seen.add(href); out.push(href); @@ -134,7 +146,13 @@ export async function parseRevistaPdfBuffer(buffer: Buffer): Promise