-
Notifications
You must be signed in to change notification settings - Fork 0
/
gggget.js
185 lines (89 loc) · 2.8 KB
/
gggget.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
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
// Requirements
var jsdom = require('jsdom');
var sys = require('sys');
var mootools = require('mootools');
var fs = require('fs');
var http = require('http');
var path = require('path');
// Definitions
var user = process.argv[2]; // called by "$node gggget.js username"
var missing_images = [];
// Read JSON
fs.readFile('./found/'+user+'.json','utf-8', function (err, data) {
if (err) throw err;
found_images = JSON.decode(data);
downloadNext();
});
// Download Files
function downloadNext(){
if(found_images.length > 0){
var next_image = found_images.pop();
requestFile(next_image);
} else {
console.log('Done!');
}
}
function lookForLocalFile(filename,user){
}
function requestFile(url){
console.log('attempting to download '+url);
var filename = url.split('/').getLast().split('?')[0];
var extension = url.split('.').getLast();
var request_host = url.split('/')[0];
var request_path = url.replace(request_host,'');
// Look for local file first
path.exists('./images/'+filename, function(exists){
if(!exists){
// Download options
var options = {
host: request_host,
port: 80,
path: request_path,
method: 'GET'
}
// Retrieve the file
var request = http.request(options, function(res){
res.setEncoding('binary');
var cl = res.headers['content-length'];
console.log(cl);
var checkContentType = function(){
if(res.headers['content-type'] == null) return false;
if(!res.headers['content-type'].contains('image')) return false;
return true;
};
//Step the queue on request end
// res.on('end', function(err){downloadNext()});
//If the request looks good, create and write the image file
if(checkContentType() && res.statusCode != 404){
// When new chunks come in, write them to the imagedata var
var imagedata = ''
res.on('data', function (chunk) {
imagedata += chunk;
});
// On request end, write the file,
res.on('end', function(err){
if(!err) {
fs.writeFile('images/'+filename, imagedata, 'binary', function (err) {
if (err) {console.log('could not write '+filename);}
downloadNext();
});
}
});
} else {
console.log(url+ ' cant be retrieved'); // could be a 404, or a html file etc.
downloadNext();
missing_images.include(url);
}
}); //get
request.on('error', function(err){
console.log('cannot reach host at '+url);
missing_images.include(url);
downloadNext();
});
request.end();
} else {
console.log('file has been downloaded, get the next one');
downloadNext();
}
});
}