-
Notifications
You must be signed in to change notification settings - Fork 0
/
getflickrimage.js
50 lines (46 loc) · 1.84 KB
/
getflickrimage.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
module.exports = function (cityName){
var resolve;
var reject;
var p = new Promise(function(res, rej){
resolve = res;
reject = rej;
});
var placeSrcString = 'https://api.flickr.com/services/rest/?api_key=836e0029ba396aba560ba337f03bd4a0&method=flickr.places.find&query=' + cityName;
fetch(placeSrcString).then(function(res){
var p = new Promise(function(resolve, reject){
if(res.status !== 200){
return false;
}
res.text().then(function(txt){
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
var place = xmlDoc.getElementsByTagName('place')[0];
resolve(place.getAttribute('place_id'));
})
});
return p;
}).then(function(place_id){
var srcString = 'https://api.flickr.com/services/rest/?api_key=836e0029ba396aba560ba337f03bd4a0&method=flickr.photos.search&place_id=' + place_id + '&tags=cityscape';
return fetch(srcString);
}).then(function(res){
if(res.status !== 200){
return false;
}
res.text().then(function(txt){
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
var photos = Array.prototype.slice.call(
xmlDoc.getElementsByTagName('photo'),0,5);
var urls = photos.map(function(e){
var farm = e.getAttribute('farm');
var id = e.getAttribute('id');
var secret = e.getAttribute('secret');
var server = e.getAttribute('server');
return 'https://farm' + farm + '.staticflickr.com/' +
server + '/' + id + '_' + secret + '.jpg';
});
resolve(urls);
});
});
return p;
}