-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdlchromesamples.js
53 lines (48 loc) · 1.4 KB
/
dlchromesamples.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
var url = 'http://developer.chrome.com/extensions/samples.html'
var cheerio = require('cheerio');
var request = require('request');
var _ = require('underscore')._;
var mkdirp = require('mkdirp');
var path = './chromesamples/'
var fs = require('fs');
var Url = require('url');
//.resolve(from,to);
//
function doRequest(url){
console.log('dorequest');
request(url, function (error, response, body) {
console.log('got response');
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
parseRequest(body,url);
}else{
console.log('err:',error);
console.log('status:',response.statusCode)
console.log('body:',body);
}
});
}
function parseRequest(body,url){
console.log('parsebody start: ',url);
var $ = cheerio.load(body);
_.each($('div.sample'),function(val,idx){ val = $($($(val).find('div')[2]).find('a'))
_.each(val,function(file,idx2){ file = $(file);
var href = file.attr('href');
var folder = href.split('/');
folder.pop();
folder = folder.join('/')+'/'
console.log('folder:',folder);
mkdirp.sync(path+folder);
var furl = Url.resolve(url,href);
dlFile(href,furl,1000*idx+100*idx2);
console.log('url:',url,"\nhref:",href,"\nfurl:",furl,"\n");
});
});
}
doRequest(url);
function dlFile(href,furl,twait){
setTimeout(function(){
console.log('startdl:',furl);
request(furl).pipe(fs.createWriteStream(path + href))
},twait);
}