-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
143 lines (115 loc) · 3.76 KB
/
run.js
File metadata and controls
143 lines (115 loc) · 3.76 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
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
'use strict';
/**
* Package dependencies
*/
var Horseman = require('node-horseman'),
validUrl = require('valid-url'),
fs = require('fs'),
prompt = require('prompt'),
program = require('commander'),
gimmeproxy = require('gimmeproxy');
/**
* Parse arguments
*/
program
.version('0.0.1')
.option('-x --action-to-perform [string]', 'Acao a executar.')
.option('-u --url [string]', 'Url opcional')
.option('-p --enable-proxy', 'Ativar proxy')
.parse(process.argv);
/**
* Stores an array of actions support by this utility framework.
* Populated on script load based on files present in the 'actions' directory
*/
var supportedActions = [],
self = this;
/**
* Loads a Horseman instance to facilitate interaction with PhantomJS
*
* @return {object} phantomInstance phantom instance
*/
var loadPhantomInstance = function () {
var options = {
'phantomPath': '/usr/local/bin/phantomjs',
'loadImages': true,
'injectJquery': true,
'webSecurity': true,
'ignoreSSLErrors': true
};
if( !program.enableProxy ) {
console.log('Crawler usando conexao transparente');
}else{
console.log('Crawler usando proxy/port:' + self.proxyIpPort);
options.proxy = self.proxyIpPort;
}
var phantomInstance = new Horseman(options);
phantomInstance.on('consoleMessage', function (msg) {
console.log('Phantom log: ', msg);
});
phantomInstance.on('error', function (msg) {
console.log('Phantom erro: ', msg);
});
return phantomInstance;
};
/**
* Triggers execution of the appropriate action
*
* @return {void}
*/
var main = function () {
if (!program.actionToPerform) {
throw 'Informe uma acao. Acoes suportadas: ', supportedActions.join(', ');
} else if (supportedActions.indexOf(program.actionToPerform) < 0) {
throw 'Acao invalida. Acoes suportadas: ', supportedActions.join(', ');
}
console.log('Executando: ', program.actionToPerform);
var performAction = require('./actions/' + program.actionToPerform),
phantomInstance = loadPhantomInstance();
prompt.start();
prompt.override = program;
switch (program.actionToPerform) {
case 'screenshot':
prompt.get([{
'name': 'url',
'description': 'Informe a URL tirar o screenshot',
'required': true,
conform: function (value) { // eslint-disable-line
return validUrl.isWebUri(value);
}
}], function (err, result) {
performAction(phantomInstance, result.url);
});
break;
case 'get_links':
prompt.get([{
'name': 'url',
'description': 'Informe a URL para buscar os links',
'required': true,
conform: function (value) { // eslint-disable-line
return validUrl.isWebUri(value);
}
}], function (err, result) {
performAction(phantomInstance, result.url);
});
break;
default:
phantomInstance.close();
throw 'Acao invalida. Acoes suportadas: ', supportedActions.join(', ');
}
};
/**
* Run immediately on script load to determine available actions and attempt to run the specified action
* @return {void}
*/
(function () {
// Generate an array of supported actions based on the files present in the 'actions' directory
fs.readdir('./actions', function (err, files) {
files.forEach(function (filename) {
supportedActions.push(filename.split('.')[0]);
});
gimmeproxy.getProxy(100).then(function(proxyData) {
self.proxyIpPort = proxyData.ipPort;
main();
});
});
})();