From 84b22cb92925f822da58d65e8176e974749a1048 Mon Sep 17 00:00:00 2001 From: Mark Rapson Date: Tue, 18 Apr 2023 19:31:45 +0100 Subject: [PATCH] Initial Release --- .gitignore | 5 +++ config-example.json | 37 +++++++++++++++++++++ index.js | 79 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 14 ++++++++ readme.md | 8 +++++ 5 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 config-example.json create mode 100644 index.js create mode 100644 package.json create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ebf53a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.vscode +node_modules +output +package-lock.json +config.json diff --git a/config-example.json b/config-example.json new file mode 100644 index 0000000..440f3d6 --- /dev/null +++ b/config-example.json @@ -0,0 +1,37 @@ +{ + "outputFolder": "output", + "apis": [ + { + "apiKey": "--- API key ---", + "documents": [ + { + "googleDocId": "--- Google Sheet ID ---", + "sheets": [ + "-- Sheet Name ---", + "-- 2nd Sheet Name ---" + ], + "pollRate": 1500 + }, + { + "googleDocId": "--- 2nd Google Sheet ID ---", + "sheets": [ + "Sheet3" + ], + "pollRate": 10000 + } + ] + }, + { + "apiKey": "--- 2nd API Key ---", + "documents": [ + { + "googleDocId": "--- 3rd Google Sheet ID ---", + "sheets": [ + "Sponsors" + ], + "pollRate": 5000 + } + ] + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..6d76eda --- /dev/null +++ b/index.js @@ -0,0 +1,79 @@ +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; + config.apis.forEach(function (document, d) { + var totalRate = 0; + document.documents.forEach(function (item, i) { + count += 1; + totalRate += (60 / (item.pollRate / 1000)); + config.apis[d].documents[i].sheets.itemNo = count; + }); + 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 == count) { + resolve(); + } + }); +}) + +check.then(function () { + console.clear(); + process.stdout.cursorTo(0, 1); + console.log(`Ctrl+C to kill the application.`); + config.apis.forEach(api => { + api.documents.forEach(docs => { + const doc = new GoogleSpreadsheet(docs.googleDocId); + doc.useApiKey(api.apiKey); + + setInterval(function () { + (async function () { + await doc.loadInfo(); + if (!fs.existsSync(`${config.outputFolder}/${doc.title}/`)) { + fs.mkdirSync(`${config.outputFolder}/${doc.title}/`, { recursive: true }); + } + docs.sheets.forEach(sheet => { + (async function () { + downloadCSV = await doc.sheetsByTitle[sheet].downloadAsCSV(); + fs.writeFile(`${config.outputFolder}/${doc.title}/${sheet}.csv`, downloadCSV, function (err) { + if (err) { + return console.log(err); + } + const d = new Date(); + process.stdout.cursorTo(0, docs.sheets.itemNo + 2); + process.stdout.clearLine(); + console.log(`"${doc.title} - ${sheet}" last updated at ${pad(d.getHours(), 2)}:${pad(d.getMinutes(), 2)}:${pad(d.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; +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f0b444b --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "google-sheets-rate-assistant", + "version": "0.0.1", + "main": "index.js", + "scripts": { + "run": "node index.js" + }, + "author": "", + "license": "ISC", + "description": "Assists in keeping requests under the free rate limit for applications such as vMix.", + "dependencies": { + "google-spreadsheet": "^3.3.0" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..4beb665 --- /dev/null +++ b/readme.md @@ -0,0 +1,8 @@ +Node must be installed on your system + +open Powershell in the directory +run 'npm install' + +run the app with 'node index.js' + +Lowest PollRate for a single item is recommended at 1200 due to various latencies that could occur. \ No newline at end of file