crawler.js 19.2 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 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
'use strict';

var path = require('path')
	, util = require('util')
	, EventEmitter = require('events').EventEmitter
	, request = require('request')
	, _ = require('lodash')
	, cheerio = require('cheerio')
	, fs = require('fs')
	, Bottleneck = require('bottleneckp')
	, seenreq = require('seenreq')
	, iconvLite = require('iconv-lite')
	, typeis = require('type-is').is
	, qs = require('querystring'),
	URL = require('url').URL;

//NOTE for polyfill purpose, cause the http2 is just table for node 10.0
let http2;
try {
	http2 = require('http2');
} catch (e) { 
	//NOTE leave it empty for pass eslint
}

var whacko = null, level, levels = ['silly', 'debug', 'verbose', 'info', 'warn', 'error', 'critical'];
try {
	whacko = require('whacko');
} catch (e) {
	e.code;
}

function defaultLog() {    //2016-11-24T12:22:55.639Z - debug:
	if (levels.indexOf(arguments[0]) >= levels.indexOf(level))
		console.log(new Date().toJSON() + ' - ' + arguments[0] + ': CRAWLER %s', util.format.apply(util, Array.prototype.slice.call(arguments, 1)));
}

function checkJQueryNaming(options) {
	if ('jquery' in options) {
		options.jQuery = options.jquery;
		delete options.jquery;
	}
	return options;
}

function readJqueryUrl(url, callback) {
	if (url.match(/^(file:\/\/|\w+:|\/)/)) {
		fs.readFile(url.replace(/^file:\/\//, ''), 'utf-8', function (err, jq) {
			callback(err, jq);
		});
	} else {
		callback(null, url);
	}
}

function contentType(res) {
	return get(res, 'content-type').split(';').filter(item => item.trim().length !== 0).join(';');
}

function get(res, field) {
	return res.headers[field.toLowerCase()] || '';
}

var log = defaultLog;

function Crawler(options) {
	var self = this;

	options = options || {};
	if (['onDrain', 'cache'].some(key => key in options)) {
		throw new Error('Support for "onDrain", "cache" has been removed! For more details, see https://github.com/bda-research/node-crawler');
	}

	self.init(options);
}
// augment the prototype for node events using util.inherits
util.inherits(Crawler, EventEmitter);

Crawler.prototype.init = function init(options) {
	var self = this;

	var defaultOptions = {
		autoWindowClose: true,
		forceUTF8: true,
		gzip: true,
		incomingEncoding: null,
		jQuery: true,
		maxConnections: 10,
		method: 'GET',
		priority: 5,
		priorityRange: 10,
		rateLimit: 0,
		referer: false,
		retries: 3,
		retryTimeout: 10000,
		timeout: 15000,
		skipDuplicates: false,
		rotateUA: false,
		homogeneous: false,
		http2: false
	};

	// return defaultOptions with overridden properties from options.
	self.options = _.extend(defaultOptions, options);

	// you can use jquery or jQuery
	self.options = checkJQueryNaming(self.options);

	// Don't make these options persist to individual queries
	self.globalOnlyOptions = ['maxConnections', 'rateLimit', 'priorityRange', 'homogeneous', 'skipDuplicates', 'rotateUA'];

	self.limiters = new Bottleneck.Cluster(self.options.maxConnections, self.options.rateLimit, self.options.priorityRange, self.options.priority, self.options.homogeneous);

	//maintain the http2 sessions
	self.http2Connections = {};

	level = self.options.debug ? 'debug' : 'info';

	if (self.options.logger)
		log = self.options.logger.log.bind(self.options.logger);

	self.log = log;

	self.seen = new seenreq(self.options.seenreq);
	self.seen.initialize().then(() => log('debug', 'seenreq is initialized.')).catch(e => log('error', e));

	self.on('_release', function () {
		log('debug', 'Queue size: %d', this.queueSize);

		if (this.limiters.empty) {

			if (Object.keys(self.http2Connections).length > 0) self._clearHttp2Session();
			return this.emit('drain');
		}
	});
};

Crawler.prototype.setLimiterProperty = function setLimiterProperty(limiter, property, value) {
	var self = this;

	switch (property) {
	case 'rateLimit': self.limiters.key(limiter).setRateLimit(value); break;
	default: break;
	}
};


Crawler.prototype.generateHttp2RequestLine = function (options) {
	const urlObj = new URL(options.uri);

	const requestLine = {
		':method': options.method || 'GET',
		':path': urlObj.pathname,
		':scheme': urlObj.protocol.replace(':', ''),
		':authority': urlObj.hostname
	};

	return requestLine;
};

Crawler.prototype.generateHttp2RequestBody = function (options) {
	let data = null;
	if (options.form) {
		if (!/^application\/x-www-form-urlencoded\b/.test(options.headers['content-type'])) {
			options.headers['content-type'] = 'application/x-www-form-urlencoded';
		}

		data = (typeof options.form === 'string') ? encodeURIComponent(options.form) : qs.stringify(options.form);
	} else if (options.json) {
		if (!/^application\/x-www-form-urlencoded\b/.test(options.headers['content-type'])) {
			data = JSON.stringify(options.body);
		}

		if (!options.headers['contentn-type']) options.headers['content-type'] = 'application/json';

	} else if (options.body !== undefined) {
		data = options.body;
	}

	//NOTE  the default situation do nothing to the 
	return data;
};

Crawler.prototype._inject = function _inject(response, options, callback) {
	var $;

	if (options.jQuery === 'whacko') {
		if (!whacko) {
			throw new Error('Please install whacko by your own since `crawler` detected you specify explicitly');
		}

		$ = whacko.load(response.body);
		callback(null, response, options, $);
	} else if (options.jQuery === 'cheerio' || options.jQuery.name === 'cheerio' || options.jQuery === true) {
		var defaultCheerioOptions = {
			normalizeWhitespace: false,
			xmlMode: false,
			decodeEntities: true
		};
		var cheerioOptions = options.jQuery.options || defaultCheerioOptions;
		$ = cheerio.load(response.body, cheerioOptions);

		callback(null, response, options, $);
	} else if (options.jQuery.jsdom) {
		var jsdom = options.jQuery.jsdom;
		var scriptLocation = path.resolve(__dirname, '../vendor/jquery-2.1.1.min.js');

		//Use promises
		readJqueryUrl(scriptLocation, function (err, jquery) {
			try {
				jsdom.env({
					url: options.uri,
					html: response.body,
					src: [jquery],
					done: function (errors, window) {
						$ = window.jQuery;
						callback(errors, response, options, $);

						try {
							window.close();
							window = null;
						} catch (err) {
							log('error', err);
						}

					}
				});
			} catch (e) {
				options.callback(e, { options }, options.release);
			}
		});
	}
	// Jquery is set to false are not set
	else {
		callback(null, response, options);
	}
};

Crawler.prototype.isIllegal = function isIllegal(options) {
	return (_.isNull(options) || _.isUndefined(options) || (!_.isString(options) && !_.isPlainObject(options)));
};

Crawler.prototype.direct = function direct(options) {
	var self = this;

	if (self.isIllegal(options) || !_.isPlainObject(options)) {
		return log('warn', 'Illegal queue option: ', JSON.stringify(options));
	}

	if (!('callback' in options) || !_.isFunction(options.callback)) {
		return log('warn', 'must specify callback function when using sending direct request with crawler');
	}

	options = checkJQueryNaming(options);

	// direct request does not follow the global preRequest
	options.preRequest = options.preRequest || null;

	_.defaults(options, self.options);

	// direct request is not allowed to retry
	options.retries = 0;

	// direct request does not emit event:'request' by default
	options.skipEventRequest = _.isBoolean(options.skipEventRequest) ? options.skipEventRequest : true;

	self.globalOnlyOptions.forEach(globalOnlyOption => delete options[globalOnlyOption]);

	self._buildHttpRequest(options);
};

Crawler.prototype.queue = function queue(options) {
	var self = this;

	// Did you get a single object or string? Make it compatible.
	options = _.isArray(options) ? options : [options];

	options = _.flattenDeep(options);

	for (var i = 0; i < options.length; ++i) {
		if (self.isIllegal(options[i])) {
			log('warn', 'Illegal queue option: ', JSON.stringify(options[i]));
			continue;
		}
		self._pushToQueue(
			_.isString(options[i]) ? { uri: options[i] } : options[i]
		);
	}
};

