const express = require('express') const bodyParser = require("body-parser"); var fs = require('fs'); var readline = require('readline'); var google = require('googleapis'); var googleAuth = require('google-auth-library'); const app = express() const router = express.Router(); router.use(bodyParser.urlencoded({ extended: false })); router.use(bodyParser.json()); require('dotenv').config() const PEOPLE_DB = process.env.PEOPLE_DB.split('/')[5]; const PAIRS_LIST = process.env.PAIRS_LIST.split('/')[5]; var SCOPES = ['https://www.googleapis.com/auth/spreadsheets']; var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; var TOKEN_PATH = TOKEN_DIR + 'sheets.googleapis.com-nodejs-quickstart.json'; var oauth2Client = null; var pairsForSave = []; var lastRow = null; //last row with names function executeAPI(callback1,callback2){ fs.readFile('client_secret.json', function processClientSecrets(err, content) { console.log("reading client secret"); if (err) { console.log('Error loading client secret file: ' + err); return; } // Authorize a client with the loaded credentials, then call the // Google Sheets API. authorize(JSON.parse(content), callback1, callback2); }); } function authorize(credentials, callback1, callback2){ var clientSecret = credentials.installed.client_secret; var clientId = credentials.installed.client_id; var redirectUrl = credentials.installed.redirect_uris[0]; var auth = new googleAuth(); oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); // Check if we have previously stored a token. fs.readFile(TOKEN_PATH, function(err, token) { console.log(TOKEN_PATH); if (err) { getNewToken(callback1, callback2); } else { oauth2Client.credentials = JSON.parse(token); callback1(callback2); } }); } function getNewToken(callback1, callback2) { var authUrl = oauth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES }); console.log('Authorize this app by visiting this url: ', authUrl); var rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question('Enter the code from that page here: ', function(code) { rl.close(); oauth2Client.getToken(code, function(err, token) { if (err) { console.log('Error while trying to retrieve access token', err); return; } oauth2Client.credentials = token; storeToken(token); callback1(callback2); }); }); } function storeToken(token) { try { fs.mkdirSync(TOKEN_DIR); } catch (err) { if (err.code != 'EEXIST') { throw err; } } console.log(JSON.stringify(token)); fs.writeFile(TOKEN_PATH, JSON.stringify(token)); console.log('Token stored to ' + TOKEN_PATH); } function getPairs(callback){ auth=oauth2Client; ranges = ['2017!A:A','2017!B:B']; //get all rows with names var sheets = google.sheets('v4'); sheets.spreadsheets.values.batchGet({ auth: auth, spreadsheetId: PAIRS_LIST, ranges: ranges }, function(err, result) { if(err) { // Handle error console.log(err); return null; } else { const pairs = []; lastRow = result.valueRanges[0].values.length; for (let i=0;i{ if ((pair.name1==names[i] && pair.name2==names[j])||(pair.name1==names[j] && pair.name2==names[i])){ console.log("Par pronaden !"); console.log(names[i] + " i " + names[j]); found=true; return; } }); if (!found){ usedNames.filter((name)=>{ if (names[i] === name || names[j] === name){ console.log("Ime vec koristeno"); found=true; return; } }); } if (!found){ next_i=true; newPairs.push({name1:names[i], name2:names[j]}); usedNames.push(names[i]); usedNames.push(names[j]); break; } } } if (next_i) continue; } pairsForSave=newPairs; callback.send(newPairs); }); }); //Napravi par -> Provjeri da li postoji -> Snimi ili ponovi postupak } function SavePairs(callback){ if (lastRow== null) return; auth=oauth2Client; const values_column1 = []; const values_column2 = []; for (let i=0;i{ pairsForSave=[]; lastRow=null; MakePairs(resp); }); app.get('/savePairs', (req,resp)=>{ SavePairs(resp); //resp.send({result:true}); }); app.listen(3005, function () { console.log('server na portu 3005'); })