Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | 1x 1x 1x 1x 1x 8x 67x 67x 1x 1x 1x 116x 116x 116x 116x 116x 183x 183x 67x 67x 67x 67x 67x 67x 67x 67x 116x 174x 174x 174x 174x 174x 19x 19x 19x 19x 19x 19x 30x 30x 30x 29x 29x 29x 29x 29x 29x 29x 29x 30x 30x 30x 30x 30x 30x 30x 30x 19x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 11x 10x 10x 10x 10x 10x 17x 7x 7x 17x 17x 8x 2x 6x 6x 6x 9x 2x 6x 6x 6x 6x 1x 6x 6x 1x 5x 1x 4x 6x 6x 6x 3x 3x 4x 4x | #!/usr/bin/env node
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module scripts/scrape-doceo-votes
* @description DOCEO XML scraper for European Parliament roll-call vote data.
*
* The EP MCP `get_latest_votes` tool has a 4–6 week publication lag because
* the Open Data API feed is populated well after each plenary. This script
* fetches the DOCEO XML documents directly, which are typically available
* within 24–48 hours of a plenary sitting.
*
* DOCEO XML URL pattern (term 10, 2024–present):
* https://www.europarl.europa.eu/doceo/document/PV-10-{YYYY-MM-DD}-RCV_EN.xml
*
* Rate limiting: 1 request/second ceiling; each call sleeps 1 s after
* completion to respect the EP's robots.txt directive.
*
* Publication-lag detection: a 404 response is treated as "not yet
* published" (publication lag), not as a hard error. The output JSON
* includes a `publicationLag` flag so callers can distinguish missing
* data from an error condition.
*
* Retry policy: up to 3 retries with exponential backoff
* (1 s / 2 s / 4 s) for transient network failures (non-404).
*
* Output: `<outputDir>/roll-call-votes.json` (canonical data file)
*
* Invocation:
* node scripts/scrape-doceo-votes.js --date 2026-05-14 --slug breaking \
* [--output-dir analysis/daily/2026-05-14/breaking/data] \
* [--term 10] [--dry-run]
*
* Exports (for unit testing):
* buildDoceoUrl(date, term)
* parseDoceoXml(xmlText)
* scrapeDoceoVotes(options)
*/
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
/** Current EP parliamentary term. */
const EP_CURRENT_TERM = 10;
/** Rate-limit inter-request delay (ms). */
const RATE_LIMIT_MS = 1_000;
/** Default retry policy. */
const DEFAULT_MAX_RETRIES = 3;
const DEFAULT_BASE_DELAY_MS = 1_000;
/** Output filename written to the data directory. */
const OUTPUT_FILENAME = 'roll-call-votes.json';
// ---------------------------------------------------------------------------
// URL construction
// ---------------------------------------------------------------------------
/**
* Build the canonical DOCEO RCV XML URL for a given plenary date and EP term.
*
* @param {string} date - ISO date string (YYYY-MM-DD)
* @param {number} [term] - EP parliamentary term number (default: 10)
* @returns {string} Full DOCEO XML URL
*/
export function buildDoceoUrl(date, term = EP_CURRENT_TERM) {
return `https://www.europarl.europa.eu/doceo/document/PV-${term}-${date}-RCV_EN.xml`;
}
// ---------------------------------------------------------------------------
// XML parsing
// ---------------------------------------------------------------------------
/**
* Decode common XML entities in a text string.
*
* @param {string} text - Text that may contain XML entity references
* @returns {string} Decoded text
*/
function decodeXmlEntities(text) {
Iif (!text) return text;
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(/&#(\d+);/g, (_, code) => {
const cp = parseInt(code, 10);
Eif (cp >= 0 && cp <= 0x10FFFF && (cp < 0xD800 || cp > 0xDFFF)) {
return String.fromCodePoint(cp);
}
return '';
})
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => {
const cp = parseInt(hex, 16);
if (cp >= 0 && cp <= 0x10FFFF && (cp < 0xD800 || cp > 0xDFFF)) {
return String.fromCodePoint(cp);
}
return '';
});
}
/**
* Extract all text occurrences of a simple XML tag from a source string.
* Only works for non-nested, single-line tag content.
* Decodes XML entities in extracted text.
*
* @param {string} xml - Source XML text
* @param {string} tag - Tag name (without `<` / `>`)
* @returns {string[]} Array of matched text content strings
*/
function extractTagContents(xml, tag) {
const results = [];
const openTag = `<${tag}`;
const closeTag = `</${tag}>`;
let searchFrom = 0;
while (searchFrom < xml.length) {
const start = xml.indexOf(openTag, searchFrom);
if (start === -1) break;
// Find the end of the opening tag (could have attributes)
const openEnd = xml.indexOf('>', start);
Iif (openEnd === -1) break;
// Self-closing tag — skip
Iif (xml[openEnd - 1] === '/') {
searchFrom = openEnd + 1;
continue;
}
const contentStart = openEnd + 1;
const contentEnd = xml.indexOf(closeTag, contentStart);
Iif (contentEnd === -1) {
searchFrom = openEnd + 1;
continue;
}
results.push(decodeXmlEntities(xml.slice(contentStart, contentEnd).trim()));
searchFrom = contentEnd + closeTag.length;
}
return results;
}
/**
* Extract the value of a named XML attribute from a tag string.
* Supports both double-quoted (`attr="value"`) and single-quoted
* (`attr='value'`) forms — both are valid XML.
*
* @param {string} tagStr - The full opening tag (e.g. `<Result.For Total="45">`)
* @param {string} attr - Attribute name
* @returns {string} Attribute value, or empty string if not found
*/
function extractAttr(tagStr, attr) {
// Escape regex metacharacters in attr name to prevent pattern injection
// even though callers currently pass safe literals.
const safeAttr = attr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Match: <word boundary>attr<whitespace?>=<whitespace?>"value" or 'value'
const re = new RegExp(`\\b${safeAttr}\\s*=\\s*(?:"([^"]*)"|'([^']*)')`);
const m = tagStr.match(re);
Iif (!m) return '';
return m[1] ?? m[2] ?? '';
}
/**
* Parse a DOCEO RCV XML document into a structured votes object.
*
* Returns a `{ votes: VoteRecord[], parsedAt: string }` object.
*
* Each `VoteRecord` has:
* - `id`: string — DOCEO vote identifier
* - `description`: string — Vote title / description text
* - `date`: string — Vote date (YYYY-MM-DD from XML attribute)
* - `method`: string — Vote method (e.g. "secret", "roll-call")
* - `for`: number — Total votes For
* - `against`: number — Total votes Against
* - `abstention`: number — Total votes Abstention
* - `membersFor`: string[] — MEP names voting For (from RCV XML)
* - `membersAgainst`: string[] — MEP names voting Against
* - `membersAbstention`: string[] — MEP names Abstaining
*
* @param {string} xmlText - Raw XML string from DOCEO
* @returns {{ votes: object[], parsedAt: string, voteCount: number }}
*/
export function parseDoceoXml(xmlText) {
const votes = [];
const parsedAt = new Date().toISOString();
// Tolerate any whitespace (space, tab, newline) after the tag name before
// attributes, OR an immediate `>` for an attribute-less opening. Valid XML
// can put a newline before the first attribute, and an exact-string
// `<RollCallVote.Result ` match would silently drop every vote in such a
// document. Non-capturing alternation avoids an unused capture group.
const blockOpenRe = /<RollCallVote\.Result(?:\s|>)/g;
const blockClose = '</RollCallVote.Result>';
let m;
blockOpenRe.lastIndex = 0;
while ((m = blockOpenRe.exec(xmlText)) !== null) {
const blockStart = m.index;
const blockEnd = xmlText.indexOf(blockClose, blockStart);
if (blockEnd === -1) break;
const block = xmlText.slice(blockStart, blockEnd + blockClose.length);
// Extract attributes from the opening tag
const openTagEnd = block.indexOf('>');
const openTag = block.slice(0, openTagEnd + 1);
const id = extractAttr(openTag, 'Identifier');
const date = extractAttr(openTag, 'Date');
const method = extractAttr(openTag, 'Method');
// Description text
const descriptions = extractTagContents(block, 'RollCallVote.Description.Text');
const description = descriptions.length > 0 ? descriptions[0] : '';
// For / Against / Abstention totals
const forTotal = _extractGroupTotal(block, 'Result.For');
const againstTotal = _extractGroupTotal(block, 'Result.Against');
const abstentionTotal = _extractGroupTotal(block, 'Result.Abstention');
// MEP names (for roll-call transparency)
const membersFor = _extractMemberNames(block, 'Result.For');
const membersAgainst = _extractMemberNames(block, 'Result.Against');
const membersAbstention = _extractMemberNames(block, 'Result.Abstention');
votes.push({
id,
description,
date,
method,
for: forTotal,
against: againstTotal,
abstention: abstentionTotal,
membersFor,
membersAgainst,
membersAbstention,
});
// Advance the regex past this block so the next exec() searches forward.
blockOpenRe.lastIndex = blockEnd + blockClose.length;
}
return { votes, parsedAt, voteCount: votes.length };
}
/**
* Extract the Total attribute from a Result.For/Against/Abstention section.
*
* @param {string} block - The full RollCallVote.Result XML block
* @param {string} sectionTag - Section tag name (e.g. "Result.For")
* @returns {number} Parsed integer total, or 0 if not found
*/
function _extractGroupTotal(block, sectionTag) {
const openTag = `<${sectionTag}`;
const startIdx = block.indexOf(openTag);
Iif (startIdx === -1) return 0;
const tagEnd = block.indexOf('>', startIdx);
Iif (tagEnd === -1) return 0;
const tagContent = block.slice(startIdx, tagEnd + 1);
const totalStr = extractAttr(tagContent, 'Total');
const parsed = parseInt(totalStr, 10);
return Number.isNaN(parsed) ? 0 : parsed;
}
/**
* Extract Member.Name text nodes from a vote result section.
*
* @param {string} block - The full RollCallVote.Result XML block
* @param {string} sectionTag - Section tag name (e.g. "Result.For")
* @returns {string[]} Array of MEP display names
*/
function _extractMemberNames(block, sectionTag) {
const openTag = `<${sectionTag}`;
const closeTag = `</${sectionTag}>`;
const startIdx = block.indexOf(openTag);
Iif (startIdx === -1) return [];
const endIdx = block.indexOf(closeTag, startIdx);
Iif (endIdx === -1) return [];
const section = block.slice(startIdx, endIdx + closeTag.length);
return extractTagContents(section, 'Member.Name');
}
// ---------------------------------------------------------------------------
// HTTP fetch with retry
// ---------------------------------------------------------------------------
/**
* Sleep for `ms` milliseconds.
*
* @param {number} ms - Milliseconds to sleep
* @returns {Promise<void>}
*/
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Fetch a URL with exponential-backoff retry.
*
* A 404 response is NOT retried — it signals publication lag and is returned
* as a distinct non-error result so callers can branch on `publicationLag`.
*
* @param {string} url
* @param {{ maxRetries?: number, baseDelayMs?: number, fetchImpl?: Function }} [options]
* @returns {Promise<{ ok: boolean, status: number, text: string|null, publicationLag: boolean }>}
*/
export async function fetchWithRetry(url, options = {}) {
const maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
const baseDelayMs = options.baseDelayMs ?? DEFAULT_BASE_DELAY_MS;
const fetchFn = options.fetchImpl ?? fetch;
let lastError = null;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
if (attempt > 0) {
const delay = baseDelayMs * Math.pow(2, attempt - 1);
await sleep(delay);
}
try {
const resp = await fetchFn(url, {
headers: {
'User-Agent': 'euparliamentmonitor/0.9.0 (+https://github.com/Hack23/euparliamentmonitor)',
'Accept': 'application/xml, text/xml, */*;q=0.8',
},
});
// 404 = publication lag — do not retry
if (resp.status === 404) {
return { ok: false, status: 404, text: null, publicationLag: true };
}
Eif (resp.ok) {
const text = await resp.text();
return { ok: true, status: resp.status, text, publicationLag: false };
}
// Non-404 error — fall through to retry
lastError = new Error(`HTTP ${resp.status} from ${url}`);
} catch (err) {
lastError = err;
}
}
throw lastError ?? new Error(`Failed to fetch ${url} after ${maxRetries + 1} attempts`);
}
// ---------------------------------------------------------------------------
// Main scraper
// ---------------------------------------------------------------------------
/**
* Scrape DOCEO roll-call vote XML for one plenary date and write the result.
*
* @param {{
* date: string,
* outputDir: string,
* term?: number,
* dryRun?: boolean,
* fetchImpl?: Function,
* rateLimitMs?: number
* }} options
* @returns {Promise<{
* success: boolean,
* publicationLag: boolean,
* outputFile: string|null,
* voteCount: number,
* url: string,
* error?: string
* }>}
*/
export async function scrapeDoceoVotes(options) {
const {
date,
outputDir,
term = EP_CURRENT_TERM,
dryRun = false,
fetchImpl,
rateLimitMs = RATE_LIMIT_MS,
retryBaseDelayMs,
} = options;
const url = buildDoceoUrl(date, term);
const retryOptions = { fetchImpl };
if (retryBaseDelayMs !== undefined) {
retryOptions.baseDelayMs = retryBaseDelayMs;
}
let fetchResult;
try {
fetchResult = await fetchWithRetry(url, retryOptions);
} catch (err) {
return {
success: false,
publicationLag: false,
outputFile: null,
voteCount: 0,
url,
error: String(err),
};
}
if (fetchResult.publicationLag) {
return {
success: false,
publicationLag: true,
outputFile: null,
voteCount: 0,
url,
error: 'Publication lag -- DOCEO XML not yet available for this date (HTTP 404)',
};
}
const parsed = parseDoceoXml(fetchResult.text ?? '');
const outputFile = path.join(outputDir, OUTPUT_FILENAME);
const outputPayload = {
date,
term,
url,
scrapedAt: parsed.parsedAt,
voteCount: parsed.voteCount,
publicationLag: false,
votes: parsed.votes,
};
if (!dryRun) {
fs.mkdirSync(outputDir, { recursive: true });
fs.writeFileSync(outputFile, JSON.stringify(outputPayload, null, 2), 'utf8');
}
// Rate-limit: sleep after write before returning
await sleep(rateLimitMs);
return {
success: true,
publicationLag: false,
outputFile: dryRun ? null : outputFile,
voteCount: parsed.voteCount,
url,
};
}
// ---------------------------------------------------------------------------
// CLI entry point
// ---------------------------------------------------------------------------
/**
* Parse minimalist `--key value` CLI args from argv.
*
* @param {string[]} argv
* @returns {Record<string, string|boolean>}
*/
/* c8 ignore start */
function parseArgs(argv) {
const out = {};
let i = 0;
while (i < argv.length) {
const arg = argv[i];
if (arg.startsWith('--')) {
const key = arg.slice(2);
const next = argv[i + 1];
if (next === undefined || next.startsWith('--')) {
out[key] = true;
i += 1;
} else {
out[key] = next;
i += 2;
}
} else {
i += 1;
}
}
return out;
}
/**
* CLI main entry point.
*
* @param {string[]} [argv]
* @returns {Promise<void>}
*/
export async function main(argv = process.argv.slice(2)) {
const args = parseArgs(argv);
if (!args.date || typeof args.date !== 'string') {
process.stderr.write('Usage: node scripts/scrape-doceo-votes.js --date YYYY-MM-DD [--slug <slug>] [--output-dir <dir>] [--term <N>] [--dry-run]\n');
process.exit(2);
}
const date = String(args.date);
const slug = args.slug ? String(args.slug) : 'unknown';
const term = args.term ? parseInt(String(args.term), 10) : EP_CURRENT_TERM;
const dryRun = args['dry-run'] === true;
const outputDir = args['output-dir']
? String(args['output-dir'])
: path.join(process.cwd(), 'analysis', 'daily', date, slug, 'data');
const result = await scrapeDoceoVotes({ date, outputDir, term, dryRun });
if (result.publicationLag) {
process.stdout.write(
JSON.stringify({
status: 'publication-lag',
publicationLag: true,
url: result.url,
date,
term,
}) + '\n',
);
process.exit(0);
}
if (!result.success) {
process.stderr.write(`Error: ${result.error ?? 'unknown'}\n`);
process.exit(1);
}
process.stdout.write(
JSON.stringify({
status: 'ok',
voteCount: result.voteCount,
outputFile: result.outputFile,
url: result.url,
}) + '\n',
);
}
// Standard ESM CLI guard — only run main() when this file is the entry point.
const isMain =
typeof process !== 'undefined' &&
process.argv[1] !== undefined &&
(process.argv[1] === fileURLToPath(import.meta.url) ||
process.argv[1].endsWith('/scrape-doceo-votes.js'));
if (isMain) {
main().catch((err) => {
process.stderr.write(`Fatal: ${err}\n`);
process.exit(1);
});
}
/* c8 ignore stop */
|