Crawler.prototype._pushToQueue = function _pushToQueue(options) {
	var self = this;

	// you can use jquery or jQuery
	options = checkJQueryNaming(options);

	_.defaults(options, self.options);
	options.headers = _.assign({}, self.options.headers, options.headers);

	// Remove all the global options from our options
	// TODO we are doing this for every _pushToQueue, find a way to avoid this
	self.globalOnlyOptions.forEach(globalOnlyOption => delete options[globalOnlyOption]);

	// If duplicate skipping is enabled, avoid queueing entirely for URLs we already crawled
	if (!self.options.skipDuplicates) {
		self._schedule(options);
		return;
	}

	self.seen.exists(options, options.seenreq).then(rst => {
		if (!rst) {
			self._schedule(options);
		}
	}).catch(e => log('error', e));
};

Crawler.prototype._schedule = function _scheduler(options) {
	var self = this;
	//NOTE this will be used to add proxy outside the class
	self.emit('schedule', options);

	self.limiters.key(options.limiter || 'default').submit(options.priority, function (done, limiter) {
		options.release = function () { done(); self.emit('_release'); };
		if (!options.callback)
			options.callback = options.release;

		if (limiter) {
			self.emit('limiterChange', options, limiter);
		}

		if (options.html) {
			self._onContent(null, options, { body: options.html, headers: { 'content-type': 'text/html' } });
		} else if (typeof options.uri === 'function') {
			options.uri(function (uri) {
				options.uri = uri;
				self._buildHttpRequest(options);
			});
		} else {
			self._buildHttpRequest(options);
		}
	});

};

Crawler.prototype._clearHttp2Session = function _clearHttp2Session() {
	log('debug', `Crawler clear all ${Object.keys(this.http2Connections).length} http2 connections`);
	Object.keys(this.http2Connections).forEach(hostName => {
		this._closeAndDeleteHttp2Session(hostName);
		log('debug', `http2 connection to ${hostName} closed`);
	});
};

Crawler.prototype._closeAndDeleteHttp2Session = function _closeAndDeleteHttp2Session(targetHost) {
	if (this.http2Connections[targetHost]) {
		this.http2Connections[targetHost].close();
		delete this.http2Connections[targetHost];
	}
};

