forked from Protonull/IceniaGov2023
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscraper-senate-records.ts
More file actions
441 lines (366 loc) · 14.6 KB
/
scraper-senate-records.ts
File metadata and controls
441 lines (366 loc) · 14.6 KB
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
import {
Client,
GatewayIntentBits,
ForumChannel,
ThreadChannel,
ChannelType
} from 'discord.js';
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
import * as yaml from 'js-yaml';
dotenv.config();
const BOT_TOKEN = process.env.DISCORD_BOT_TOKEN;
// Republic of Icenia Senate Records Forum
// NOTE: Replace this with the actual Senate forum ID if different
const FORUM_CHANNEL_ID = '1071577869505011822';
const NEWS_DIR = path.join(__dirname, 'src', 'content', 'news');
const STATUS_FILE = path.join(__dirname, 'src', 'data', 'scraper-status.yml');
interface LegislationData {
fullTitle: string;
link: string;
content: string;
threadId: string;
createdAt: Date;
sponsorUsername: string;
}
// Month names for formatting
const MONTH_NAMES = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
// Get existing thread IDs from news markdown files
function getExistingThreadIds(): Set<string> {
const existingIds = new Set<string>();
if (!fs.existsSync(NEWS_DIR)) {
return existingIds;
}
const files = fs.readdirSync(NEWS_DIR);
for (const file of files) {
if (!file.endsWith('.md')) continue;
const filePath = path.join(NEWS_DIR, file);
const content = fs.readFileSync(filePath, 'utf-8');
// 1. Try to extract from frontmatter discord_thread_id
const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---/);
if (frontmatterMatch) {
try {
const yamlData = yaml.load(frontmatterMatch[1]) as any;
if (yamlData && yamlData.discord_thread_id) {
existingIds.add(String(yamlData.discord_thread_id));
continue;
}
} catch (e) {
// Fallback to regex if YAML parsing fails
}
}
// 2. Fallback: Extract thread ID from Discord link
const linkMatch = content.match(/discord\.com\/channels\/\d+\/(\d+)/);
if (linkMatch) {
existingIds.add(linkMatch[1]);
}
}
return existingIds;
}
// Generate a slug from the CLEAN headline
function generateSlug(text: string): string {
return text
.trim()
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-')
.substring(0, 50);
}
// Helper to format date as YYYY-MM-DD HH:MM:SS +00:00
function formatFullDate(date: Date): string {
const pad = (n: number) => n.toString().padStart(2, '0');
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds} +00:00`;
}
// Sanitize content by escaping Discord-style mentions
function sanitizeContent(content: string): string {
if (!content) return content;
return content.replace(/<([@#][!&]?\d+)>/g, '<$1>');
}
// Format a headline into Title Case while keeping small words lowercase (unless first word).
function formatHeadline(text: string): string {
if (!text) return text;
const smallWords = new Set([
'a','an','the','and','but','or','for','nor','on','at','to','from','by','of','in','into','with','over','per'
]);
// Normalize whitespace and lowercase everything first
const tokens = text.trim().toLowerCase().split(/\s+/);
const titled = tokens.map((w, i) => {
if (i !== 0 && smallWords.has(w)) return w;
return w.charAt(0).toUpperCase() + w.slice(1);
});
return titled.join(' ');
}
// Create markdown file for a legislation
function getSenateTermForDate(targetDate: Date): number | null {
const NEWS_DIR = path.join(__dirname, 'src', 'content', 'news');
if (!fs.existsSync(NEWS_DIR)) return null;
const files = fs.readdirSync(NEWS_DIR).filter(f => f.endsWith('.md'));
let bestTerm: number | null = null;
let bestDate: Date | null = null;
for (const file of files) {
const filePath = path.join(NEWS_DIR, file);
const content = fs.readFileSync(filePath, 'utf-8');
const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---/);
if (!frontmatterMatch) continue;
let fm: any;
try {
fm = yaml.load(frontmatterMatch[1]) as any;
} catch (e) {
continue;
}
if (!fm) continue;
// Only consider senate election posts that have a numeric term
if (!(fm.changetype === 'senate-election' || fm.changetype === 'senate-byelection') || typeof fm.term !== 'number') {
continue;
}
// Determine the post date from frontmatter or filename
let postDate: Date | null = null;
if (fm.date) {
try { postDate = new Date(fm.date); } catch (e) { postDate = null; }
}
if (!postDate) {
// Try to parse leading YYYY-MM-DD from filename
const m = file.match(/^(\d{4}-\d{2}-\d{2})/);
if (m) postDate = new Date(m[1]);
}
if (!postDate) continue;
if (postDate.getTime() <= targetDate.getTime()) {
if (!bestDate || postDate.getTime() > bestDate.getTime()) {
bestDate = postDate;
bestTerm = fm.term;
}
}
}
return bestTerm;
}
function createMarkdownFile(data: LegislationData): void {
const date = data.createdAt;
const dateStr = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
const fullTimestamp = formatFullDate(date);
const monthYear = `${MONTH_NAMES[date.getMonth()]} ${date.getFullYear()}`;
// Strip "Res.", "02-08", etc from headline
const rawHeadline = data.fullTitle.replace(/^[^\d]*\d{2}-\d{2}[:\s—-]*\s*/i, '').trim() || data.fullTitle;
const headline = formatHeadline(rawHeadline);
const slug = generateSlug(headline);
const filename = `${dateStr}-${slug}.md`;
const filePath = path.join(NEWS_DIR, filename);
if (fs.existsSync(filePath)) {
console.log(` Skipping (file exists): ${filename}`);
return;
}
const safeContent = sanitizeContent(data.content);
let termNumber = getSenateTermForDate(date);
// Fallback overrides for known terms (YYYY-MM => term number)
if (!termNumber) {
const overrides: Record<string, number> = {
'2026-01': 43
};
const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`;
if (overrides[key]) termNumber = overrides[key];
}
const footer = termNumber ? `[Passed by Term ${termNumber} Senate](${data.link})` : `[Passed by the ${monthYear} Senate](${data.link})`;
const markdown = `---
changetolaw: true
layout: "@layouts/news/act.astro"
institution: senate
headline: Passing the ${headline}
date: ${fullTimestamp}
discord_thread_id: ${data.threadId}
excerpt: Sponsored by ${data.sponsorUsername}
document:
type: markdown
value: |
AUTOMATICALLY SCRAPED CONTENT:
${safeContent.split('\n').map(line => ' ' + line).join('\n')}
changes: []
icon: /assets/images/law_stock.jpeg
---
${footer}
`;
fs.writeFileSync(filePath, markdown, 'utf-8');
console.log(` Created: ${filename}`);
}
// Update scraped items YAML and status
function updateScrapedItemsYaml(results: LegislationData[], runStartTime: Date): void {
const yamlPath = path.join(__dirname, 'src', 'data', 'scraped-items.yml');
let data: any = { items: [] };
if (fs.existsSync(yamlPath)) {
const yamlContent = fs.readFileSync(yamlPath, 'utf-8');
const loaded = yaml.load(yamlContent) as any;
if (Array.isArray(loaded)) {
data.items = loaded;
} else if (loaded && Array.isArray(loaded.items)) {
data = loaded;
}
}
const existingIds = new Set(data.items.map((item: any) => String(item.id)));
for (const res of results) {
if (!existingIds.has(res.threadId)) {
data.items.push({
id: res.threadId,
title: res.fullTitle,
date: res.createdAt.toISOString().substring(0, 10),
checked: false
});
}
}
const newYamlContent = yaml.dump(data);
fs.writeFileSync(yamlPath, newYamlContent, 'utf-8');
console.log(`Updated ${yamlPath} with ${results.length} new items`);
// Update status file
let status: any = {};
if (fs.existsSync(STATUS_FILE)) {
status = yaml.load(fs.readFileSync(STATUS_FILE, 'utf-8')) || {};
}
status.senate_last_scraped_at = runStartTime.toISOString();
fs.writeFileSync(STATUS_FILE, yaml.dump(status), 'utf-8');
console.log(`Updated ${STATUS_FILE} with new senate_last_scraped_at: ${status.senate_last_scraped_at}`);
}
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
]
});
client.once('clientReady', async () => {
console.log(`Logged in as ${client.user?.tag}`);
try {
await scrapeForum();
} catch (error) {
console.error('Error during scraping:', error);
}
await client.destroy();
});
async function scrapeForum() {
const runStartTime = new Date();
const channel = await client.channels.fetch(FORUM_CHANNEL_ID);
if (!channel || channel.type !== ChannelType.GuildForum) {
console.error("The ID provided is not a Forum Channel.");
return;
}
const forumChannel = channel as ForumChannel;
console.log(`Scanning Senate Forum: ${forumChannel.name}`);
// Load last scraped date from status file
let dateCutoff = new Date('2026-01-10');
if (fs.existsSync(STATUS_FILE)) {
const status = yaml.load(fs.readFileSync(STATUS_FILE, 'utf-8')) as any;
if (status?.senate_last_scraped_at) {
dateCutoff = new Date(status.senate_last_scraped_at);
}
}
console.log(`--------------------------------------------------`);
console.log(`CUTOFF DATE: ${dateCutoff.toISOString()}`);
console.log(`(Threads archived or created before this will be skipped)`);
console.log(`--------------------------------------------------\n`);
const existingThreadIds = getExistingThreadIds();
console.log(`Found ${existingThreadIds.size} existing scraped threads`);
const passedTag = forumChannel.availableTags.find(tag => tag.name.toLowerCase() === "passed");
if (!passedTag) {
console.error("No 'Passed' tag found in forum channel.");
return;
}
const passedTagId = passedTag.id;
const results: LegislationData[] = [];
const processedIds = new Set<string>();
// 1. Process Active Threads
const activeThreads = await forumChannel.threads.fetchActive();
console.log(`Found ${activeThreads.threads.size} active threads.`);
for (const thread of activeThreads.threads.values()) {
await processThread(thread, false);
processedIds.add(thread.id);
}
// 2. Process Archived Threads (with pagination to minimize API calls and stop early)
console.log(`\nFetching archived threads...`);
let hasMore = true;
let lastTimestamp: Date | undefined = undefined;
let archivedCount = 0;
while (hasMore) {
const archived = await forumChannel.threads.fetchArchived({
limit: 50,
before: lastTimestamp
});
if (archived.threads.size === 0) {
console.log("No more archived threads found.");
break;
}
archivedCount += archived.threads.size;
console.log(`Checking batch of ${archived.threads.size} archived threads (Total checked: ${archivedCount})...`);
for (const thread of archived.threads.values()) {
if (processedIds.has(thread.id)) continue;
const archiveTimestamp = thread.archiveTimestamp || 0;
const createdAt = thread.createdAt || new Date();
// Use archiveTimestamp for cutoff if available, otherwise fallback to createdAt
const compareTime = archiveTimestamp || createdAt.getTime();
if (compareTime < dateCutoff.getTime()) {
console.log(`Reached threads older than cutoff (${new Date(compareTime).toISOString()}). Stopping archived scan.`);
hasMore = false;
break;
}
await processThread(thread, true);
processedIds.add(thread.id);
lastTimestamp = createdAt;
}
if (!archived.hasMore) hasMore = false;
}
async function processThread(thread: ThreadChannel, isArchived: boolean) {
const createdAt = thread.createdAt || new Date();
// 1. Date cutoff check (for active threads or fallback)
// If it's archived, we already checked the cutoff in the loop
if (!thread.archived && createdAt < dateCutoff) return;
// 2. Already scraped check
if (existingThreadIds.has(thread.id)) {
console.log(`-> Skipping (already scraped): ${thread.name}`);
return;
}
// 3. 'Passed' tag check
if (!thread.appliedTags?.includes(passedTagId)) {
console.log(`-> Skipping (not Passed): ${thread.name}`);
return;
}
const title = thread.name;
let content = "";
let sponsor = "[Unknown]";
try {
const starterMsg = await thread.fetchStarterMessage();
if (starterMsg) {
content = starterMsg.content;
sponsor = starterMsg.author?.tag ?? sponsor;
} else {
content = "[No text content found]";
}
} catch (e) {
content = "[Unable to fetch message]";
}
results.push({
fullTitle: title,
link: `https://discord.com/channels/${thread.guildId}/${thread.id}`,
content: content,
threadId: thread.id,
createdAt: createdAt,
sponsorUsername: sponsor,
});
const statusLabel = isArchived ? "[ARCHIVED]" : "[ACTIVE]";
console.log(`-> Processed ${statusLabel}: ${title}`);
}
console.log(`\n\n--- EXTRACTION COMPLETE: Found ${results.length} new matches ---`);
if (!fs.existsSync(NEWS_DIR)) {
fs.mkdirSync(NEWS_DIR, { recursive: true });
}
for (const res of results) {
createMarkdownFile(res);
}
updateScrapedItemsYaml(results, runStartTime);
console.log('\nDone!');
}
client.login(BOT_TOKEN);