-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (61 loc) · 1.77 KB
/
index.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
var Flickr = require('flickr').Flickr,
csv = require("fast-csv"),
fs = require("fs"),
CONFIG = require('./config.js'),
var flickr = new Flickr(CONFIG.api, CONFIG.secret);
var getPhotos = function(id, lat, lon){
var flickr_params = {
lat:lat,
lon:lon,
radius:0.02,
radius_units:'km',
per_page:1,
format:'json'
};
flickr.executeAPIRequest("flickr.photos.search", flickr_params, false, function(err, result) {
// Show the error if we got one
if(err) {
console.log("FLICKR ERROR: " + err);
datacount = datacount + 1
if(datacount < datalength){
getPhotos(data[datacount][0], data[datacount][1],data[datacount][2])
}
}else{
// Do something with flicker photos
console.log(id, result.photos.total)
csvStream.write({id: id, total: result.photos.total});
datacount = datacount + 1
if(datacount < datalength){
getPhotos(data[datacount][0], data[datacount][1],data[datacount][2])
}else{
console.log("finished")
}
}
})
}
var csvStream = csv
.format({headers: true})
.transform(function(row){
return {
id: row.id,
total: row.total
};
}),
writableStream = fs.createWriteStream("data/img_count.csv");
writableStream.on("finish", function(){
console.log("DONE!");
});
csvStream.pipe(writableStream);
var data = [],
datalength;
csv
.fromPath("data/places.csv", {delimiter:'\t'})
.on("data", function(d){
//set your unique id, lat and lon column
data.push([d[0], d[1],d[2]])
})
.on("end", function(){
datalength = data.length - 1;
datacount = 0;
getPhotos(data[datacount][0], data[datacount][1],data[datacount][2])
});