-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.ts
60 lines (50 loc) · 2.12 KB
/
fetch.ts
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
import { exit } from 'process';
import fs from 'fs'
import { extractFromSource, makeUniq, categorize } from "./utils"
import request from 'request'
import deepmerge from 'deepmerge';
const URL = 'https://ceb.mu/customer-corner/power-outage-information'
const path = './data/power-outages.json'
const pathLatest = './data/power-outages.latest.json'
request(URL, function (error, _, body) {
if (error) {
console.error('error:', error);
exit(1)
}
console.log('Fetched raw data from CEB');
console.log('Pre-extracting data ...')
const result = /var arDistrictLocations = ({".+"});/gm.exec(body);
if (!result || result.length !== 2) {
console.error('error: result from pre-extraction is null or malformed');
exit(1);
}
const preExtracted = JSON.parse(result?.[1] || '');
console.log('Pre-extracted data');
// The newly fetched data from CEB Website.
console.log('Extracting data ...');
const newData = extractFromSource(preExtracted);
console.log('Extracted data');
// Create aggregate data file if not exist.
if (!fs.existsSync(path)) {
console.log('Creating aggregate data file ...');
fs.writeFileSync(path, JSON.stringify(newData));
console.log('Created aggregate data file');
}
console.log('Updating aggregate data file ...');
const rawdata = fs.readFileSync(path); // open file
const oldData = JSON.parse(rawdata.toString());
const mergedData = deepmerge(oldData, newData) // Merge old and new data
const uniq = makeUniq(mergedData); // remove duplicate data & empty values
fs.writeFileSync(path, JSON.stringify(uniq));
console.log('Updated aggregate data file');
// Create latest data file if not exist.
if (!fs.existsSync(pathLatest)) {
console.log('Creating latest data file ...');
fs.writeFileSync(pathLatest, JSON.stringify(newData));
console.log('Created latest data file');
}
console.log('Updating latest data file ...');
fs.writeFileSync(pathLatest, JSON.stringify(categorize(newData)));
console.log('Updated latest data file');
exit(0);
});