-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoholdownloaddata.js
More file actions
519 lines (470 loc) · 16.4 KB
/
oholdownloaddata.js
File metadata and controls
519 lines (470 loc) · 16.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright (C) 2019 hetuw
// GNU General Public License version 2 https://www.gnu.org/licenses/gpl-2.0.txt
let isWin = process.platform === "win32";
let fileSeperator = '/';
if (isWin) fileSeperator = '\\';
const fs = require('fs');
const http = require('http');
let askForDate = false;
let forceUpdateMode = false; // if true will also update files that contain more bytes than they should have
let hideDownloadErrors = true; // errors can be harmless but scare people
var args = process.argv.slice(0);
for (var a = 2; a < args.length; a++) {
if (args[a].toLowerCase() === '-force') forceUpdateMode = true;
else if (args[a].toLowerCase() === '--force') forceUpdateMode = true;
else if (args[a].toLowerCase() === '-f') forceUpdateMode = true;
else if (args[a].toLowerCase() === 'force') forceUpdateMode = true;
else if (args[a].toLowerCase() === 'f') forceUpdateMode = true;
if (args[a].toLowerCase() === '-error') hideDownloadErrors = false;
else if (args[a].toLowerCase() === '--error') hideDownloadErrors = false;
else if (args[a].toLowerCase() === '-errors') hideDownloadErrors = false;
else if (args[a].toLowerCase() === '--errors') hideDownloadErrors = false;
else if (args[a].toLowerCase() === '-e') hideDownloadErrors = false;
else if (args[a].toLowerCase() === 'e') hideDownloadErrors = false;
else if (args[a].toLowerCase() === 'error') hideDownloadErrors = false;
else if (args[a].toLowerCase() === 'errors') hideDownloadErrors = false;
if (args[a].toLowerCase() === '-date') askForDate = true;
else if (args[a].toLowerCase() === '--date') askForDate = true;
else if (args[a].toLowerCase() === '-d') askForDate = true;
else if (args[a].toLowerCase() === 'date') askForDate = true;
else if (args[a].toLowerCase() === 'd') askForDate = true;
}
let updatesHappend = 0;
let bytesDownloaded = 0;
let filesDownloaded = 0;
let lastLogFilesDownloaded = -1;
let keepUpdating = true;
function createHttpOptions(link) {
let hostname;
let path;
if (link.indexOf("//") > -1) {
hostname = link.split('/')[2];
} else {
hostname = link.split('/')[0];
}
path = link.substring(link.indexOf(hostname)+hostname.length);
let options = {
hostname: hostname,
path: path,
timeout: 30000,
};
return options;
}
function getHttp( link ) {
return new Promise( function (resolve, reject) {
// -------------------------------------------------------------------
let options = createHttpOptions(link);
var data = '';
http.get(options, (resp) => {
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Return the result.
resp.on('end', () => {
filesDownloaded++;
bytesDownloaded += data.length;
resolve(data);
});
}).on("error", (err) => {
reject(err.message);
});
// -------------------------------------------------------------------
});
}
function getHttpSilent( link ) {
return new Promise( function (resolve, reject) {
// -------------------------------------------------------------------
let options = createHttpOptions(link);
var data = '';
http.get(options, (resp) => {
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Return the result.
resp.on('end', () => {
resolve(data);
});
}).on("error", (err) => {
reject(err.message);
});
// -------------------------------------------------------------------
});
}
const readline = require('readline');
async function getUserInput(question) {
return new Promise( function (resolve, reject) {
// -------------------------------------------------------------------
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
// -------------------------------------------------------------------
});
}
// -------------------------------------------------------------------
const maxDownloadsAtATime = 10000;
let downloadsInProgress = 0;
let intervDownloadQueue = null;
function DownloadInfo() {
this.link = null;
this.file = null;
}
var downloadQueue = [];
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
async function sleep(millis) {
return new Promise( resolve => {
setTimeout(resolve, millis);
});
}
function loopDownloadQueue() {
while (downloadsInProgress < maxDownloadsAtATime && downloadQueue.length > 0) {
let di = downloadQueue.shift();
downloadFile(di.link, di.file);
}
}
function addToDownloadQueue(link, file) {
let di = new DownloadInfo();
di.link = link;
di.file = file;
downloadQueue.push(di);
}
function downloadFile(link, file) {
downloadsInProgress++;
getHttp(link).then((data) => {
downloadsInProgress--;
fs.writeFile(file, data, (err) => {
if (err) console.log("ERROR: "+err);
});
}).catch( function (err) {
if (!hideDownloadErrors) console.log(err);
if (!hideDownloadErrors) console.log("ERROR while downloading "+link);
let waitSeconds = getRandomInt(7) + 7;
if (!hideDownloadErrors) console.log("Trying again in "+waitSeconds+" seconds ...");
if (!hideDownloadErrors) console.log(" ");
setTimeout( () => {
downloadsInProgress--;
downloadFile(link, file);
}, waitSeconds*1000);
});
}
async function keepDownloading(link) {
let success = false;
let data;
while (!success) {
try {
data = await getHttpSilent(link);
success = true;
} catch(err) {
console.log("ERROR while downloading "+link);
console.log(err);
let waitSeconds = getRandomInt(7) + 7;
console.log("Trying again in "+waitSeconds+" seconds ...");
await sleep(waitSeconds*1000);
}
}
return data;
}
// -------------------------------------------------------------------
const rootLink = "http://publicdata.onehouronelife.com/publicLifeLogData/";
const rootFolder = "oholData";
var date_begin = []; // contains 3 ints, year - month - day
var date_end = [];
var allLinks = []; // array of Links() - indices are servernames like "server12" or "bigserver2"
function Links() {
this.link = ""; // contains link to the list of data and name links
this.curseLink = ""; // contains link to the list of curse links
this.dateLinks = []; // contains link to text file that has the data - indices are dates like "2019_02_09"
this.dateLinksSize = []; // contains size of files in bytes
this.nameLinks = []; // contains link to text file that has the names - indices are dates like "2019_02_09"
this.nameLinksSize = []; // contains size of files in bytes
this.curseLinks = []; // contains link to text file that has the curses - indices are dates like "2019_02_09"
this.curseLinksSize = []; // contains size of files in bytes
}
main();
async function main() {
if (askForDate) {
await getBeginEndDates();
}
console.log("Downloading all data and saving it to '"+rootFolder+"'");
console.log("This may take a while... ");
console.log(" ");
await getAllLinks();
if (!fs.existsSync(rootFolder)){
fs.mkdirSync(rootFolder);
}
intervDownloadQueue = setInterval( loopDownloadQueue, 2000 );
let intervUpdateLog = setInterval(() => {
if (lastLogFilesDownloaded === filesDownloaded) return;
lastLogFilesDownloaded = filesDownloaded;
let percentProgress = (filesDownloaded/(filesDownloaded+downloadsInProgress+downloadQueue.length)*100).toFixed(2)+" %";
console.log(getTimeStr()+" - "+filesDownloaded+" files downloaded "+bytesReadable(bytesDownloaded)+", "+(downloadsInProgress+downloadQueue.length)+" missing - status: "+percentProgress);
if (downloadsInProgress <= 0) {
if (keepUpdating) {
lastLogFilesDownloaded = -1;
filesDownloaded = 0;
bytesDownloaded = 0;
if (forceUpdateMode && updatesHappend > 0) {
clearInterval(intervUpdateLog);
clearInterval(intervDownloadQueue);
console.log("Download complete!");
return;
}
console.log("Updating files ... ");
updateAllFiles();
updatesHappend++;
if (!keepUpdating) {
clearInterval(intervUpdateLog);
clearInterval(intervDownloadQueue);
console.log("Download complete!");
}
return;
}
clearInterval(intervUpdateLog);
clearInterval(intervDownloadQueue);
console.log("Download complete!");
}
}, 5000);
console.log("Downloading files ... ");
downloadAll();
}
async function getBeginEndDates() {
console.log("Input dates in this format 'YEAR_MONTH_DAY', for example '2019_01_23'");
let datesAreGood = false;
while (!datesAreGood) {
date_begin = await getDateFromUser('date_begin: ');
date_end = await getDateFromUser('date_end: ');
if (dateEqualsDate(date_begin, date_end) > 0) {
console.log("ERROR: date_begin is bigger than date_end "+getDateString(date_begin)+" > "+getDateString(date_end)+"\n");
} else datesAreGood = true;
}
console.log(" ");
}
async function getDateFromUser(question) {
let dateIsGood = false;
let date;
while (!dateIsGood) {
let dateStr = await getUserInput(question);
try {
date = stringToDate(dateStr);
let d = new Date(date[0]+'-'+date[1]+'-'+date[2]);
if (isNaN(d.getTime())) console.log("ERROR: Invalid date: "+dateStr+"\n");
else dateIsGood = true;
} catch (err) {
console.log("ERROR: Invalid date: "+dateStr+"\n");
}
}
return date;
}
function downloadAll() {
for (let server in allLinks) {
let serverFolder = rootFolder+fileSeperator+server;
fs.exists(serverFolder, (exists) => {
if (!exists) {
fs.mkdirSync(serverFolder);
}
downloadServerData(server, serverFolder);
});
}
}
function downloadServerData(server, serverFolder) {
for (let d in allLinks[server].dateLinks) {
let file = serverFolder+fileSeperator+d;
fs.exists(file, (exists) => {
if (!exists) addToDownloadQueue(allLinks[server].dateLinks[d], file);
});
}
for (let d in allLinks[server].nameLinks) {
let file = serverFolder+fileSeperator+d+"_names";
fs.exists(file, (exists) => {
if (!exists) addToDownloadQueue(allLinks[server].nameLinks[d], file);
});
}
for (let d in allLinks[server].curseLinks) {
let file = serverFolder+fileSeperator+d+"_curses";
fs.exists(file, (exists) => {
if (!exists) addToDownloadQueue(allLinks[server].curseLinks[d], file);
});
}
}
async function getAllLinks() {
console.log("Downloading links: "+rootLink+"\n");
let html_serverLinks = await keepDownloading(rootLink);
let serverLinkList = html_serverLinks.match(/\=\"lifeLog_.+?\.com\//g);
let curseLinkList = html_serverLinks.match(/\=\"curseLog_.+?\.com\//g);
for (var i in serverLinkList) {
let cLinkToList = String(serverLinkList[i]).substr(2);
let serverName = String(String(cLinkToList.match(/_.+?\./)).match(/[A-Za-z0-9]+/));
serverName = serverName.toLowerCase();
allLinks[serverName] = new Links();
allLinks[serverName].link = rootLink + cLinkToList;
console.log("Downloading links: "+allLinks[serverName].link);
let html_days = await keepDownloading(allLinks[serverName].link);
let lines = html_days.split('\n');
for (var l in lines) {
let line = lines[l];
let dayLink = line.match(/\=\"20.+?[^s]\.txt/g);
if (dayLink) {
dayLink = String(dayLink).substr(2);
let date = stringToDate(dayLink);
if (askForDate && !isValidDate(date)) continue;
let dateStr = getDateString(date);
allLinks[serverName].dateLinks[dateStr] = allLinks[serverName].link + dayLink;
allLinks[serverName].dateLinksSize[dateStr] = parseInt(line.match(/[0-9]+$/gm));
continue;
}
let nameLink = line.match(/\=\"20.+?_names\.txt/g);
if (nameLink) {
nameLink = String(nameLink).substr(2);
let date = stringToDate(nameLink);
if (askForDate && !isValidDate(date)) continue;
let dateStr = getDateString(date);
allLinks[serverName].nameLinks[dateStr] = allLinks[serverName].link + nameLink;
allLinks[serverName].nameLinksSize[dateStr] = parseInt(line.match(/[0-9]+$/gm));
}
}
}
for (var i in curseLinkList) {
let cLinkToList = String(curseLinkList[i]).substr(2);
let serverName = String(String(cLinkToList.match(/_.+?\./)).match(/[A-Za-z0-9]+/));
serverName = serverName.toLowerCase();
if (!allLinks[serverName]) {
console.log("Warning: Found curse log for server: "+serverName);
console.log("Warning: But did not find any other data about this server");
continue;
}
allLinks[serverName].curseLink = rootLink + cLinkToList;
console.log("Downloading curse links: "+allLinks[serverName].curseLink);
let html_days = await keepDownloading(allLinks[serverName].curseLink);
let lines = html_days.split('\n');
for (var l in lines) {
let line = lines[l];
let dayLink = line.match(/\=\"20.+?[^s]\.txt/g);
if (dayLink) {
dayLink = String(dayLink).substr(2);
let date = stringToDate(dayLink);
if (askForDate && !isValidDate(date)) continue;
let dateStr = getDateString(date);
allLinks[serverName].curseLinks[dateStr] = allLinks[serverName].curseLink + dayLink;
allLinks[serverName].curseLinksSize[dateStr] = parseInt(line.match(/[0-9]+$/gm));
continue;
}
}
}
let linkCount = 0;
for (var key in allLinks) linkCount++;
if (linkCount < 1) {
console.log("ERROR: Could not find any links");
process.exit();
}
console.log(" ");
}
async function updateAllFiles() {
let allFiles = [];
fs.readdirSync(rootFolder).forEach( (file) => {
allFiles[file] = new Links();
});
let updateComplete = true;
for (var server in allFiles) {
let dir = rootFolder + fileSeperator + server;
allFiles[server].link = dir;
fs.readdirSync(dir).forEach( (file) => {
let diff = 0;
const filePath = dir+fileSeperator+file;
const stats = fs.statSync(filePath);
if (file.indexOf("names") > -1) {
let d = file.replace("_names", "");
if (!allLinks[server].nameLinksSize[d]) return;
diff = allLinks[server].nameLinksSize[d] - stats.size;
if ((!forceUpdateMode && diff > 0) || (forceUpdateMode && diff !== 0)) {
updateComplete = false;
if (diff > 0) console.log("updating: "+server+" "+d+" -> missing "+bytesReadable(diff));
else console.log("updating: "+server+" "+d+" -> too much "+bytesReadable(diff*-1));
addToDownloadQueue(allLinks[server].nameLinks[d], filePath);
}
} else if (file.indexOf("curse") > -1) {
if (!allLinks[server].curseLinksSize[file]) return;
diff = allLinks[server].curseLinksSize[file] - stats.size;
if ((!forceUpdateMode && diff > 0) || (forceUpdateMode && diff !== 0)) {
updateComplete = false;
if (diff > 0) console.log("updating: "+server+" "+file+" -> missing "+bytesReadable(diff));
else console.log("updating: "+server+" "+file+" -> too much "+bytesReadable(diff*-1));
addToDownloadQueue(allLinks[server].curseLinks[file], filePath);
}
} else {
if (!allLinks[server].dateLinksSize[file]) return;
diff = allLinks[server].dateLinksSize[file] - stats.size;
if ((!forceUpdateMode && diff > 0) || (forceUpdateMode && diff !== 0)) {
updateComplete = false;
if (diff > 0) console.log("updating: "+server+" "+file+" -> missing "+bytesReadable(diff));
else console.log("updating: "+server+" "+file+" -> too much "+bytesReadable(diff*-1));
addToDownloadQueue(allLinks[server].dateLinks[file], filePath);
}
}
});
}
if (updateComplete) keepUpdating = false;
}
function getTimeStr() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var minute = date.getMinutes();
minute = (minute < 10 ? "0" : "") + minute;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return hour+":"+minute+":"+sec;
}
function isValidDate(date) {
if (dateEqualsDate(date, date_begin) < 0) return false;
if (dateEqualsDate(date, date_end) > 0) return false;
return true;
}
// returns 0 if they are equal, 1 if dateA is bigger, -1 if dateA is smaller
// dateA = [ 2018, 11, 23 ]; // for example
function dateEqualsDate(dateA, dateB) {
for (var d in dateA) {
if (dateA[d] > dateB[d]) return 1;
if (dateA[d] < dateB[d]) return -1;
}
return 0;
}
function getDateString(date) {
var str = "";
str = date[0] + "_";
if (date[1] < 10) str += 0;
str += date[1] + "_";
if (date[2] < 10) str += 0;
str += date[2];
return str;
}
function stringToDate(dateStr) {
let splitted = dateStr.split('_');
for (var i = 0; i < 3; i++) {
splitted[i] = splitted[i].match(/[0-9]+/);
}
let date = [ parseInt(splitted[0]) , parseInt(splitted[1]) , parseInt(splitted[2]) ];
return date;
}
const storageUnits = {
'TB': 1000000000000,
'GB': 1000000000,
'MB': 1000000,
'KB': 1000,
'B': 1
}
function bytesReadable(bytes) {
for (var s in storageUnits) {
if (bytes >= storageUnits[s]) {
return (bytes/storageUnits[s]).toFixed(2)+s;
}
}
return bytes+"B";
}