fix(writer-pipeline): decode literal \u2013 in revista PDF URLs (BIT-31118) #53

Merged
spark merged 2 commits from fix/BIT-31118-revista-imprensa-u2013-404 into main 2026-10-13 06:25:22 +01:00
2 changed files with 48 additions and 2 deletions

No files matched your search

@@ -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', () => {
+20 -2
View File
@@ -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<string>();
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<RevistaEntr
}
export function pdfDateFromRevistaUrl(pdfUrl: string): string | null {
const m = pdfUrl.match(/Revista-de-Imprensa-(\d{2})\.(\d{2})\.(\d{4})\.pdf/i);
// Accept hyphen or en-dash before the date, and optional WP hash suffix:
// Revista-de-Imprensa-12.10.2026.pdf
// Revista-de-Imprensa-–-12.10.2026.pdf
// Revista-de-Imprensa-12.10.2026-9a3f277b.pdf
const m = pdfUrl.match(
/Revista-de-Imprensa-(?:(?:\u2013|%E2%80%93)-)?(\d{2})\.(\d{2})\.(\d{4})(?:-[a-f0-9]+)?\.pdf/i,
);
if (!m) return null;
return `${m[3]}-${m[2]}-${m[1]}`;
}