-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (83 loc) · 3.77 KB
/
index.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
const { GoogleSpreadsheet } = require('google-spreadsheet');
const fs = require('fs');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
const config = require('./config.json');
const check = new Promise(function (resolve) {
var count = 0,
position = {};
config.apis.forEach(function (document, d) {
var totalRate = 0;
document.documents.forEach(function (item, i) {
item.sheets.forEach(function (sh, s) {
count += 1;
position[d + "" + i + "" + s] = count;
});
totalRate += (60 / (item.pollRate / 1000));
//config.apis[d].documents[i].sheets.itemNo = count;
});
config.position = position;
var stdin = process.openStdin();
if (totalRate > 50) {
console.log('\n---------- RATE LIMIT WARNING ----------');
readline.question(`\nYour poll rate will be ${totalRate} per minute, this is above the recommendation of 50 per minute.\nIf the GoogleAPI limit is reached (60 per min on free) you will receive no updates until a break period has passed.\n(API: ${document.apiKey})\n\nAre you sure you want to continue? [y/n]: `, answer => {
if (answer == 'y') {
readline.close();
resolve();
} else {
process.exit();
}
});
} else if (config.apis.length - 1 == d) {
resolve();
}
});
})
check.then(function () {
console.clear();
process.stdout.cursorTo(0, 1);
console.log(`Ctrl+C to kill the application.`);
config.apis.forEach(function (api, d) {
api.documents.forEach(function (docs, i) {
const doc = new GoogleSpreadsheet(docs.googleDocId, { apiKey: api.apiKey});
setInterval(function () {
(async function () {
await doc.loadInfo();
var titleClean = doc.title.replace(/[^a-zA-Z0-9 ]/g, '');
if (!fs.existsSync(`${config.outputFolder}/${titleClean}/`)) {
fs.mkdirSync(`${config.outputFolder}/${titleClean}/`, { recursive: true });
}
docs.sheets.forEach(function (sheet, s) {
(async function () {
try {
downloadCSV = await doc.sheetsByTitle[sheet].downloadAsCSV();
} catch (error) {
console.log(`Sheet '${sheet}' does not exist in document '${doc.title}', check your config file.`);
process.exit();
}
fs.writeFile(`${config.outputFolder}/${titleClean}/${sheet.replace(/[^a-zA-Z0-9 ]/g)}.csv`, downloadCSV, function (err) {
if (err) {
return console.log(err);
}
const date = new Date();
process.stdout.cursorTo(0, config.position[d + "" + i + "" + s] + 2);
process.stdout.clearLine();
console.log(`"${doc.title} - ${sheet}" last updated at ${pad(date.getHours(), 2)}:${pad(date.getMinutes(), 2)}:${pad(date.getSeconds(), 2)}`);
process.stdout.cursorTo(31, 1);
});
}());
});
}());
}, docs.pollRate)
});
});
}, function (err) {
console.log(err);
})
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}