-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
182 lines (152 loc) · 4.78 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
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
var path = require('path')
, fs = require('fs')
, urlUtil = require('url')
, async = require('async')
, Q = require('q')
, _ = require('underscore')
, cheerio = require('cheerio')
, common = require('./lib/common')
/**
* init an instance of the Scraper
*/
module.exports.init = function(options){
return new Scraper(options);
}
module.exports.common = common;
/**
* Constructor
*/
function Scraper(options){
var defaultOptions = {
cc:1,
delay:5,
timeout:60*000,
proxy: false,
proxies:[],
proxy_file: path.join(__dirname, '../proxy.txt'),
proxy_auth:'',
cache: false,
dir: path.join(__dirname, "../")
};
this.options = options || {}
if(this.options.proxy_file || this.options.proxy_auth || this.options.proxies){
//auto enabling the proxies feature when caller provides one of these options
this.options.proxy = true;
}
//override default optiosn with custom options
this.options = common.mergeObjs(this.options, defaultOptions);
this.__loadProxies();
this.request = require('request');
var self = this;
var worker = function(options, cb){
self.__doLoad(options.url, options.options).then(function($){
cb();//indicate to the queue that this request is done
options.handler(null, $); //pass response data to handler
}).fail(function(err){
cb();//indicate to the queue that this request is done
options.handler(err, null); //pass response data to handler
})
}
this.queue = async.queue(worker, options.cc);
}
//*** API ****
//support: load(url), load(options), or load(url, options)
Scraper.prototype.load = function(url, options){
if(typeof url === 'object'){
//case: load(options)
options = url;
url = options.url;
}
options = options || {}
url = url || '';
if(options.form) options.method = options.method || 'POST';
if(url.contains('http://localhost') || url.contains('127.0.0.1')) options.no_proxy = true;
var deferred = Q.defer();
this.queue.push({url:url, options:options, handler: function(err, $){
if(err)
deferred.reject(err);
else
deferred.resolve($);
}})
return deferred.promise;
}
Scraper.prototype.pagin = function(options){
require('./lib/pager').create({
sp: this,
init: options.init,
loadedHandler: options.loaded,
doneHandler: options.done || function(err){
if(err) console.log(err);
}
}).start();
}
//***-- end of API ***
/**
* load given url using Mikeal's Request object
*/
Scraper.prototype.__doLoad = function(url, options ){
var deferred = Q.defer();
options.url = url;
options.headers || (options.headers = {"User-Agent" : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'} );
options.strictSSL = false;
options.followAllRedirects = true;
options.timeout = options.timeout || this.options.timeout;
if(this.options.proxy && options.proxy !== false){
var proxy = this.options.proxies[Math.floor( (Math.random() * this.options.proxies.length) + 1) - 1].trim();
if(this.options.proxy_auth){
proxy = 'http://' + this.options.proxy_auth + '@' + proxy;
}
options.proxy = proxy;
}
var sp = this;
setTimeout(function(){
try{
sp.request(options, function(err, res, body) {
var accept_codes = options.accept_codes || [];
accept_codes.push(200);
if(err || accept_codes.indexOf(res.statusCode) === -1){
deferred.reject(err || new Error("httpcode: " +res.statusCode));
}else{
//success
if(options.plain_text){
//simply return body as text file
deferred.resolve(body);
}else{
//var cheerio = require('cheerio');
var $ = cheerio.load(body);
//make cheerio object xpath-able
_.extend($, require('./lib/xpath'));
//resolve all relative links to absolute
$('a').each(function(i, el){
var old = $(el).attr('href');
if(old && old.length && !old.contains('javascript') && !old.contains('mailto:') && old[0] !== '#'){
$(this).attr('href', urlUtil.resolve(url, old))
}
})
$('form').each(function(i, el){
var old = $(el).attr('action');
if(old && old.length && !old.contains('javascript') && old[0] !== '#'){
$(this).attr('action', urlUtil.resolve(url, old))
}
})
//if(res.statusCode !== 200) {console.log(res);process.exit();}
deferred.resolve($);
}
}
});
}catch(err){
deferred.reject(err);
}
}, this.options.delay)
return deferred.promise;
}
Scraper.prototype.__loadProxies = function(){
if(!this.options.proxy_file || !fs.existsSync(this.options.proxy_file)) return;
//reset proxies
this.options.proxies = [];
var self = this;
fs.readFileSync(this.options.proxy_file).toString().split("\n")
.forEach(function(proxy){
self.options.proxies.push(proxy.trim());
})
}