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 | 1x 1x 1x 1x 1x 1x 1x 10x 15x 15x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | #!/usr/bin/env tsx
/**
* @module scripts/rss-watch
* @description Watches a Riksdagen RSS feed and emits PIR-ready match signals for tracked dok_ids.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { decodeHtmlEntities } from './html-utils.js';
export interface RssItem {
readonly title: string;
readonly link: string;
readonly guid: string;
readonly pubDate?: string;
readonly description?: string;
}
export interface RssSignal extends RssItem {
readonly matchedDokIds: readonly string[];
}
export interface RssWatchResult {
readonly schemaVersion: '1.0';
readonly fetchedAt: string;
readonly feedUrl: string;
readonly trackedDokIds: readonly string[];
readonly itemCount: number;
readonly signalCount: number;
readonly status: 'ok' | 'error';
readonly signals: readonly RssSignal[];
readonly notes?: string;
}
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = path.resolve(__dirname, '..');
export const DEFAULT_RSS_OUTPUT = path.join(REPO_ROOT, 'data', 'rss-watch.json');
// Default to the unscoped Riksdagen RSS feed so RSS PIR matching works for any
// document type (propositions, motions, questions, interpellations, betänkanden).
// Callers can scope to a specific doktyp via `--feed-url`.
export const DEFAULT_RSS_FEED_URL = 'https://data.riksdagen.se/rss/dokumentlista/';
export const RSS_REQUEST_TIMEOUT_MS = 15_000;
const ITEM_RE = /<item>([\s\S]*?)<\/item>/gi;
// Only accept RSS root elements (<rss> or <channel>) because parseRssItems()
// only extracts <item> elements. An Atom <feed> would pass validation but yield
// zero parsed entries, silently dropping signals.
const RSS_ROOT_RE = /<(rss|channel)[\s>]/i;
function decodeXml(value: string | undefined): string {
return decodeHtmlEntities((value ?? '').trim());
}
function getTag(block: string, tagName: string): string | undefined {
const match = new RegExp(`<${tagName}>([\\s\\S]*?)</${tagName}>`, 'i').exec(block);
return match?.[1] ? decodeXml(match[1]) : undefined;
}
export function parseRssItems(xml: string): RssItem[] {
const items: RssItem[] = [];
for (const match of xml.matchAll(ITEM_RE)) {
const block = match[1] ?? '';
const title = getTag(block, 'title');
const link = getTag(block, 'link');
const guid = getTag(block, 'guid') ?? link;
Iif (!title || !link || !guid) continue;
items.push({
title,
link,
guid,
pubDate: getTag(block, 'pubDate'),
description: getTag(block, 'description'),
});
}
return items;
}
function dokIdMatchesHaystack(haystack: string, dokId: string): boolean {
// Require a non-alphanumeric boundary on both sides of the dok_id so the
// tracked id `H901SFU1` does not falsely match an RSS item that references
// `H901SFU10` (i.e. a longer dok_id with the tracked id as a prefix).
const escaped = dokId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp(`(?:^|[^A-Z0-9])${escaped}(?:$|[^A-Z0-9])`).test(haystack);
}
export function buildRssSignals(items: readonly RssItem[], trackedDokIds: readonly string[]): RssSignal[] {
const normalizedIds = trackedDokIds
.map((value) => value.toUpperCase().trim())
.filter(Boolean);
Iif (normalizedIds.length === 0) return [];
return items.flatMap((item) => {
const haystack = `${item.title} ${item.link} ${item.guid} ${item.description ?? ''}`.toUpperCase();
const matchedDokIds = normalizedIds.filter((dokId) => dokIdMatchesHaystack(haystack, dokId));
if (matchedDokIds.length === 0) return [];
return [{ ...item, matchedDokIds }];
});
}
export async function watchRssFeed(
feedUrl = DEFAULT_RSS_FEED_URL,
trackedDokIds: readonly string[] = [],
fetchFn: typeof fetch = globalThis.fetch,
): Promise<RssWatchResult> {
const fetchedAt = new Date().toISOString();
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), RSS_REQUEST_TIMEOUT_MS);
try {
const response = await fetchFn(feedUrl, {
signal: controller.signal,
headers: {
Accept: 'application/rss+xml, application/xml, text/xml;q=0.9, */*;q=0.8',
'User-Agent': 'Mozilla/5.0 (compatible; Riksdagsmonitor rss-watch)',
},
});
Iif (!response.ok) {
return {
schemaVersion: '1.0',
fetchedAt,
feedUrl,
trackedDokIds,
itemCount: 0,
signalCount: 0,
status: 'error',
signals: [],
notes: `HTTP ${response.status} ${response.statusText}`,
};
}
const xml = await response.text();
// Defence-in-depth: a 2xx response that returns an HTML error page or a
// schema-changed payload would otherwise be silently treated as a healthy
// feed with zero items. Require the body to look like RSS XML
// (<rss> or <channel> root element).
Iif (!RSS_ROOT_RE.test(xml)) {
return {
schemaVersion: '1.0',
fetchedAt,
feedUrl,
trackedDokIds,
itemCount: 0,
signalCount: 0,
status: 'error',
signals: [],
notes: 'Response body does not look like RSS XML (no <rss> or <channel> root element)',
};
}
const items = parseRssItems(xml);
const signals = buildRssSignals(items, trackedDokIds);
return {
schemaVersion: '1.0',
fetchedAt,
feedUrl,
trackedDokIds,
itemCount: items.length,
signalCount: signals.length,
status: 'ok',
signals,
notes: trackedDokIds.length > 0 ? 'Emit signals to PIR consumer by reading signals[] from this artifact' : 'No tracked dok_ids configured',
};
} catch (error) {
return {
schemaVersion: '1.0',
fetchedAt,
feedUrl,
trackedDokIds,
itemCount: 0,
signalCount: 0,
status: 'error',
signals: [],
notes: error instanceof Error ? error.message : String(error),
};
} finally {
clearTimeout(timer);
}
}
export function persistRssWatch(result: RssWatchResult, outputPath = DEFAULT_RSS_OUTPUT): string {
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, `${JSON.stringify(result, null, 2)}\n`, 'utf8');
return outputPath;
}
function parseArgs(argv: readonly string[]): { feedUrl: string; trackedDokIds: string[]; output: string; persist: boolean } {
let feedUrl = DEFAULT_RSS_FEED_URL;
let trackedDokIds: string[] = [];
let output = DEFAULT_RSS_OUTPUT;
let persist = true;
for (let i = 2; i < argv.length; i++) {
const token = argv[i];
const next = argv[i + 1];
if (token === '--feed-url' && next) {
feedUrl = next;
i++;
continue;
}
if (token === '--dok-ids' && next) {
trackedDokIds = next.split(',').map((value) => value.trim()).filter(Boolean);
i++;
continue;
}
if (token === '--output' && next) {
output = next;
i++;
continue;
}
if (token === '--no-persist') {
persist = false;
}
}
return { feedUrl, trackedDokIds, output, persist };
}
async function main(): Promise<void> {
const args = parseArgs(process.argv);
const result = await watchRssFeed(args.feedUrl, args.trackedDokIds);
if (args.persist) {
persistRssWatch(result, args.output);
}
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}
Iif (path.resolve(fileURLToPath(import.meta.url)) === path.resolve(process.argv[1] ?? '')) {
main().catch((error: unknown) => {
console.error(`rss-watch: ${error instanceof Error ? error.message : String(error)}`);
process.exit(1);
});
}
|