-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.js
47 lines (43 loc) · 1.16 KB
/
spider.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
var request = require('request');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
var utilities = require('./utilities');
function spider(url, callback) {
var filename = utilities.urlToFilename(url);
fs.exists(filename, function(exists) {
if(!exists) {
console.log("Downloading " + url);
request(url, function(err, response, body) {
if(err) {
callback(err);
} else {
mkdirp(path.dirname(filename), function(err) {
if(err) {
callback(err);
} else {
fs.writeFile(filename, body, function(err) {
if(err) {
callback(err);
} else {
callback(null, filename, true);
}
});
}
});
}
});
} else {
callback(null, filename, false);
}
});
}
spider(process.argv[2], function(err, filename, downloaded) {
if(err) {
console.log(err);
} else if(downloaded){
console.log('Completed the download of "'+ filename +'"');
} else {
console.log('"'+ filename +'" was already downloaded');
}
});