Last active
January 31, 2018 14:42
-
-
Save abhijitmehta/f0d4d404a54dad9c5f5c2baf9531f0bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Download the Node helper library from twilio.com/docs/node/install . You can do this by doing “ sudo npm install twilio -g —save " | |
// These vars are your accountSid and authToken from twilio.com/user/account | |
var accountSid = process.env.TWILIO_ACCOUNT_SID; | |
var authToken = process.env.TWILIO_AUTH_TOKEN ; | |
var LookupsClient = require('twilio').LookupsClient; | |
var client = new LookupsClient(accountSid, authToken); | |
var fs = require('fs'); | |
var readline = require('readline'); | |
var errFileCreated='N'; | |
var reTryFileCreated='N'; | |
var limit = require("simple-rate-limiter"); | |
if (process.argv.length <= 3) | |
{ | |
console.log("\n\n\tUsage: " + __filename + " <inputfilePathAndName> " + " <outputfilePathAndName>\n"); | |
process.exit(-1); | |
} | |
else | |
{ | |
inputfile = process.argv[2]; | |
outputfile = process.argv[3] ; | |
fs.stat(inputfile, function(err, stats) | |
{ | |
if (err) | |
{ | |
console.log("\n\tFile :: " + process.argv[2] + " does not exist\n") ; | |
process.exit(-1); | |
} | |
if(stats) | |
{ | |
//console.log(`stats: ${JSON.stringify(stats)}`); | |
outputStream = fs.createWriteStream(outputfile,{ | |
flags: 'w', | |
defaultEncoding: 'utf8', | |
fd: null, | |
mode: 0o666, | |
autoClose: true | |
} | |
); | |
outputStream.write("OriginallyProviderNumber|CustomerNo|AddressISOCode|type|name|mobile_network_code|mobile_country_code|national_format|inernational_format|country_code\n"); | |
} | |
}); | |
} | |
const line = readline.createInterface( | |
{ | |
input: fs.createReadStream(inputfile) | |
}); | |
var doOneLookup = function(currLine,inputPhoneNumber) | |
{ | |
return new Promise (function(resolve, reject) | |
{ | |
client.phoneNumbers(inputPhoneNumber) | |
.get( | |
{ | |
type: 'carrier' | |
}, function(error, number) | |
{ | |
if(number) | |
{ | |
outputStream.write(currLine.replace(/,/g , "|")+"|"+number.carrier.type+"|"+ number.carrier.name+"|"+ number.carrier.mobile_network_code+"|"+ number.carrier.mobile_country_code+"|"+ number.national_format+"|"+ number.phone_number+"|"+ number.country_code+'\n'); | |
resolve("OK"); | |
} | |
if(error && error["status"] === "ENOTFOUND") | |
{ | |
//console.log("Arghh!!") | |
reject("GOT_ENOTFOUND"); | |
} | |
if(error && error["status"] != "ENOTFOUND") | |
{ | |
//console.log("Error Code" + error["status"]); | |
if(errFileCreated === 'N') | |
{ | |
errFile=outputfile+'.err'; | |
errFileStream = fs.createWriteStream(errFile,{ | |
flags: 'w', | |
defaultEncoding: 'utf8', | |
fd: null, | |
mode: 0o666, | |
autoClose: true | |
} | |
); | |
errFileStream.write("OriginallyProviderNumber|CustomerNo|AddressISOCode|<error>\n"); | |
errFileStream.write(currLine.replace(/,/g , "|")+"|"+JSON.stringify(error) +">\n"); | |
errFileCreated='Y'; | |
} | |
else | |
{ | |
errFileStream.write(currLine.replace(/,/g , "|")+"|"+JSON.stringify(error) +">\n"); | |
} | |
resolve("OK"); | |
} | |
} | |
) ; | |
}); | |
} | |
function AttemptLookup(maxRetries, lineContent,phoneNumber,doOneLookup) | |
{ | |
//console.log("attempting" + phoneNumber); | |
return doOneLookup(lineContent,phoneNumber).catch(function(err) { | |
if (maxRetries <= 0) | |
{ | |
if(reTryFileCreated === 'N') | |
{ | |
reTryFile=outputfile+'.reTry'; | |
reTryFileStream = fs.createWriteStream(reTryFile,{ | |
flags: 'w', | |
defaultEncoding: 'utf8', | |
fd: null, | |
mode: 0o666, | |
autoClose: true | |
} | |
); | |
reTryFileStream.write("OriginallyProviderNumber,CustomerNo,AddressISOCode\n"); | |
reTryFileStream.write(lineContent+"\n"); | |
reTryFileCreated='Y'; | |
} | |
else | |
{ | |
reTryFileStream.write(lineContent+"\n"); | |
} | |
throw err; | |
} | |
return AttemptLookup(maxRetries - 1, lineContent,phoneNumber,doOneLookup); | |
}); | |
} | |
var doLookup = function() | |
{ | |
limit(line.on('line', function (inputRecord) | |
{ | |
//console.log('Line from file:', currLine); | |
currPhoneNumber = inputRecord.split(",")[0]; | |
//console.log('PhoneNumber from Line:', inputPhoneNumber); | |
AttemptLookup(3,inputRecord,currPhoneNumber,doOneLookup); | |
})).to(100).per(1000); | |
} | |
doLookup(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment