-
Notifications
You must be signed in to change notification settings - Fork 2
/
flickr.js
399 lines (378 loc) · 10.6 KB
/
flickr.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
function Flickr(init) {
var ajaxSettings = {
url: 'https://api.flickr.com/services/rest/',
data: {
format: 'json'
},
dataType: 'jsonp',
jsonp: false,
jsonpCallback: 'jsonFlickrApi'
};
if (typeof init === 'string') {
ajaxSettings.data.api_key = init;
} else if (typeof init === 'object') {
jQuery.extend(ajaxSettings, init);
} else if (typeof console === 'object' &&
typeof console.warn === 'function') {
console.warn('Initialize with a Flickr API key: var flickr = new Flickr("...");');
}
var self = this;
function getCallback(callback, consoleMethod) {
if (typeof callback === 'function') {
return callback;
} else if (typeof console === 'object') {
if (!consoleMethod) {
consoleMethod = 'log';
}
return function(response) {
console[consoleMethod](response);
};
}
}
function responseHandler(response, callback, args) {
callback = getCallback(callback);
if (typeof response === 'object') {
if (response.photos &&
response.photos.photo) {
for (var i = 0; i < response.photos.photo.length; i++) {
response.photos.photo[i] = new FlickrPhoto(self, response.photos.photo[i]);
}
} else if (response.photoset &&
response.photoset.photo) {
for (var i = 0; i < response.photoset.photo.length; i++) {
response.photoset.photo[i] = new FlickrPhoto(self, response.photoset.photo[i]);
}
}
callback.apply(self, [response, args]);
} else {
callback.apply(self, [{
stat: 'fail',
code: -1,
message: 'No response.'
}, args]);
}
}
function getArgs(args, required) {
if (typeof args === 'undefined') {
return {};
} else if (typeof args !== 'object' &&
required.length === 1) {
var key = required[0];
var value = args;
var args = {};
args[key] = value;
return args;
} else {
return args;
}
}
function validateArgs(method, args, required) {
var missingArgs = [], key;
for (var i = 0; i < required.length; i++) {
arg = required[i];
if (!args[arg]) {
missingArgs.push(arg);
}
}
if (missingArgs.length > 0 &&
typeof console === 'object' &&
typeof console.warn === 'function') {
console.warn('Flickr API method ' + method +
' requires argument: ' + missingArgs.join(', '));
}
}
// Credit: http://stackoverflow.com/a/822486
function stripHTML(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
function getHelp(method, callback) {
self.reflection.getMethodInfo(method, function(response) {
callback = getCallback(callback, 'info');
var required = [];
var optional = [];
for (var i = 0; i < response.arguments.argument.length; i++) {
var argument = response.arguments.argument[i];
if (argument.name === 'api_key') {
continue;
} else if (parseInt(argument.optional, 10) === 0) {
required.push(argument.name);
} else {
optional.push(argument.name);
}
}
var help = method + ' - ' + stripHTML(response.method.description._content);
help += '\n\n' + getUsage(method, required, optional) + '\n';
if (required.length > 0) {
help += '\nRequired arguments: ' + required.join(', ');
}
if (optional.length > 0) {
help += '\nOptional arguments: ' + optional.join(', ');
}
help += '\n\nMore info: https://www.flickr.com/services/api/' + method + '.html';
callback(help);
});
}
function getUsage(method, required, optional) {
if (required.length === 0 &&
optional.length === 0) {
return 'Usage: ' + method + '(callback);';
} else if (required.length === 0) {
return 'Usage: ' + method + '(arguments, callback);';
}
var usage = 'Usage: ' + method + '({\n';
var arg;
for (var i = 0; i < required.length; i++) {
arg = required[i];
usage += ' ' + arg + ': [' + arg + ']';
if (i < required.length - 1) {
usage += ',';
}
usage += '\n';
}
usage += ' }, callback);';
if (required.length === 1) {
usage += '\n\n Or: ' + method + '([' + arg + '], callback);';
}
return usage;
}
var methodList = [];
function createMethod(method, required) {
methodList.push(method);
var methodWrapper = function(args, callback) {
var settings = jQuery.extend(true, {}, ajaxSettings);
settings.data.method = method;
if (typeof args === 'function' &&
required.length === 0) {
callback = args;
args = {};
} else if (typeof args === 'string' &&
method === 'flickr.photos.search') {
args = {
text: args
};
} else {
args = getArgs(args, required);
}
validateArgs(method, args, required);
jQuery.extend(settings.data, args);
jQuery.ajax(settings).done(function(response) {
responseHandler.apply(self, [response, callback, args]);
});
return self;
};
methodWrapper.help = function(callback) {
getHelp(method, callback);
return self;
};
return methodWrapper;
}
function setup(context, baseMethod, methods) {
var method;
for (var key in methods) {
method = baseMethod + '.' + key;
if (typeof methods[key].length == 'number') {
context[key] = createMethod(method, methods[key]);
} else if (typeof methods[key] === 'object') {
context[key] = {};
setup(context[key], method, methods[key]);
}
}
}
setup(this, 'flickr', {
cameras: {
getBrandModels: ['brand'],
getBrands: []
},
collections: {
getTree: []
},
commons: {
getInstitutions: []
},
contacts: {
getPublicList: ['user_id']
},
favorites: {
getContext: ['photo_id', 'user_id'],
getPublicList: ['user_id']
},
galleries: {
getInfo: ['gallery_id'],
getList: ['user_id'],
getListForPhoto: ['photo_id'],
getPhotos: []
},
groups: {
getInfo: ['group_id'],
search: ['text'],
discuss: {
replies: {
getInfo: ['topic_id', 'reply_id'],
getList: ['topic_id']
},
topics: {
getInfo: ['topic_id'],
getList: ['group_id']
}
},
pools: {
getContext: ['photo_id', 'group_id'],
getPhotos: ['group_id']
}
},
interestingness: {
getList: []
},
machinetags: {
getNamespaces: [],
getPairs: [],
getPredicates: [],
getRecentValues: [],
getValues: ['namespace', 'predicate']
},
panda: {
getList: [],
getPhotos: ['panda_name']
},
people: {
findByEmail: ['find_email'],
findByUsername: ['username'],
getInfo: ['user_id'],
getPhotos: ['user_id'],
getPhotosOf: ['user_id'],
getPublicGroups: ['user_id'],
getPublicPhotos: ['user_id']
},
photos: {
getAllContexts: ['photo_id'],
getContactsPublicPhotos: ['user_id'],
getContext: ['photo_id'],
getExif: ['photo_id'],
getFavorites: ['photo_id'],
getInfo: ['photo_id'],
getRecent: [],
getSizes: ['photo_id'],
search: [],
comments: {
getList: ['photo_id']
},
geo: {
getLocation: ['photo_id']
},
licenses: {
getInfo: []
},
people: {
getList: ['photo_id']
}
},
photosets: {
getContext: ['photo_id', 'photoset_id'],
getInfo: ['photoset_id'],
getList: [],
getPhotos: ['photoset_id'],
comments: {
getList: ['photoset_id']
}
},
places: {
find: ['query'],
findByLatLon: ['lat', 'lon'],
getChildrenWithPhotosPublic: [],
getInfo: [],
getInfoByUrl: ['url'],
getPlaceTypes: [],
getShapeHistory: [],
getTopPlacesList: ['place_type_id'],
placesForBoundingBox: ['bbox'],
placesForTags: ['place_type_id'],
resolvePlaceId: ['place_id'],
resolvePlaceURL: ['url'],
tagsForPlace: []
},
reflection: {
getMethodInfo: ['method_name'],
getMethods: []
},
tags: {
getClusterPhotos: ['tag', 'cluster_id'],
getClusters: ['tag'],
getHotList: [],
getListPhoto: ['photo_id'],
getListUser: ['user_id'],
getListUserPopular: ['user_id'],
getListUserRaw: ['user_id'],
getRelated: []
},
test: {
echo: []
},
urls: {
getGroup: [],
getUserPhotos: [],
getUserProfile: [],
lookupGallery: [],
lookupGroup: [],
lookupUser: []
}
});
this.help = function(method, callback) {
callback = getCallback(callback, 'info');
if (!method) {
callback(
'flickr.js - A simple JavaScript wrapper for the Flickr API\n\n' +
'To list the available methods: flickr.help(\'methods\')\n' +
'To get help about a particular method: flickr.example.methodName.help() or ' +
'flickr.help(\'flickr.example.methodName\')\n\n' +
'More info: https://www.flickr.com/services/api/'
);
} else if (method === 'methods') {
callback(
'To get help about a particular method: flickr.example.methodName.help() or ' +
'flickr.help(\'flickr.example.methodName\')\n\n' +
methodList.join('\n')
);
} else {
getHelp(method, callback);
}
return self;
};
}
function FlickrPhoto(flickr, values) {
jQuery.extend(this, values);
var self = this;
this.getInfo = function(callback) {
flickr.photos.getInfo({
photo_id: self.id
}, callback);
};
Object.call(this);
}
FlickrPhoto.prototype.src = function(size, origFormat) {
/*
http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
or
http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
or
http://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
*/
var base = 'http://farm' + this.farm +
'.staticflickr.com/' + this.server +
'/' + this.id +
'_' + this.secret;
if (!size) {
return base + '.jpg';
} else if (size === 'o') {
if (!origFormat) {
origFormat = 'jpg';
}
return base + '_o.' + origFormat;
} else {
return base + '_' + size + '.jpg';
}
};
FlickrPhoto.prototype.href = function() {
return 'https://flickr.com/photos/' + this.owner + '/' + this.id;
};