forked from danscan/githubdownfall.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.ts
More file actions
145 lines (118 loc) · 3.4 KB
/
scrape.ts
File metadata and controls
145 lines (118 loc) · 3.4 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
/**
* Backfill script: scrapes githubstatus.com history pages for all incidents
* from 2025 onward and upserts them into the local SQLite database.
*
* Run once with: bun scrape.ts
*/
import { Database } from "bun:sqlite";
const HISTORY_URL = "https://us.githubstatus.com/history";
const INCIDENT_URL = "https://us.githubstatus.com/api/v2/incidents";
const CUTOFF_YEAR = 2025;
// Pages 1-5 cover Feb 2026 back to Dec 2024 (3 months per page).
const PAGES = [1, 2, 3, 4, 5];
interface HistoryIncident {
code: string;
impact: string;
name: string;
}
interface HistoryMonth {
days: number;
incidents: HistoryIncident[];
name: string;
starts_on: number;
year: number;
}
// –
// Database
// –
const db = new Database("incidents.db");
db.run(`
CREATE TABLE IF NOT EXISTS incidents (
id TEXT PRIMARY KEY,
name TEXT,
status TEXT,
created_at TEXT,
updated_at TEXT,
resolved_at TEXT,
impact TEXT,
shortlink TEXT,
started_at TEXT
)
`);
const existing = new Set(
db
.prepare("SELECT id FROM incidents")
.all()
.map((row: Record<string, string>) => row.id)
);
const insert = db.prepare(`
INSERT OR REPLACE INTO incidents
(id, name, status, created_at, updated_at, resolved_at, impact, shortlink, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
// –
// Scrape history pages
// –
/** Extract the embedded months JSON from a history page's HTML. */
function parseMonths(html: string): HistoryMonth[] {
const match = html.match(/"months":\[(.*?)\],"show_component_filter/);
if (!match) return [];
const decoded = match[1].replace(/"/g, '"').replace(/'/g, "'");
return JSON.parse(`[${decoded}]`);
}
/** Fetch full incident details from the individual incident API. */
async function fetchIncident(code: string) {
const res = await fetch(`${INCIDENT_URL}/${code}.json`);
if (!res.ok) return null;
const data = await res.json();
return data.incident;
}
// –
// Main
// –
let codes: string[] = [];
console.log("Scraping history pages...");
for (const page of PAGES) {
const res = await fetch(`${HISTORY_URL}?page=${page}`);
const html = await res.text();
const months = parseMonths(html);
for (const month of months) {
// Skip months before our cutoff
if (month.year < CUTOFF_YEAR) continue;
for (const incident of month.incidents) {
if (!existing.has(incident.code)) {
codes.push(incident.code);
}
}
console.log(` ${month.name} ${month.year}: ${month.incidents.length} incidents`);
}
}
codes = [...new Set(codes)];
console.log(`\nFound ${codes.length} incidents to backfill.\n`);
// Fetch and insert in batches to be polite
const BATCH = 5;
let inserted = 0;
for (let i = 0; i < codes.length; i += BATCH) {
const batch = codes.slice(i, i + BATCH);
const results = await Promise.all(batch.map(fetchIncident));
for (const incident of results) {
if (!incident) continue;
insert.run(
incident.id,
incident.name,
incident.status,
incident.created_at,
incident.updated_at,
incident.resolved_at,
incident.impact,
incident.shortlink,
incident.started_at
);
inserted++;
}
process.stdout.write(` ${Math.min(i + BATCH, codes.length)}/${codes.length}\r`);
}
console.log(`\nDone. Inserted ${inserted} incidents.`);
console.log(
`Total in DB: ${db.prepare("SELECT COUNT(*) as count FROM incidents").get().count}`
);