-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (72 loc) · 2.46 KB
/
index.js
File metadata and controls
79 lines (72 loc) · 2.46 KB
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
const { Channel } = require("./core/channel");
const config = require("./config.json");
const {
moveNotUsedTokenToUsed,
getNotUsedChannels,
removeUnAuthToken,
checkFiles,
DBFiles,
} = require("./core/files");
const { CSVModule } = require("./core/csv_module");
const path = require("path");
const fs = require("fs")
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const csv_module = new CSVModule();
async function selectRandomChannel() {
// get random channel
const tokens = getNotUsedChannels(); // get not used tokens from file
const randomIndex = randomIntFromInterval(0, tokens.length - 1); // random index of array
const channel = new Channel(tokens[randomIndex]); // create instance of channel
try {
await channel.checkHealth(); //check channel status
} catch (e) {
if (e === 401) {
// if channel not auth - remove him from tokens array in config and try new
console.log(`Token ${tokens[randomIndex]} - unauth. Remove from config file.`)
removeUnAuthToken(tokens[randomIndex]);
return await selectRandomChannel();
}
}
moveNotUsedTokenToUsed(channel.token);
return channel;
}
async function timeOutCheckPhone(phone) {
try {
const channel = await selectRandomChannel();
const phoneStatus = await channel.checkPhone(phone); // check phone in wa. if valid - exist
const timeOfComplete = new Date();
console.log(
`${channel.token} check phone: ${phone}, in time: ${timeOfComplete}. Result: ${phoneStatus}`
);
csv_module.writeCSV({phone, status: phoneStatus}, "./result.csv")
} catch (e) {
console.log(e);
}
}
async function massCheckPhones(phones) {
let delay = 0;
for (let i = 0; i < phones.length; i++) {
const randomDelay = randomIntFromInterval(config.minDelay, config.maxDelay); // random delay between checks
delay += randomDelay;
try {
setTimeout(timeOutCheckPhone, delay, phones[i]); // set delayed check
} catch (e) {
console.log(e);
}
}
}
async function init() {
try {
if (config.tokens.length === 0)
throw "Not found valid tokens! (check config.json)";
if(fs.existsSync(DBFiles.phones))
checkFiles();
const phones = csv_module.getPhonesFromCSV(path.resolve("./phones.csv"));
await massCheckPhones(phones);
} catch (e) {
console.log(e);
}
}
init().then(() => console.log("Started"));