Last active
January 23, 2017 23:17
-
-
Save tfogo/3d47cb259273775a0e5ccbaa78282486 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var twilio = require('twilio'); | |
var primality = require('primality'); | |
var countries = require('./countries'); | |
const accountSid = process.env.TWILIO_AC_SID; | |
const authToken = process.env.TWILIO_AUTH_TOKEN; | |
var client = require('twilio')(accountSid, authToken); | |
function getRandomInt(min, max) { | |
// max and min numst be integers | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
function getPrimePhoneNumbers(isoCountry, areaCode) { | |
// An array to hold all the promises we get from Twilio API requests | |
let promises = []; | |
// Get the length of the country code so we know what to cut off from the number | |
let countryCodeLen = countries[isoCountry].code.length; | |
for (let i = 0; i < 20; i++) { | |
// Make a call to the available phone numbers API for the US | |
let twilioPromise = client.availablePhoneNumbers(isoCountry).local.list({ | |
Contains: getRandomInt(10,99), | |
areaCode: areaCode | |
}); | |
promises.push(twilioPromise); | |
} | |
// phonePromise will resolve when all of the promises in the promises array resolve | |
let phonePromise = Promise.all(promises); | |
// assign the resolution of phonePromise to a variable | |
primeNumberPromise = phonePromise.then(data => { | |
var verifiedPrimes = []; | |
data.forEach(twilioObject => { | |
twilioObject.available_phone_numbers.forEach(e => { | |
if (primality(e.phone_number.slice(countryCodeLen)) | |
&& !verifiedPrimes.includes(e.phone_number.slice(countryCodeLen))) { | |
// Slice off the country code using the length calculated above | |
verifiedPrimes.push(e.phone_number.slice(countryCodeLen)); | |
} | |
}) | |
}); | |
return verifiedPrimes; | |
}); | |
return primeNumberPromise; | |
} | |
console.log('US numbers:'); | |
getPrimePhoneNumbers('US', 650).then(data => console.log(data)); | |
// Search by area code only works for US and Canada. Twilio just ignores it for other countries | |
console.log('UK numbers:'); | |
getPrimePhoneNumbers('GB').then(data => console.log(data)); |
This file contains hidden or 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
module.exports = { | |
US: { | |
name: "United States", | |
code: "+1", | |
}, | |
AU: { | |
name: "Australia", | |
code: "+61", | |
}, | |
AT: { | |
name: "Austria", | |
code: "+43", | |
}, | |
BE: { | |
name: "Belgium", | |
code: "+32", | |
}, | |
BR: { | |
name: "Brazil", | |
code: "+55", | |
}, | |
BG: { | |
name: "Bulgaria", | |
code: "+359", | |
}, | |
CA: { | |
name: "Canada", | |
code: "+1", | |
}, | |
CL: { | |
name: "Chile", | |
code: "+56", | |
}, | |
CY: { | |
name: "Cyprus", | |
code: "+357", | |
}, | |
CZ: { | |
name: "Czech Republic", | |
code: "+420", | |
}, | |
DK: { | |
name: "Denmark", | |
code: "+45", | |
}, | |
DO: { | |
name: "Dominican Republic", | |
code: "+1829", | |
}, | |
SV: { | |
name: "El Salvador", | |
code: "+503", | |
}, | |
EE: { | |
name: "Estonia", | |
code: "+372", | |
}, | |
FI: { | |
name: "Finland", | |
code: "+358", | |
}, | |
FR: { | |
name: "France", | |
code: "+33", | |
}, | |
DE: { | |
name: "Germany", | |
code: "+49", | |
}, | |
GR: { | |
name: "Greece", | |
code: "+30", | |
}, | |
HK: { | |
name: "Hong Kong", | |
code: "+852", | |
}, | |
HU: { | |
name: "Hungary", | |
code: "+36", | |
}, | |
ID: { | |
name: "Indonesia", | |
code: "+62", | |
}, | |
IE: { | |
name: "Ireland", | |
code: "+353", | |
}, | |
IL: { | |
name: "Israel", | |
code: "+972", | |
}, | |
IT: { | |
name: "Italy", | |
code: "+39", | |
}, | |
JP: { | |
name: "Japan", | |
code: "+81", | |
}, | |
LV: { | |
name: "Latvia", | |
code: "+371", | |
}, | |
LT: { | |
name: "Lithuania", | |
code: "+370", | |
}, | |
LU: { | |
name: "Luxembourg", | |
code: "+352", | |
}, | |
MT: { | |
name: "Malta", | |
code: "+356", | |
}, | |
MX: { | |
name: "Mexico", | |
code: "+52", | |
}, | |
NL: { | |
name: "Netherlands", | |
code: "+31", | |
}, | |
NZ: { | |
name: "New Zealand", | |
code: "+64", | |
}, | |
NO: { | |
name: "Norway", | |
code: "+47", | |
}, | |
PE: { | |
name: "Peru", | |
code: "+51", | |
}, | |
PL: { | |
name: "Poland", | |
code: "+48", | |
}, | |
PT: { | |
name: "Portugal", | |
code: "+351", | |
}, | |
PR: { | |
name: "Puerto Rico", | |
code: "+1", | |
}, | |
RO: { | |
name: "Romania", | |
code: "+40", | |
}, | |
SK: { | |
name: "Slovakia", | |
code: "+421", | |
}, | |
ZA: { | |
name: "South Africa", | |
code: "+27", | |
}, | |
ES: { | |
name: "Spain", | |
code: "+34", | |
}, | |
SE: { | |
name: "Sweden", | |
code: "+46", | |
}, | |
CH: { | |
name: "Switzerland", | |
code: "+41", | |
}, | |
GB: { | |
name: "United Kingdom", | |
code: "+44", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment