-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
141 lines (122 loc) · 3.9 KB
/
index.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
const fs = require("fs");
const parse = require("csv-parse");
const cliProgress = require("cli-progress");
const axios = require("axios");
const async = require("async");
<<<<<<< HEAD
require("dotenv").config();
=======
require('dotenv').config()
>>>>>>> main
function validate(email, fileName, progressBar) {
var config = {
method: "get",
maxBodyLength: Infinity,
url: "http://localhost:9292?email=" + email,
headers: {
Authorization: process.env.ACCESS_TOKEN,
Accept: "application/json",
},
};
return axios(config)
.then(function (response) {
if (response.data.success) {
// check if the email exists in the validated file
<<<<<<< HEAD
const validatedFileName = `./validated/${fileName.slice(
0,
-4
)}_validated.csv`;
const fileExists = fs.existsSync(validatedFileName);
if (
fileExists &&
fs.readFileSync(validatedFileName).toString().includes(email)
=======
const validatedFileName = `./validated/${fileName.slice(0, -4)}_validated.csv`;
const fileExists = fs.existsSync(validatedFileName);
if (
fileExists &&
fs
.readFileSync(validatedFileName)
.toString()
.includes(email)
>>>>>>> main
) {
return;
}
// If the email is not found in the validated file, insert it
if (!fileExists) {
fs.writeFileSync(validatedFileName, "email\n");
}
fs.appendFileSync(validatedFileName, email + "\n");
}
})
.catch(function (error) {
// Commented to prevent console.log from spamming
// console.log(error);
// validate(email);
})
.finally(function () {
progressBar.increment();
});
}
// create a new async queue with a maximum concurrency limit of 10
const q = async.queue((task, callback) => {
validate(task.email, task.fileName, task.progressBar)
.then(() => callback())
.catch((err) => callback(err));
}, 10);
q.drain(() => {
console.log("All items have been processed");
});
// Open the csv folder and loop through the files
fs.readdir("./csv", (err, files) => {
files.forEach((file) => {
console.log("Reading File: " + file);
// create a new progress bar instance and use shades_classic theme
const bar1 = new cliProgress.SingleBar(
{
format: "{bar} {percentage}% | ETA: {eta}s | {value}/{total} Emails",
},
cliProgress.Presets.shades_classic
);
var exec = require("child_process").exec;
exec("wc -l ./csv/" + file, function (error, results) {
// get the line count
let lineCount = results.split(" ")[0];
var count = 0;
bar1.start(lineCount, 0);
// create the validated file
const validatedFileName = `./validated/${file.slice(0, -4)}_validated.csv`;
if (!fs.existsSync(validatedFileName)) {
fs.writeFileSync(validatedFileName, "email\n");
}
// open csv file
fs.createReadStream("./csv/" + file)
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
// validate the string to see if its an email
if (row[0].includes("@")) {
// push each email and the file name to the queue
q.push({
email: row[0],
fileName: file,
progressBar: bar1,
});
}
})
.on("end", function () {
// stop the progress bar
bar1.stop();
// count
let validatedEmailCount = 0;
// read the validated file and count the number of lines (excluding header)
const validatedEmails = fs.readFileSync(validatedFileName).toString();
validatedEmailCount = validatedEmails.split(/\r?\n/).length - 1;
console.log(
`${validatedEmailCount} emails found valid in file ${file}`
);
});
});
});
});