const fs = require("fs"); const { parse } = require("csv-parse"); const axios = require('axios'); var array = []; const apiKey = 'RfmxChNLLodO6M0Z88BwG9Xyu' function wait(ms) { return new Promise(r => setTimeout(r, ms)); } function getUserProps(xApiKey, person) { axios({ method: 'get', url: 'https://api.northpass.com/v2/properties/people/' + person, headers: { 'accept': 'application/json', 'x-api-key': xApiKey, 'content-type': 'application/json' } }) .then(res => { info = '"' + res.data.data.attributes.properties.account_name + '"' + ';' + '"' + res.data.data.attributes.properties.company + '"' + '\n' fs.appendFile('harriUserProps.csv', info, function(err) { if (err) throw err; console.log('Saved!'); return 'saved' }); }) .catch(err => { console.log(err); }); }; function fetchPeople(xApiKey, users, num) { let index = num; axios({ method: 'get', url: 'https://api.northpass.com/v2/people?filter[' + users[index][0] + ']=' + encodeURIComponent(users[index][1]), headers: { 'accept': 'application/json', 'x-api-key': xApiKey, } }) .then(res => { if (index < users.length - 1) { console.log(index) console.log(users[index][0], users[index][1], res.data.data[0].id) getUserProps(xApiKey, res.data.data[0].id) index++; wait(300) fetchPeople(xApiKey, users, index) wait(300) } else { getUserProps(xApiKey, res.data.data[0].id) console.log("complete") } }) .catch(err => { console.log(err); }); }; function csvReader(someArray, filePath) { fs.createReadStream(filePath) .pipe(parse({ delimiter: ',', columns: true })) .on("data", function(row) { if (row['Email'] == '') { someArray.push(['sso_uid', row['SSO UID']]) } else { someArray.push(['email', row['Email']]) } }) .on("error", function(error) { console.log(error.message); }) .on("end", async function() { console.log("finished"); fetchPeople(apiKey, someArray, 0) }) } csvReader(array, "Untitled spreadsheet - harri-academy-people.csv")