forked from D-Marc1/facebook-javascript-all-photos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb-code.js
113 lines (91 loc) · 4.01 KB
/
fb-code.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class FbAllPhotos {
constructor() {
this.fbAlbumsPhotosObj = {};
}
get fullObj() {
return this.fbAlbumsPhotosObj
}
getAlbums(limitAlbums, winCallback, failCallback) {
FB.api('/me?fields=albums.limit(' + limitAlbums + '){name,count,cover_photo{picture}}', response => {
if(response && response.error) { //If response exists and error
if(typeof failCallback === 'function') failCallback(response.error);
return;
} else if(!response || !response.hasOwnProperty('albums')) {
if(typeof failCallback === 'function') failCallback('noResponse');
return;
}
response.albums.data.forEach(album => {
album.cover_photo = (album.cover_photo) ? album.cover_photo.picture : ''; //All we need is picture
});
this.fbAlbumsPhotosObj = response.albums;
if(typeof winCallback === 'function') winCallback(this.fbAlbumsPhotosObj);
});
}
getPhotosInAlbum(albumId, limitPics, winCallback, failCallback) {
const index = this.fbAlbumsPhotosObj.data.findIndex(album => album.id == albumId); //Get index of album. Loose checking due to id as string
if(index === -1) {
if(typeof failCallback === 'function') failCallback('noAlbum');
return;
}
FB.api(albumId + '/?fields=photos.limit(' + limitPics + '){picture,images}', response => {
if(response && response.error) { //If response exists and error
if(typeof failCallback === 'function') failCallback(response.error);
return;
} else if(!response || !response.hasOwnProperty('photos')) {
if(typeof failCallback === 'function') failCallback('noResponse');
return;
}
response.photos.data.forEach(photo => {
photo.picture_full = photo.images[0].source; //[0] is the largest image
delete photo.images; //Only need one full image
});
this.fbAlbumsPhotosObj.data[index].photos = response.photos;
if(typeof winCallback === 'function') winCallback(this.fbAlbumsPhotosObj.data[index].photos);
});
}
async getMoreAlbums(winCallback, failCallback) {
if(!this.fbAlbumsPhotosObj.paging.hasOwnProperty('next')) { //If there are no more albums
if(typeof failCallback === 'function') failCallback('noMore');
return;
}
try {
let response = await fetch(this.fbAlbumsPhotosObj.paging.next);
if(!response.ok) throw new Error('Server error');
response = await response.json();
response.data.forEach(album => {
album.cover_photo = (album.cover_photo) ? album.cover_photo.picture : ''; //All we need is picture
});
this.fbAlbumsPhotosObj.data.push(...response.data); //Append albums
this.fbAlbumsPhotosObj.paging = response.paging; //Set paging to new values
if(typeof winCallback === 'function') winCallback(this.fbAlbumsPhotosObj);
} catch(error) {
if(typeof failCallback === 'function') failCallback(error);
return;
}
}
async getMorePhotosInAlbum(albumId, winCallback, failCallback) {
const index = this.fbAlbumsPhotosObj.data.findIndex(album => album.id == albumId); //Get index of album. //Get index of album. Loose checking due to id as string
if(index === -1) { //If there are no more albums
if(typeof failCallback === 'function') failCallback('noAlbum');
return;
} else if(!this.fbAlbumsPhotosObj.data[index].photos.paging.hasOwnProperty('next')) { //If there are no more albums
if(typeof failCallback === 'function') failCallback('noMore');
return;
}
try {
let response = await fetch(this.fbAlbumsPhotosObj.paging.next);
if(!response.ok) throw new Error('Server error');
response = await response.json();
response.data.forEach(photo => {
photo.picture_full = photo.images[0].source; //[0] is the largest image
delete photo.images; //Don't need the rest, only one
});
this.fbAlbumsPhotosObj.data[index].photos.data.push(...response.data); //Append photos in album
this.fbAlbumsPhotosObj.data[index].photos.paging = response.paging; //Set paging to new values
if(typeof winCallback === 'function') winCallback(this.fbAlbumsPhotosObj.data[index].photos);
} catch(error) {
if(typeof failCallback === 'function') failCallback(error);
return;
}
}
}