-
Notifications
You must be signed in to change notification settings - Fork 3
/
flickr.js
49 lines (42 loc) · 1.4 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
var https = require('https');
var async = require("async");
var flatten = require("lodash.flatten");
var base = "https://api.flickr.com/services/rest/?";
var apiKey = process.env.FLICKR_API_KEY;
function get(method ,opts, callback) {
var api_url = base + "&method=flickr."+ method + "&api_key=" + apiKey + "&format=json" + "&nojsoncallback=1";
for (var item in opts) {
api_url += "&" + item + "=" + opts[item];
}
var data = "";
https.get(api_url, function(res) {
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var jsonObj = JSON.parse(data);
callback(jsonObj);
});
});
}
// we assume we can only pass a string or an array of strings
var findPictures = function(query, callback) {
if(typeof query === "string") query = [query];
async.mapLimit(query, 5, function(str, cb) {
get("photos.search", {"text":str}, function(data){
var photos = data.photos.photo;
var arr = [];
for (var i = 0; i < photos.length; i++) {
var photo = photos[i];
var url = "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg";
arr.push(url);
}
cb(null, arr);
});
},function(err, data) {
if(err) return console.error('findPictures - flickr:', err);
callback(flatten(data));
});
};
// export the module
module.exports = findPictures;