Skip to content

Instantly share code, notes, and snippets.

@trevanhetzel
Last active July 30, 2022 03:09
Show Gist options
  • Save trevanhetzel/10798221 to your computer and use it in GitHub Desktop.
Save trevanhetzel/10798221 to your computer and use it in GitHub Desktop.
Facebook Album Fetcher
/* Descripton: A basic plugin to fetch all albums and associated photos from a Facebook page
* Usage: Simply reference the script and instantiate it
* Example instantiaion:
$(window).load(function () {
albumFetcher({
fbId: "137248356344058",
container: ".photos"
});
});
*/
albumFetcher = function (params) {
var albumUrls = [],
photos = [];
$.ajax({
url: "http://graph.facebook.com/" + params.fbId + "/albums",
type: "get",
dataType: "json",
async: false,
success: function (res) {
var data = res.data,
albums = [];
for (var album in data) {
albums.push({
"id": data[album].id,
"name": data[album].name
});
}
for (var id in albums) {
albumUrls.push({
"id": albums[id].id,
"name": albums[id].name
});
}
}
});
for (var url in albumUrls) {
$(params.container)
.append("<div class='fb-fetcher--album'><h2>" + albumUrls[url].name + "</h2>" +
"<ul class='fb-fetcher--photos' id='" + albumUrls[url].id + "'></ul></div>");
$.ajax({
url: "http://graph.facebook.com/" + albumUrls[url].id + "/photos",
type: "get",
dataType: "json",
async: false,
success: function (res) {
var photoData = res.data;
for (var photo in photoData) {
photos.push({
"thumb": photoData[photo].picture,
"source": photoData[photo].source,
"albumId": albumUrls[url].id
});
}
}
});
}
photos.reverse();
for (var single in photos) {
$(document).find("#" + photos[single].albumId).append("<li><a href='" + photos[single].source + "'><img src='" + photos[single].thumb + "'></a></li>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment