-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (55 loc) · 1.5 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
var jsdom = require('jsdom'),
http = require('http'),
fs = require('fs'),
jquery = fs.readFileSync("./js/jquery.js").toString(),
url = 'http://www.aio.com.tw/wall',
lists = [],
path = './data/';
// fetch image function.
function fetchImg (url) {
jsdom.env({
html: url,
src: [
jquery
],
done: function(errors, window) {
var $ = window.$;
$("img").each(function (idx, el) {
var nodeSrc = $(el).attr("src");
lists.push(nodeSrc);
});
// save image
saveImage(lists.pop(), path);
}
});
}
function saveImage (url, filepath) {
if ( ! url.match(/^http:\/\//)) {
return;
}
var urls = url.replace("http://", "").split("/"),
options = {
host: urls.shift(),
port: 80,
path: urls.join("/")
};
http.get(options, function(res){
var imagedata = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imagedata += chunk
});
res.on('end', function(){
var fullname = filepath + urls.pop();
fs.writeFile(fullname, imagedata, 'binary', function(err){
// if (err) throw err
console.log('File saved : ' + fullname);
if (lists.length > 0) {
saveImage(lists.pop(), path);
}
});
});
});
}
fetchImg(url);
console.log("Starting....enjoy it!");