forked from chill117/proxy-lists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·195 lines (156 loc) · 4.01 KB
/
cli.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
'use strict';
var _ = require('underscore');
var fs = require('fs');
var program = require('commander');
var pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
var ProxyLists = require('.');
var validOutputFormats = ['json', 'csv', 'txt'];
var proxyFieldNames = ['source', 'ipAddress', 'port', 'country', 'protocols', 'anonymityLevel'];
function list(value) {
return value.split(',');
}
function value(value) {
return value;
}
program
.version(pkg.version)
.description(pkg.description);
program
.command('getProxies')
.option(
'-a, --anonymity-levels <list>',
'Get proxies with these anonymity levels [' + ProxyLists._anonymityLevels.join(', ') + ']',
list,
null
)
.option(
'-c, --countries <list>',
'Get proxies from these countries [us, ca, cz, ..]',
list,
null
)
.option(
'-p, --protocols <list>',
'Get proxies that support these protocols [' + ProxyLists._protocols.join(', ') + ']',
list,
null
)
.option(
'-s, --sources-white-list <list>',
'Get proxies from these sources only [' + _.keys(ProxyLists._sources).join(', ') + ']',
list,
null
)
.option(
'-x, --sources-black-list <list>',
'Do not get proxies from these sources [' + _.keys(ProxyLists._sources).join(', ') + ']',
list,
null
)
.option(
'-f, --output-file [value]',
'File to which the output will be written',
value,
'proxies'
)
.option(
'-F, --output-format [value]',
'Format in which the output will be written [' + validOutputFormats.join(', ') + ']',
value,
'txt'
)
.option(
'--series',
'Perform all asynchronous operations in series'
)
.option(
'--sample',
'Get a sample of proxies from each source'
)
.action(function() {
var outputFormat = this.outputFormat;
var outputFile = process.cwd() + '/' + this.outputFile + '.' + this.outputFormat;
var writeStream = fs.createWriteStream(outputFile);
writeStream.on('error', console.error.bind(console));
var numWriting = 0;
var wroteData = false;
var ended = false;
function onData(data) {
if (!_.isEmpty(data)) {
numWriting++;
switch (outputFormat) {
case 'json':
data = _.map(data, function(row) {
return JSON.stringify(row);
});
writeStream.write((wroteData ? ',' : '') + data.join(','));
break;
case 'csv':
data = _.map(data, function(row) {
return _.map(proxyFieldNames, function(fieldName) {
return _.isArray(row[fieldName]) ? row[fieldName].join('/') : row[fieldName];
}).join(',');
});
writeStream.write('\n' + data.join('\n'));
break;
case 'txt':
data = _.map(data, function(row) {
return row.ipAddress + ':' + row.port;
});
writeStream.write((wroteData ? '\n' : '') + data.join('\n'));
break;
}
numWriting--;
wroteData = true;
}
endIfDoneWritingData();
}
function onEnd() {
ended = true;
endIfDoneWritingData();
}
function endIfDoneWritingData() {
if (ended && !numWriting) {
endOutput();
}
}
var startOutput = _.once(function() {
console.log('Writing output to ' + outputFile);
switch (outputFormat) {
case 'json':
writeStream.write('[');
break;
case 'csv':
writeStream.write(proxyFieldNames.join(','));
break;
}
});
var endOutput = _.once(function() {
switch (outputFormat) {
case 'json':
writeStream.write(']');
break;
}
writeStream.end();
console.log('Done!');
});
console.log('Getting proxies...');
var options = _.pick(this, [
'anonymityLevels',
'countries',
'protocols',
'sourcesWhiteList',
'sourcesBlackList',
'sample',
'series'
]);
var gettingProxies = ProxyLists.getProxies(options);
gettingProxies.on('data', onData);
gettingProxies.on('error', function(error) {
console.error(error);
});
gettingProxies.on('end', onEnd);
startOutput();
});
program.parse(process.argv);