Skip to content

Instantly share code, notes, and snippets.

@kevincolten
Last active August 16, 2018 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolten/192c40180dd28bbc6d3935405f904f79 to your computer and use it in GitHub Desktop.
Save kevincolten/192c40180dd28bbc6d3935405f904f79 to your computer and use it in GitHub Desktop.
Insightly Scripts
.env
node_modules

TODO

  • Leads
  • Contacts
  • Custom Fields
  • Attachments
  • Notes
  • Emails
  • Comments
  • Tasks
  • Events
const fs = require('fs');
const fetch = require('node-fetch');
const btoa = require('btoa');
let headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY),
'Accept-Encoding': 'gzip'
};
let insightlyData = [];
let fileName = 'insightlyLeads.json';
fetchInsightlyData(0, 'Leads');
function fetchInsightlyData(skip) {
fetch(`https://api.insight.ly/v2.3/Leads/?top=500&skip=${skip}`, {
method: 'GET',
headers
})
.then(res => {
return res.json();
})
.then(leads => {
if (typeof leads === 'string') {
console.log(leads);
if (leads.contains('Second')) {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY_2),
'Accept-Encoding': 'gzip'
};
return fetchInsightlyLeads();
} else {
return process.exit();
}
}
insightlyData.push(...leads);
console.log(`fetched ${leads.length} leads`);
if (leads.length === 500) {
setTimeout(() => {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY),
'Accept-Encoding': 'gzip'
};
fetchInsightlyLeads(skip + 500);
}, 200);
} else {
console.log('fetching insightly lead attachments');
fetchInsightlyLeadAttachments(0);
}
}).catch(error => {
console.log(error);
});
}
function fetchInsightlyLeadAttachments(idx) {
const lead = insightlyLeads[idx];
fetch(`https://api.insight.ly/v2.3/Leads/${lead.LEAD_ID}/FileAttachments`, {
method: 'GET',
headers
})
.then(res => {
return res.json();
})
.then(attachments => {
if (typeof attachments === 'string') {
console.log(attachments);
if (attachments.contains('Second')) {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY_2),
'Accept-Encoding': 'gzip'
};
return fetchInsightlyLeadAttachments(idx);
} else {
return process.exit();
}
}
if ((idx + 1) % 50 === 0) console.log(`fetched attachments for ${idx + 1} leads`);
insightlyLeads[idx].ATTACHMENTS = attachments;
fs.writeFileSync(fileName, JSON.stringify(insightlyLeads, null, 2), 'utf8');
if (idx < insightlyLeads.length - 1) {
setTimeout(() => {
headers = {
'Authorization': 'Basic ' + btoa(process.env.INSIGHTLY_API_KEY),
'Accept-Encoding': 'gzip'
};
fetchInsightlyLeadAttachments(++idx);
}, 200);
} else {
fs.writeFileSync(fileName, JSON.stringify(insightlyLeads, null, 2), 'utf8');
process.exit();
}
})
.catch(error => {
console.log(error);
});
}
{
"name": "insightly-scripts",
"version": "1.0.0",
"main": "index.js",
"repository": "git@gist.github.com:192c40180dd28bbc6d3935405f904f79.git",
"author": "Kevin Colten <kevincolten@gmail.com>",
"license": "MIT",
"dependencies": {
"btoa": "^1.2.1",
"dotenv-cli": "^1.4.0",
"node-fetch": "^2.2.0"
}
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
btoa@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/btoa/-/btoa-1.2.1.tgz#01a9909f8b2c93f6bf680ba26131eb30f7fa3d73"
cross-spawn@^4.0.0:
version "4.0.2"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-4.0.2.tgz#7b9247621c23adfdd3856004a823cbe397424d41"
dependencies:
lru-cache "^4.0.1"
which "^1.2.9"
dotenv-cli@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/dotenv-cli/-/dotenv-cli-1.4.0.tgz#e8e80830ed88b48a03b5eb7ec26147ca717f7409"
dependencies:
cross-spawn "^4.0.0"
dotenv "^4.0.0"
minimist "^1.1.3"
dotenv@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-4.0.0.tgz#864ef1379aced55ce6f95debecdce179f7a0cd1d"
isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
lru-cache@^4.0.1:
version "4.1.3"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-4.1.3.tgz#a1175cf3496dfc8436c156c334b4955992bce69c"
dependencies:
pseudomap "^1.0.2"
yallist "^2.1.2"
minimist@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
node-fetch@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.2.0.tgz#4ee79bde909262f9775f731e3656d0db55ced5b5"
pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"
which@^1.2.9:
version "1.3.1"
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
dependencies:
isexe "^2.0.0"
yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment