-
Notifications
You must be signed in to change notification settings - Fork 0
/
photos.js
104 lines (99 loc) · 2.33 KB
/
photos.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
var Flickr = require("flickrapi");
if (process.env.NODE_ENV !== "production") {
var env = require("node-env-file");
env(__dirname + "/.env");
}
let {
flick_api_key: api_key,
flick_user_id: user_id,
flickr_secret: secret,
flickr_access_token: access_token,
flickr_access_token_secret: access_token_secret
} = process.env;
var flickrOptions = {
api_key,
secret,
user_id,
access_token,
access_token_secret
};
let flickrApiObject;
var flickrObject = {
api_key,
user_id,
page: 1,
per_page: 20
};
Flickr.authenticate(flickrOptions, function(error, flickr) {
if (error) {
console.log("Error instiating flickr API object: ", error);
}
flickrApiObject = flickr;
});
function getAlbums() {
let defer = new Promise((resolve, reject) => {
flickrApiObject.photosets.getList(
Object.assign({}, flickrObject, {
primary_photo_extras: "url_s, url_m, url_o, url_t, url_h"
}),
function(err, result) {
if (err) {
let statusCode = err.message === "Photo not found" ? 404 : 500;
reject({ statusCode, err });
}
resolve(result.photosets.photoset);
}
);
});
return defer;
}
function getAlbumDetails(albumid, page_num) {
let defer = new Promise((resolve, reject) => {
flickrApiObject.photosets.getPhotos(
Object.assign({}, flickrObject, {
photoset_id: albumid,
extras: "url_m, url_o, url_t, url_h",
privacy_filter: 2,
page: page_num
}),
function(err, result) {
if (err) {
let statusCode = err.message === "Photoset not found" ? 404 : 500;
reject({ statusCode, err });
}
if (!result) {
resolve({});
return;
}
resolve(result.photoset);
}
);
});
return defer;
}
function getPhoto(photoid) {
let defer = new Promise((resolve, reject) => {
flickrApiObject.photos.getSizes(
Object.assign({}, flickrObject, {
photo_id: photoid
}),
function(err, result) {
if (err) {
let statusCode = err.message === "Photo not found" ? 404 : 500;
reject({ statusCode, err });
}
if (!result) {
resolve({});
return;
}
resolve(result);
}
);
});
return defer;
}
module.exports = {
getAlbums,
getAlbumDetails,
getPhoto
};