Crawler.prototype._buildHttpRequest = function _buildHTTPRequest(options) {
	var self = this;

	log('debug', options.method + ' ' + options.uri);
	if (options.proxy)
		log('debug', 'Use proxy: %s', options.proxy);

	// Cloning keeps the opts parameter clean:
	// - some versions of "request" apply the second parameter as a
	// property called "callback" to the first parameter
	// - keeps the query object fresh in case of a retry

	var ropts = _.assign({}, options);

	if (!ropts.headers) { ropts.headers = {}; }
	if (ropts.forceUTF8) { ropts.encoding = null; }
	// specifying json in request will have request sets body to JSON representation of value and
	// adds Content-type: application/json header. Additionally, parses the response body as JSON
	// so the response will be JSON object, no need to deal with encoding
	if (ropts.json) { options.encoding = null; }
	if (ropts.userAgent) {
		if (self.options.rotateUA && _.isArray(ropts.userAgent)) {
			ropts.headers['User-Agent'] = ropts.userAgent[0];
			// If "rotateUA" is true, rotate User-Agent
			options.userAgent.push(options.userAgent.shift());
		} else {
			ropts.headers['User-Agent'] = ropts.userAgent;
		}
	}

	if (ropts.referer) {
		ropts.headers.Referer = ropts.referer;
	}

	if (ropts.proxies && ropts.proxies.length) {
		ropts.proxy = ropts.proxies[0];
	}

	var doRequest = function (err) {
		if (err) {
			err.message = 'Error in preRequest' + (err.message ? ', ' + err.message : err.message);
			switch (err.op) {
			case 'retry': log('debug', err.message + ', retry ' + options.uri); self._onContent(err, options); break;
			case 'fail': log('debug', err.message + ', fail ' + options.uri); options.callback(err, { options: options }, options.release); break;
			case 'abort': log('debug', err.message + ', abort ' + options.uri); options.release(); break;
			case 'queue': log('debug', err.message + ', queue ' + options.uri); self.queue(options); options.release(); break;
			default: log('debug', err.message + ', retry ' + options.uri); self._onContent(err, options); break;
			}
			return;
		}

		//do http2.* request 
		if (ropts.http2) {
			if (!http2) {
				process.nextTick(() => {
					const notSupportedHttp2Error = new Error('you are trying to use http2 API which may not be supported for your current environment or node version');
					notSupportedHttp2Error.code = 'NOHTTP2SUPPORT';
					self._onContent(notSupportedHttp2Error, options);
				});
				return;
			}
			self._http2request(ropts, options);
		} else {
			if (ropts.skipEventRequest !== true) {
				self.emit('request', ropts);
			}

			var requestArgs = ['uri', 'url', 'qs', 'method', 'headers', 'body', 'form', 'formData', 'json', 'multipart', 'followRedirect', 'followAllRedirects', 'maxRedirects', 'removeRefererHeader', 'encoding', 'pool', 'timeout', 'proxy', 'auth', 'oauth', 'strictSSL', 'jar', 'aws', 'gzip', 'time', 'tunnel', 'proxyHeaderWhiteList', 'proxyHeaderExclusiveList', 'localAddress', 'forever', 'agent', 'strictSSL', 'agentOptions', 'agentClass'];

			request(_.pick.apply(self, [ropts].concat(requestArgs)), function (error, response) {
				if (error) {
					return self._onContent(error, options);
				}

				self._onContent(error, options, response);
			});
		}
	};

	if (options.preRequest && _.isFunction(options.preRequest)) {
		options.preRequest(ropts, doRequest);
	} else {
		doRequest();
	}
};

Crawler.prototype._buildHttp2Session = function _buildHttp2Session(targetHost) {
	const self = this;

	const newHttp2Connection = self.http2Connections[targetHost] = http2.connect(targetHost);

	log('debug', `connect to a new ${targetHost}`);

	newHttp2Connection.on('error', (err) => {
		log('warn', `Http2 stession error ${targetHost}, got error ${err}`);
	}).on('goaway', () => {
		log('debug', `Http2 session${targetHost} connection goaway`);
	}).on('connect', () => {
		log('debug', `Http2 session${targetHost} connection init`);
	}).once('close', () => {
		log('debug', `Http2 session ${targetHost} connection closed`);
	});
};

Crawler.prototype._http2request = function _http2request(ropts, options) {
	const self = this;

	const targetHost = new URL(ropts.uri).origin;
	ropts.headers = Object.assign(ropts.headers, self.generateHttp2RequestLine(ropts));
	const requestBody = ropts.headers[':method'] === 'GET' ? null : self.generateHttp2RequestBody(ropts);
	const response = {
		headers: {}
	};
	const chunks = [];
	let http2Error = null;

	if (!self.http2Connections[targetHost] || self.http2Connections[targetHost].destroyed) {
		self._buildHttp2Session(targetHost);
	}

	let req = null;
	try {
		req = self.http2Connections[targetHost].request(ropts.headers);
	} catch (e) {
		//to handle the goaway issue, goaway will make the session can not be established
		//but it can not be detected at the moment that stream init
		//try catch seems the way to sovle it
		self._onContent(e, options, response);
		return;
	}

	req.on('response', headers => {
		//Where build the response obj
		response.statusCode = headers[':status'];
		response.request = {
			uri: `${req.sentHeaders[':scheme']}://${req.sentHeaders[':authority']}${req.sentHeaders[':path']}`,
			method: req.sentHeaders[':method'],
			headers: Object.assign({}, req.sentHeaders, req.sentInfoHeaders)
		};
		for (const name in headers) {
			response.headers[name] = headers[name];
		}
	});

	req.on('error', (err) => {
		log('debug', `Http2 stream error${ropts.uri}, got error ${err}`);
		http2Error = err;
	});

	req.on('data', chunk => {
		chunks.push(chunk);
	});

	req.setTimeout(self.options.timeout);

	req.on('timeout', () => {
		const error = new Error('ESOCKETTIMEDOUT');
		error.code = 'ESOCKETTIMEDOUT';
		http2Error = error;
		req.close();
	});

	req.once('close', () => {
		if (http2Error) self._onContent(http2Error, options, response);
		else {
			response.body = Buffer.concat(chunks);
			self._onContent(null, options, response);
		}
	});

	req.on('end', () => {
		log('debug', `${ropts.uri} stream ends`);
	});

	//set request body
	req.end(requestBody);
};

Crawler.prototype._onContent = function _onContent(error, options, response) {
	var self = this;

	if (error) {
		switch (error.code) {
		case 'NOHTTP2SUPPORT':
			//if the enviroment is not support http2 api, all request rely on http2 protocol
			//are aborted immediately no matter how many retry times left
			log('error', 'Error ' + error + ' when fetching ' + (options.uri || options.url) + ' skip all retry times');
			break;
		default:
			log('error', 'Error ' + error + ' when fetching ' + (options.uri || options.url) + (options.retries ? ' (' + options.retries + ' retries left)' : ''));
			if (options.retries) {
				setTimeout(function () {
					options.retries--;
					self._schedule(options);
					options.release();
				}, options.retryTimeout);
				return;
			}
			break;
		}

		options.callback(error, { options: options }, options.release);

		return;
	}

	if (!response.body) { response.body = ''; }

	log('debug', 'Got ' + (options.uri || 'html') + ' (' + response.body.length + ' bytes)...');

	try {
		self._doEncoding(options, response);
	} catch (e) {
		return options.callback(e, { options: options }, options.release);
	}

	response.options = options;

	if (options.method === 'HEAD' || !options.jQuery) {
		return options.callback(null, response, options.release);
	}

	var injectableTypes = ['html', 'xhtml', 'text/xml', 'application/xml', '+xml'];
	if (!options.html && !typeis(contentType(response), injectableTypes)) {
		log('warn', 'response body is not HTML, skip injecting. Set jQuery to false to suppress this message');
		return options.callback(null, response, options.release);
	}

	log('debug', 'Injecting');

	self._inject(response, options, self._injected.bind(self));
};

Crawler.prototype._injected = function (errors, response, options, $) {
	log('debug', 'Injected');

	response.$ = $;
	options.callback(errors, response, options.release);
};

Crawler.prototype._doEncoding = function (options, response) {
	var self = this;

	if (options.encoding === null) {
		return;
	}

	if (options.forceUTF8) {
		var charset = options.incomingEncoding || self._parseCharset(response);
		response.charset = charset;
		log('debug', 'Charset ' + charset);

		if (charset !== 'utf-8' && charset !== 'ascii') {// convert response.body into 'utf-8' encoded buffer
			response.body = iconvLite.decode(response.body, charset);
		}
	}

	response.body = response.body.toString();
};

Crawler.prototype._parseCharset = function (res) {
	//Browsers treat gb2312 as gbk, but iconv-lite not.
	//Replace gb2312 with gbk, in order to parse the pages which say gb2312 but actually are gbk.
	function getCharset(str) {
		var charset = (str && str.match(/charset=['"]?([\w.-]+)/i) || [0, null])[1];
		return charset && charset.replace(/:\d{4}$|[^0-9a-z]/g, '') == 'gb2312' ? 'gbk' : charset;
	}
	function charsetParser(header, binary, default_charset = null) {
		return getCharset(header) || getCharset(binary) || default_charset;
	}

	var charset = charsetParser(contentType(res));
	if (charset)
		return charset;

	if (!typeis(contentType(res), ['html'])) {
		log('debug', 'Charset not detected in response headers, please specify using `incomingEncoding`, use `utf-8` by default');
		return 'utf-8';
	}

	var body = res.body instanceof Buffer ? res.body.toString() : res.body;
	charset = charsetParser(contentType(res), body, 'utf-8');

	return charset;
};

Object.defineProperty(Crawler.prototype, 'queueSize', {
	get: function () {
		return this.limiters.unfinishedClients;
	}
});

module.exports = Crawler;