small script changes and notes

This commit is contained in:
Norm Rasmussen
2023-05-12 16:45:50 -04:00
parent aa64ae8ba5
commit 5c8b9a7a3b
29 changed files with 695 additions and 1702 deletions

87
Scripts/csvTest.js Normal file
View File

@ -0,0 +1,87 @@
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")