/
get_all.js
91 lines (74 loc) · 2.03 KB
/
get_all.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
var async = require("async");
var paginate = require("./paginate");
var debug = require("debug")("json");
module.exports = function(
token,
shouldGetAlbumPhotos,
shouldGetTaggedPhotos,
cb
) {
paginate(url("/me/albums"), function(err, albums) {
if (err) {
throw err;
}
if (!shouldGetAlbumPhotos) albums = [];
// for each album, fill in all the photos objects
async.map(albums, addPhotosToAlbum, function(err, albums) {
if (err) {
return cb(err);
}
if (!shouldGetTaggedPhotos) {
debug("finished getting all albums. Not getting tagged photos");
cb(null, albums);
} else {
debug("getting tagged photos.");
paginate(
url("/me/photos").concat("&fields=id,images,source,created_time"),
function(err, taggedPhotos) {
if (err) {
return cb(err);
}
debug("got all tagged photos.");
cb(
null,
albums.concat({
name: "Photos of me (Tagged photos)",
photos: taggedPhotos
})
);
}
);
}
});
});
// gets all photos from an album and append it to it
function addPhotosToAlbum(album, cb) {
if (!album.id) {
cb(new Error("no album id found"));
}
debug("Getting photos to album with id: " + album.id);
paginate(
url("/", album.id, "/photos").concat(
"&fields=id,images,source,created_time"
),
function(err, photos) {
if (err) {
return cb(err);
}
// TODO: paginate in likes and comments of each photo.
album.photos = photos;
debug("finished adding photos to album: " + album.name);
cb(null, album);
}
);
}
// ---------- helpers ----------
function url() {
var BASE_URL = "https://graph.facebook.com/v2.12";
var paths = [].slice.apply(arguments);
return [BASE_URL]
.concat(paths)
.concat(["?access_token=", token])
.join("");
}
};