MaxySpark MaxySpark - 4 months ago 31
Node.js Question

Can't get all images from facebook album using fb graph api with nodejs?

I am learning nodejs.
I tried to download all my photos from Mobile uploads album.
I used 'fb' npm module for graph api. I am only getting 25 photos but in my album there are 300+ photos.
How can I get all photos?

var _ = require('underscore');
var request = require('request');
var FB = require('fb');
var fs = require('fs');

var my_token = "xyz";
FB.setAccessToken(mt_token);
var urls = [];

FB.api('/me/albums','get', function (res) {
if(!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
var data = _.findWhere(res.data, {name:"Mobile Uploads"});
FB.api('/'+data.id+'/photos', 'get', (resp) => {
if(!resp || resp.error) {
console.log(!resp ? 'error occurred' : resp.error);
return;
}
resp.data.forEach(function(element) {
urls.push(element.source);
});

for(var i = 0;i<urls.length;i++){
request(urls[i]).pipe(fs.createWriteStream('img/'+i+'.jpg'));
console.log("Downloaded : "+i+'.jpg');
}

});
});


enter image description here
Thanx

Answer

finally got it using request module facebook api did not work for me

function downloadLoop(urls) {
            for(i;i<urls.length;i++){
                request(urls[i]).pipe(fs.createWriteStream('img/'+(i+1)+'.jpg')).on('finish', function(response) {         
                    console.log("Download Completed : "+(++j)+'/'+i);
                    });

            }
}

function download(next) {
        request({
            url: next,
            json: true
        }, function(err, res, body) {
            if(err) throw err;
            for (var i = 0; i < body.data.length; i++) {
                urls.push(body.data[i].images[0].source);                           
            }
            console.log("getting images... Total Image : "+ urls.length);
            if(body.paging && body.paging.next) {
             download(body.paging.next);

        } else {
                console.log("Completed");
            }
downloadLoop(urls);
        });
}

Github Link

Comments