Skip to content

Instantly share code, notes, and snippets.

@mknospe
Last active April 25, 2019 16:04
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 mknospe/36aafc99ddcdeaf3edb4096fe8bfd099 to your computer and use it in GitHub Desktop.
Save mknospe/36aafc99ddcdeaf3edb4096fe8bfd099 to your computer and use it in GitHub Desktop.
NodeJS script to lookup carrier information in parallel using the paid service twilio. (https://www.twilio.com/docs/api/lookups) The data is read from a CSV-file, parsed, processed and written to the output csv-file.
// Install dependencies
// npm i -s node-fetch csv-writer csvtojson
const fetch = require('node-fetch');
const csvToJson = require('csvtojson');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
function lookup(phonenumber) {
const sid = '__YOUR_TWILIO_SID__';
const token = '__YOUR_TWILIO_AUTH_TOKEN__';
const url = `https://${sid}:${token}@lookups.twilio.com/v1/PhoneNumbers/${phonenumber}?Type=carrier&countryCode=US`;
return fetch(url).then(res => res.json()).then(data => {
const {carrier = {}} = data || {};
const {name = ''} = carrier;
return name;
}).catch(function(e) {
console.log(e);
});
}
async function append(data, file) {
const csvWriter = createCsvWriter({
path: file,
header: [
{id: 'GID', title: 'GID'},
{id: 'Mobile', title: 'Mobile'},
{id: 'Phone', title: 'Phone'},
{id: 'Lead Status', title: 'Lead Status'},
{id: 'Lead Creation Datetime', title: 'Lead Creation Datetime'},
{id: 'CarrierMobile', title: 'Carrier Mobile'},
{id: 'CarrierPhone', title: 'Carrier Phone'},
],
append: true,
});
return csvWriter.writeRecords(data).then(() => console.log('Append to file ...ok'));
}
function createSet(arr, type) {
return arr.filter(item => item[type]).map(item => item[type]);
}
async function lookupAndAppendCarrierData(leadData, type) {
const set = createSet(leadData, type);
const nums = set.map(item => lookup(item));
const procNums = await Promise.all(nums);
let j = 0;
for (let i = 0; i < leadData.length; i++) {
const lead = leadData[i];
if (lead[type]) {
lead['Carrier'+type] = procNums[j];
j++
}
}
return leadData;
}
async function processChunk(leadData, fileOutput) {
leadData = await lookupAndAppendCarrierData(leadData, 'Phone');
leadData = await lookupAndAppendCarrierData(leadData, 'Mobile');
await append(leadData, fileOutput);
}
async function run() {
const file = 'leads_2019';
const fileInput = `${file}.csv`;
const fileOutput = `${file}_done.csv`;
const leadData = await csvToJson().fromFile(fileInput);
const chunkSize = 200;
const startOffset = 0;
const stopAfterNumberOfLines = startOffset * chunkSize + 20000;
const numChunks = Math.ceil(leadData.length / chunkSize);
for (var i = startOffset; i < numChunks; i++) {
const start = i * chunkSize;
const end = (i + 1) * chunkSize;
const chunk = leadData.slice(start, end);
console.log(`Processing chunk ${start} - ${end} (${i})`);
await processChunk(chunk, fileOutput);
console.log(`> Done (${i})`);
if (end >= stopAfterNumberOfLines) {
console.log(`>> Stopped at ${stopAfterNumberOfLines} <<`);
break;
}
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment