Skip to content

Instantly share code, notes, and snippets.

@smithrobs
Created August 4, 2017 18:42
Show Gist options
  • Save smithrobs/cf6491d8380c70788a15ffa8fd6b8987 to your computer and use it in GitHub Desktop.
Save smithrobs/cf6491d8380c70788a15ffa8fd6b8987 to your computer and use it in GitHub Desktop.
facebook album sipper
var https = require('https')
,querystring = require('querystring')
,config = require('./config')
,client = require('scp2')
,fs = require('fs');
var access_token;
var getImageLinks = (photoIds) => {
https.get('https://graph.facebook.com/' + photoIds[0] + '?fields=width,height,images&access_token=' + access_token, (res) => {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var fbResponse = JSON.parse(body);
// determine smallest and largest image by width*height
var smallest = fbResponse.images[0];
var largest = fbResponse.images[0];
fbResponse.images.forEach((i) => {
var wh = i.width * i.height;
if ((smallest.width * smallest.height) > wh)
smallest = i;
if ((largest.width * largest.height) < wh)
largest = i;
});
var outputA = [];
outputA.push({
thumb: smallest.source,
// thumb: 'thumb1.jpg',
image: largest.source,
// title: 'my first image',
// description: 'Lorem ipsum caption',
// link: 'http://domain.com'
});
console.log(outputA);
});
}).on('error', (e) => {
console.error(e);
})
};
var getPhotos = (albums) => {
var photo_requests = albums.map((p) => ({'method':'GET', 'relative_url': p.cover_photo}));
var post_data = querystring.stringify({
'access_token' : access_token,
'batch': JSON.stringify(photo_requests)
});
var options = {
hostname: 'graph.facebook.com',
port: 443,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
var batch_req = https.request(options, function(res) {
var batchResponse = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
batchResponse += chunk;
});
res.on('end', function () {
var jsonResponse = JSON.parse(batchResponse);
var responses = jsonResponse.map((r) => (JSON.parse(r.body)));
var pictures = responses.map((r) => ({id: r.id, source: r.source}));
// Join album to cover photo, outputting HTML
var outHtml = '';
albums.forEach((a, idx, ary) => {
for(var i=0;i<pictures.length;i++)
{
var pic = pictures[i];
if (pic.id === a.cover_photo)
{
ary[idx].source = pic.source;
outHtml += `<div class="col-lg-4" style="text-align:center;">
<a href="${a.link}" target="_blank">
<div class="gallery-image img-circle fancy-hover" style="background:url('${a.source}');width:385px;height:385px;margin:auto;">
<div class="overlay"></div>
<p>${a.name}</p>
</div>
</a>
</div>`;
}
}
});
fs.writeFileSync('galleries.html', outHtml, 'utf8', (err) => {
if (err) throw err;
});
// copy to server
client.scp('galleries.html', 'user:[email protected]:public_html/galleries.html', function(err) {
if (err) throw err;
console.log('done');
})
});
});
batch_req.write(post_data);
batch_req.end();
return;
albumIds.forEach((a) => {
// Get photo IDs
https.get('https://graph.facebook.com/' + a + '/photos?access_token=' + access_token, (res) => {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var fbResponse = JSON.parse(body);
var photos = fbResponse.data.map((a) => a.id);
console.log("PhotoIds to load: ", photos);
// TODO: need to paginate through results
getImageLinks(photos);
});
}).on('error', (e) => {
console.error(e);
})
});
};
/// get access token
https.get('https://graph.facebook.com/oauth/access_token?client_id=' + config.facebook.appId + '&client_secret=' + config.facebook.appSecret + '&grant_type=client_credentials', (res) => {
res.on('data', (d) => {
access_token = d.toString().split('=')[1];
https.get('https://graph.facebook.com/<this should be your company id>/albums?access_token=' + access_token, (res) => {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
var fbResponse = JSON.parse(body);
var albumIds = fbResponse.data.filter((a) => a.name !== 'Profile Pictures' && a.name !== 'Cover Photos').map((a) => ({ id: a.id, name: a.name, link: a.link, cover_photo: a.cover_photo}));
//console.log("Albums to parse: ", albumIds);
getPhotos(albumIds);
});
})
});
}).on('error', (e) => {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment