-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageSpider.js
57 lines (45 loc) · 1.26 KB
/
imageSpider.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
var Cache = require('./lib/cache');
var Defer = require('./lib/defer');
var fs = require('fs');
var util = require('util');
var mkdirp = require('mkdirp');
var request = require('request');
function ImageSpider(options) {
options = options || {};
this.maxSockets = options.maxSockets || 2;
this.defer = new Defer({
MAX_CONNECTIONS: this.maxSockets,
callbackContext: this
});
}
ImageSpider.prototype.add = function (urls) {
this.defer.push(urls);
// execute pop when add.
this.defer.pop(this.start);
};
ImageSpider.prototype.start = function (url) {
var dirs = url.replace(/^https?:\/\//, '')
.split('/');
var filename = dirs.pop();
dirs = './images/' + dirs.join('/') + '/';
if (!fs.existsSync(dirs)) {
mkdirp.sync(dirs);
}
var self = this;
if (Cache.get(url)) {
self.defer.resetIndex();
self.defer.pop(self.start);
return;
}
console.log('[Get img]', url);
// cache the url.
// important!!! before request.get
Cache.set(url);
request.get(url)
.on('end', function () {
self.defer.resetIndex();
self.defer.pop(self.start);
})
.pipe(fs.createWriteStream(dirs + filename));
};
module.exports = ImageSpider;