-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulkLookups.js
152 lines (127 loc) · 5.59 KB
/
bulkLookups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//////////////////////////////////////////////////////////////////////////////////////////////////
//// Call script with: node bulkLookups.js input-file.csv ////
//////////////////////////////////////////////////////////////////////////////////////////////////
var sid = process.env.BULK_SID;
var auth = process.env.BULK_AUTH;
var lookupType = process.env.BULK_LOOKUP_TYPE; // line type only right now
var bulkCSVHeaders = process.env.BULK_CSV_HEADERS;
var errorOutput = process.env.BULK_ERROR_OUTPUT;
if (!(process.argv[2] && sid && auth && lookupType && bulkCSVHeaders)) {
console.log("Please pass in the input filename.");
process.exit(1);
} else {
console.log("You are running " + lookupType + " Lookups");
}
var pnCsv = process.argv[2];
var concurrency = 10;
var request = require('request');
var fs = require('fs');
var async = require('async');
var rp = require('request-promise');
var parse = require('csv-parse');
var transform = require('stream-transform');
var stream = fs.createWriteStream('output-' + lookupType + '.csv');
var errorStream = fs.createWriteStream('errors-' + lookupType + '.csv');
var errorList = [];
var uriSuffix;
if (lookupType === "line_type_intelligence")
uriSuffix = "?Fields=line_type_intelligence";
else if (lookupType === "caller_name")
uriSuffix = "?Fields=caller_name";
else if (lookupType === "validation")
uriSuffix = "?Fields=validation";
if (lookupType === "line_type_intelligence" && bulkCSVHeaders) {
stream.write("phone_number, carrier_name, carrier_type, mcc, mnc, country_code\n");
}
if (lookupType === "caller_name" && bulkCSVHeaders) {
stream.write("phone_number,caller_name,caller_type\n");
}
if (lookupType === "validation" && bulkCSVHeaders) {
stream.write("phone_number, calling_country_code, national_format, country_code\n");
}
var q = async.queue(function (task, callback) {
var uri = 'https://lookups.twilio.com/v2/PhoneNumbers/' + task.phoneNumber + uriSuffix;
var options = {
json: true,
uri: uri,
method: 'GET',
auth: {
user: sid,
pass: auth
}
};
rp(options)
.then(function (response) {
if (lookupType === "line_type_intelligence") {
if (!response.valid) {
console.log("Invalid number: ", task.phoneNumber);
errorStream.write(task.phoneNumber + ": Invalid Number" + "\n");
}
else if(response.line_type_intelligence.carrier_name == null) {
console.log("error with this number: ", task.phoneNumber);
errorStream.write(task.phoneNumber + response.line_type_intelligence.error_code + "\n");
}
else if (response.line_type_intelligence.carrier_name) {
var carrierName = response.line_type_intelligence.carrier_name.replace(",", "");
stream.write(response.phone_number + ',' + response.line_type_intelligence.carrier_name + ',' + response.line_type_intelligence.type + ',' + response.line_type_intelligence.mobile_country_code + ',' + response.line_type_intelligence.mobile_network_code + ',' + response.country_code + '\n');
}
}
else if (lookupType === "caller_name") {
if (!response.valid) {
console.log("Invalid number: ", task.phoneNumber);
errorStream.write(task.phoneNumber + ": Invalid Number" + "\n");
}
else {
if (response.caller_name.caller_name) {
var caller_name = response.caller_name.caller_name.replace(",", "");
}
stream.write(response.phone_number + ',' + caller_name + ',' + response.caller_name.caller_type + '\n');
}
}
else if (lookupType === "validation") {
if(response.valid) {
stream.write(response.phone_number + ',' + response.calling_country_code + ',' + response.national_format + ',' + response.country_code + '\n');
}
else {
console.log("Invalid number: ", task.phoneNumber);
errorStream.write(task.phoneNumber + ": Invalid Number" + "\n");
}
}
callback();
})
.catch(function (err) {
if (task)
if (errorOutput) {
console.log('big error: ', err);
stream.write("error" + '\n');
}
if (!task.retries) {
task.retries = 1;
q.push(task);
}
else if (task.retries < 5) {
task.retries++;
q.push(task);
} else {
console.log("error with this number: ", task.phoneNumber);
errorStream.write(task.phoneNumber + "invalid\n");
errorList.push([task, 'Maximum retries exceeded!']);
}
callback();
});
}, concurrency);
q.drain = function () {
console.log('All numbers looked up.');
if (errorList.length > 0 && errorOutput) {
if(errorOutput) {
console.log('Errors:\n', errorList);
}
console.log("Check errors-" + lookupType + ".csv for numbers and error codes");
console.log("See a list of errors codes at https://www.twilio.com/docs/api/errors");
}
};
fs.createReadStream(pnCsv)
.pipe(parse({delimiter: ':'}))
.on('data', function (csvrow) {
q.push({phoneNumber: csvrow[0]});
});