Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Grabber.prototype = {
- method2: function(url, offset, limit) {
- console.log(url);
- var self = this;
- request.get({url: url+"&offset="+offset+"&limit="+limit }, function(error, response, body) {
- console.log(http.getAgent('localhost', 8001));
- if (error || response.statusCode != 200) {
- console.log(error);
- console.log(response);
- return;
- }
- var jsonImages = JSON.parse(body);
- for (var i = 0, il = jsonImages.data.length; i < il; i++) {
- var img = jsonImages.data[i];
- var r = request.get({url:img.source, headers: {'content-type': 'image/jpg'}}, function(error, response, body) {
- if (error || response.statusCode != 200) {
- console.log(error);
- console.log(response);
- return;
- }
- });
- r.pipe(fs.createWriteStream('images/'+img.id+'.jpg'));
- }
- if (jsonImages.data.length != 0 || offset != 30) {
- offset = offset + 5;
- self.method2(url,offset, limit);
- }
- });
- },
- method1: function(url) {
- function readImage(images, i, il) {
- if (i == il) {
- return;
- }
- var r = request.get({url:images[i].source, headers: {'content-type': 'image/jpg'}}, function(error, response, body) {
- if (error || response.statusCode != 200) {
- console.log(error);
- console.log(response);
- return;
- }
- });
- var ws = fs.createWriteStream('images/'+images[i].id+'.jpg');
- ws.on("error", function(exception) {
- console.log(exception);
- });
- r.pipe(ws);
- setTimeout(function() {
- console.log(i, il, images[i].id, images[i].source);
- i++;
- readImage(images, i, il);
- }, 400);
- }
- request.get({url: url }, function(error, response, body) {
- if (error || response.statusCode != 200) {
- console.log(error);
- console.log(response);
- return;
- }
- var jsonImages = JSON.parse(body);
- readImage(jsonImages.data, 0, 30 /*jsonImages.data.length*/);
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement