-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
208 lines (157 loc) · 6.74 KB
/
update.js
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
const fs = require('fs');
const fetch = require('node-fetch');
const moment = require('moment');
async function fetchActiveCases() {
const jsonUrl = 'https://opendata.ecdc.europa.eu/covid19/casedistribution/json';
const { records: activeCases } = await (await fetch(jsonUrl)).json();
const activeCasesNormalized = activeCases.map(({ dateRep, cases, deaths, countriesAndTerritories }) => ({
dateString: dateRep,
cases: +cases,
deaths: +deaths,
tests: 0,
countryName: getCountryName(countriesAndTerritories),
}));
maybeAddMissingDays(activeCasesNormalized);
fs.writeFileSync('./data/global-cases-and-deaths.js', 'window.data = ' + JSON.stringify(activeCasesNormalized, null, 4));
}
function maybeAddMissingDays(activeCases) {
const romaniaEntries = activeCases.filter((x) => x.countryName == 'Romania');
const todayString = moment().format('DD/MM/YYYY');
const maybeMissingDays = [todayString, '05/03/2020', '03/03/2020'];
maybeMissingDays.forEach((maybeMissingDay) => {
if (!romaniaEntries.find((x) => x.dateString == maybeMissingDay)) {
const currentHour = moment().format('HH');
if (+currentHour > 11 || maybeMissingDay !== todayString) {
activeCases.push({ countryName: 'Romania', dateString: maybeMissingDay, deaths: 0, recoveries: 0, cases: 0 });
}
}
});
}
function getCountryName(countryName) {
if (countryName.toLowerCase().startsWith('united_states_of_amer')) {
return 'USA';
}
if (countryName.startsWith('cases')) {
return 'Diamond Princess';
}
return countryName.replace(/\_/g, ' ');
}
function bumpRomaniaVersion() {
const indexHtmlPath = './index.html';
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8');
const myRegexp = /data\/romania\.js\?v=([0-9]*)/;
const [_, version] = myRegexp.exec(indexHtml);
const replacedIndexHtml = indexHtml.replace(myRegexp, 'data/romania.js?v=' + (+version + 1));
fs.writeFileSync(indexHtmlPath, replacedIndexHtml);
}
function bumpRomaniaDeathsVersion() {
const indexHtmlPath = './index.html';
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8');
const myRegexp = /data\/romania-deaths\.js\?v=([0-9]*)/;
const [_, version] = myRegexp.exec(indexHtml);
const replacedIndexHtml = indexHtml.replace(myRegexp, 'data/romania-deaths.js?v=' + (+version + 1));
fs.writeFileSync(indexHtmlPath, replacedIndexHtml);
}
function bumpGlobalCasesVersion() {
const indexHtmlPath = './index.html';
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8');
const myRegexp = /data\/global-cases-and-deaths\.js\?v=([0-9]*)/;
const [_, version] = myRegexp.exec(indexHtml);
const replacedIndexHtml = indexHtml.replace(myRegexp, 'data/global-cases-and-deaths.js?v=' + (+version + 1));
fs.writeFileSync(indexHtmlPath, replacedIndexHtml);
}
function bumpAppJsVersion() {
const indexHtmlPath = './index.html';
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8');
const myRegexp = /js\/app\.js\?v=([0-9]*)/;
const result = myRegexp.exec(indexHtml);
const [_, version] = result;
const replacedIndexHtml = indexHtml.replace(myRegexp, 'js/app.js?v=' + (+version + 1));
fs.writeFileSync(indexHtmlPath, replacedIndexHtml);
}
function bumpAppCssVersion() {
const indexHtmlPath = './index.html';
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8');
const myRegexp = /css\/styles\.css\?v=([0-9]*)/;
const [_, version] = myRegexp.exec(indexHtml);
const replacedIndexHtml = indexHtml.replace(myRegexp, 'css/styles.css?v=' + (+version + 1));
fs.writeFileSync(indexHtmlPath, replacedIndexHtml);
}
async function addTodayCases() {
// const targetDay = '17';
const targetDay = moment().format('D');
const { todayCases, todayRecoveries, todayDeaths, todayTests } = await crawlDayCases(targetDay);
if (!todayCases) {
return;
}
let romaniaDailyCasesString = fs.readFileSync('./data/romania.js', 'utf8');
const romaniaDailyCases = JSON.parse(romaniaDailyCasesString.replace('window.romaniaData = ', ''));
const todayKey = `${targetDay}/${+moment().format('MM')}/${+moment().format('YYYY')}`;
const totalCasesSoFar = Object.keys(romaniaDailyCases)
.map((x) => romaniaDailyCases[x].cases)
.reduce((a, b) => a + b);
const totalRecoveriesSoFar = Object.keys(romaniaDailyCases)
.map((x) => romaniaDailyCases[x].recoveries)
.reduce((a, b) => a + b);
const totalDeathsSoFar = Object.keys(romaniaDailyCases)
.map((x) => romaniaDailyCases[x].deaths)
.reduce((a, b) => a + b);
const totalTestsSoFar = Object.keys(romaniaDailyCases)
.map((x) => romaniaDailyCases[x].tests)
.reduce((a, b) => a + b);
romaniaDailyCases[todayKey] = {
cases: todayCases - totalCasesSoFar,
recoveries: todayRecoveries - totalRecoveriesSoFar,
deaths: todayDeaths - totalDeathsSoFar,
tests: todayTests - totalTestsSoFar,
};
const newRomaniaDailyCases = {};
[todayKey, ...Object.keys(romaniaDailyCases)].forEach((key) => {
newRomaniaDailyCases[key] = romaniaDailyCases[key];
});
const newRomaniaDailyCasesString = 'window.romaniaData = ' + JSON.stringify(newRomaniaDailyCases, null, 2);
fs.writeFileSync('./data/romania.js', newRomaniaDailyCasesString);
}
async function crawlDayCases(day = +moment().format('D')) {
const months = [
'ianuarie',
'februarie',
'martie',
'aprilie',
'mai',
'iunie',
'iulie',
'august',
'septembrie',
'octombrie',
'noiembrie',
'decembrie',
];
const month = months[+moment().format('MM') - 1];
const year = +moment().format('YYYY');
const url = `http://stirioficiale.ro/informatii/buletin-de-presa-${day}-${month}-${year}-ora-13-00`;
const pageHtml = await (await fetch(url)).text();
if (pageHtml.includes('nu am găsit pagina')) {
console.log('[' + moment().format('DD/MM/YYYY HH:mm:ss') + '] ' + 'Article not published yet. Page URL: ' + url);
return {};
} else {
fetchActiveCases();
bumpAppJsVersion();
bumpAppCssVersion();
bumpGlobalCasesVersion();
bumpRomaniaVersion();
bumpRomaniaDeathsVersion();
const todayCases = +pageHtml.match(/au fost confirmate ([0-9+\.]+) (de )?cazuri/)[1].replace(/\./g, '');
console.log('totalCases', todayCases);
const todayRecoveries = +pageHtml.match(/([0-9+\.]+) (de )?pacienți au fost declarați vindecați/)[1].replace(/\./g, '');
console.log('totalRecoveries', todayRecoveries);
const todayDeaths = +pageHtml
.match(/Până astăzi, ([0-9+\.]+) (de )?persoane diagnosticate cu infecție cu SARS – CoV – 2 au decedat./)[1]
.replace(/\./g, '');
console.log('totalDeaths', todayDeaths);
const todayTests = +pageHtml.match(/au fost prelucrate ([0-9+\.]+)/)[1].replace(/\./g, '');
console.log('totalTests', todayTests);
return { todayCases, todayRecoveries, todayDeaths, todayTests };
}
}
addTodayCases();