This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
photos.js
284 lines (261 loc) · 8.21 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
var fs = require("fs"),
download = require("./download").downloadPhoto,
getSetMetadata = require("./sets"),
glob = require("glob"),
progress = require("progress"),
progressBarAggregate,
progressBar,
photos = [],
sizeMap = {
"Square": "s",
"Large Square": "q",
"Thumbnail": "t",
"Small": "m",
"Medium 640": "z",
"Medium 800": "c",
"Large": "b",
"Original": "o"
};
/**
* Given a photo object, get its info (either from
* flickr, or from .json file if we already have it)
* and get all the associated photographs.
*/
function processPhoto(flickr, options) {
"use strict";
var photo_idx = options.photo_idx,
photo = options.photo,
id = photo.id,
secret = photo.secret,
sizes = [],
filename_cmt = flickr.options.locals.dirstructure.ia.photos.comments + "/" + id + ".json",
filename_ctx = flickr.options.locals.dirstructure.ia.photos.contexts + "/" + id + ".json",
filename_md = flickr.options.locals.dirstructure.ia.photos.root + "/" + id + ".json",
metadata;
// record progress
progressBar.tick();
// kick off single-photo sync
var photoFunctions = {
getMetaData: function(nextCall) {
// console.log("getMetaData");
flickr.photos.getInfo({
photo_id: id,
secret: secret
}, function(error, result) {
if(error) {
console.log(photo_idx + " returned a query error");
return console.log(error);
}
metadata = result.photo;
if(!metadata) {
console.log(photo_idx + " is somehow not a photo");
return console.log(result);
}
nextCall(metadata);
});
},
getSizes: function(nextCall) {
// console.log("getSizes");
flickr.photos.getSizes({
photo_id: id
}, function(error, result) {
if(error) {
console.log(photo_idx + " returned a query error");
return console.log(error);
}
metadata.sizes = result.sizes.size.filter(function(s) {
return !!sizeMap[s.label];
}).map(function(s) {
return sizeMap[s.label];
});
nextCall();
});
},
getContexts: function(nextCall) {
// console.log("getContexts");
if(!fs.existsSync(filename_ctx)) {
flickr.photos.getAllContexts({
photo_id: id
}, function(error, result) {
if(error) {
console.log(photo_idx + " returned a query error");
return console.log(error);
}
delete result.stat;
fs.writeFile(filename_ctx, JSON.prettyprint(result), function() {
return nextCall();
});
});
}
else { nextCall(); }
},
getComments: function(nextCall) {
// console.log("getComments");
if(!fs.existsSync(filename_cmt)) {
flickr.photos.comments.getList({
photo_id: id
}, function(error, result) {
if(error) {
console.log(photo_idx + " returned a query error");
return console.log(error);
}
var comments = result.comments;
if(!comments) {
console.log(photo_idx + " is somehow not a photo");
return console.log(result);
}
fs.writeFile(filename_cmt, JSON.prettyprint(comments), function() {
return nextCall();
});
});
}
else { nextCall(); }
},
run: function() {
// Did we already know this photo? If so, and the data from
// Flickr is newer, we need to resync this photograph.
var ondisk = false;
if(fs.existsSync(filename_md)) {
ondisk = JSON.parse(fs.readFileSync(filename_md));
}
// 1: get the photo metadata from Flickr
photoFunctions.getMetaData(function(metadata) {
// 2: check if we need to resync
// 2a: we do not.
if(ondisk && ondisk.dates.lastupdate === metadata.dates.lastupdate) {
// console.log("2a");
return download(flickr, ondisk, options.next);
}
// 2b: we do.
else {
// console.log("2b");
var getComments = photoFunctions.getComments,
getContexts = photoFunctions.getContexts,
getSizes = photoFunctions.getSizes,
gsFn = function() {
fs.writeFile(filename_md, JSON.prettyprint(metadata), function() {
return download(flickr, metadata, options.next);
});
},
gcxFn = function() { getSizes(gsFn); },
gcFn = function() { getContexts(gcxFn); };
// Run through the function chain, writing the metadata
// to disk once we have all the updated information.
getComments(gcFn);
}
});
}
};
photoFunctions.run();
}
/**
* Processing all photographs one by one
*/
function processPhotos(flickr, options) {
if(options.photo_idx >= options.total) {
console.log("done downloading photo metadata.");
if(options.callback) {
options.callback();
}
return;
}
var photo_idx = options.photo_idx,
photo = photos[photo_idx],
method = "flickr.photos.getInfo",
next = (function(flickr, options) {
options.photo = false;
options.photo_idx++;
return function() {
setTimeout(function() { processPhotos(flickr, options); }, 0);
};
}(flickr, options));
if(!photo) {
console.error("for some reason, photo " + photo_idx + " is undefined...");
return next();
}
options.photo = photo;
options.next = next;
processPhoto(flickr, options);
}
/**
* this function grabs all photo definitions from Flickr
*/
function aggregatePhotos(flickr, options) {
if(options.tally >= options.total) {
console.log("done fetching photo information from Flickr.");
console.log();
console.log("Downloading photos and metadata from Flickr.");
progressBar = new progress(' [:bar] :current/:total', { total: options.total });
setTimeout(function() {
// delete any photos that were deleted from flickr, if specified
if(options.removeDeleted) {
var ia = flickr.options.locals,
dirs = ia.dirstructure,
keys = ia.photo_keys.slice(),
pos;
photos.forEach(function(photo) {
pos = keys.indexOf(photo.id);
if(pos===-1) return;
keys.splice(pos,1);
});
if(keys.length>0) {
console.log("Pruning files from the local mirror that were deleted on Flickr");
console.log("(reason: downsync called with --prune or removeDeleted==true)");
}
// prune the remaining photos
keys.forEach(function(key) {
var match = dirs.root + "/**/" + key + ".*";
glob(match, function (err, files) {
if(err) { return console.error(err); }
files.forEach(function(file) {
fs.unlink( file, function(err, result){ if(err) { console.error(err); }});
});
});
});
}
// Move on to processing our list of photos
processPhotos(flickr, {
photo_idx: 0,
total: options.total,
callback: options.callback
});
}, 1);
return;
}
flickr.photos.search({
user_id: options.user_id,
per_page: options.per_page,
page: options.page
}, function(error, result) {
var batch = result.photos.photo;
options.tally += batch.length;
options.page += 1;
progressBarAggregate.tick(options.per_page);
photos = photos.concat(batch);
aggregatePhotos(flickr, options);
});
}
/**
* export just this function
*/
module.exports = function(flickr, next_function) {
flickr.photos.search({
user_id: flickr.options.user_id,
}, function(error, result) {
if(error) {
return console.log(error);
}
var total = parseInt(result.photos.total, 10);
console.log("Found " + total + " photos to downsync.");
progressBarAggregate = new progress(' [:bar] :current/:total', { total: total });
aggregatePhotos(flickr, {
user_id: flickr.options.user_id,
per_page: 100,
page: 1,
tally: 0,
total: total,
removeDeleted: flickr.options.removeDeleted,
callback: next_function
});
});
};