forked from denysdovhan/chernivtsi-outages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
116 lines (100 loc) · 3.22 KB
/
index.ts
File metadata and controls
116 lines (100 loc) · 3.22 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
import { JSDOM } from 'jsdom';
import { parse } from 'date-fns';
import dateFns from 'date-fns-tz';
import { promises as fs } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { markdownTable } from 'markdown-table';
type Connectivity = 'on' | 'off' | 'unknown';
type OutagesTable = Connectivity[][];
const URL = 'https://oblenergo.cv.ua/shutdowns/';
const TZ = 'Europe/Kiev';
interface OutagesData {
table: OutagesTable;
date: Date;
}
function mapValues(text: string): Connectivity {
switch (text.trim().toLowerCase()) {
case 'з':
return 'on';
case 'в':
return 'off';
case 'мз':
return 'unknown';
default:
return 'unknown';
}
}
function mapValuesToEmoji(value: Connectivity): '🟩' | '🟥' | '🟨' {
switch (value) {
case 'on':
return '🟩';
case 'off':
return '🟥';
case 'unknown':
return '🟨';
default:
return '🟨';
}
}
function tableToEmoji(table: OutagesTable) {
return table.map((row) => row.map(mapValuesToEmoji));
}
function toTimestamp(date: Date): string {
// Convert date to a local timezone
return dateFns.format(dateFns.utcToZonedTime(date, TZ), 'yyyy-MM-dd', {
timeZone: TZ,
});
}
async function fetchData(): Promise<OutagesData> {
const dom = await JSDOM.fromURL(URL);
const groupRowsEl = dom.window.document.querySelectorAll('#gsv div[id^=inf]');
const dateEl = dom.window.document.querySelector('#gsv ul p');
const date = parse(dateEl?.textContent?.trim()!, 'dd.MM.yyyy', Date.now());
const table = Array.from(groupRowsEl, (row) =>
Array.from(row.children, (cell) => mapValues(cell.textContent ?? ''))
);
return { date, table };
}
export function dataToMarkdown(table: OutagesTable, timestamp: string) {
const hours = Array(24)
.fill(0)
.map((_, index) => `${index + 1}`);
const headers = ['Group/Hour', ...hours];
const tableWithEmojis = tableToEmoji(table);
const tableWithDescription = [
headers,
...tableWithEmojis.map((row, index) => [`${index + 1}`, ...row]),
];
const tableString = markdownTable(tableWithDescription, {
padding: false,
});
return `# ${timestamp}\n\n${tableString}`;
}
export function dataToCSV(table: OutagesTable) {
return table
.map((row) => row.join(','))
.join('\n')
.concat('\n');
}
export async function storeData({ table, date }: OutagesData) {
const timestamp = toTimestamp(date);
const json = JSON.stringify({ date: timestamp, data: table });
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const readme = dataToMarkdown(table, timestamp);
const csv = dataToCSV(table);
const diskOperations = ['latest', `history/${timestamp}`].map(async (dir) => {
const dest = path.join(dirname, '/outages', dir);
await fs.mkdir(dest, { recursive: true });
await fs.writeFile(path.join(dest, `data.json`), json);
await fs.writeFile(path.join(dest, `data.csv`), csv);
await fs.writeFile(path.join(dest, `readme.md`), readme);
});
return Promise.all(diskOperations);
}
export async function extractOutages() {
const data = await fetchData();
console.log(dataToMarkdown(data.table, toTimestamp(data.date)));
return storeData(data);
}