-
Notifications
You must be signed in to change notification settings - Fork 10
/
geo-lookup.js
213 lines (193 loc) · 5.29 KB
/
geo-lookup.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
const https = require('https');
let gContext;
// Github configuration is read from process.env
let GH_USER = process.env.GH_USER;
let GH_KEY = process.env.GH_KEY;
let GH_REPO = process.env.GH_REPO;
let GH_FILE = process.env.GH_FILE;
// Per-run cache of geographical locations, to avoid hitting Maps API unnecessarily
let GEO_CACHE = {};
// ENTRY POINT - retrieve data file from Github and decode/parse it before passing
// into getGeo (which iterates over each entry)
module.exports = function(context, data) {
// Make the context available globally
gContext = context;
// Do the work!
getGithubJson(githubFilename(), (data, err) => {
if (!err) {
// No error; base64 decode and JSON parse the data from the Github response
// (see https://developer.github.com/v3/repos/contents/#get-contents)
let content = JSON.parse(
new Buffer(data.content, 'base64').toString('ascii')
);
// Retrieve the geo information for each item in the original data
getGeo(makeIterator(content), (updatedContent, err) => {
if (!err) {
// Everything went well- go ahead and push back up to Github. Note that
// we need to base64 encode the JSON to embed it into the PUT (dear god, why)
let updatedContentB64 = new Buffer(
JSON.stringify(updatedContent, null, 2)
).toString('base64');
let pushData = {
path: GH_FILE,
message: 'Looked up locations, beep boop.',
content: updatedContentB64,
sha: data.sha
};
putGithubJson(githubFilename(), pushData, err => {
context.log('All done!');
context.done();
});
} else {
gContext.log('All done with get Geo error: ' + err);
context.done();
}
});
} else {
gContext.log('All done with error: ' + err);
context.done();
}
});
};
// Given an array of entries (wrapped in an iterator), walk over each of them and populate
// the lat/long, using Google Maps API. Note that we also cache locations to try and save
// some API calls
function getGeo(itr, cb) {
let curr = itr.next();
if (curr.done) {
// All done processing- pass the (now-populated) entries to the next callback
cb(curr.data);
return;
}
let location = curr.value.Location;
// Check the cache to see if we've already looked up this location
if (location in GEO_CACHE) {
gContext.log(
'Cached ' +
location +
' -> ' +
GEO_CACHE[location].lat +
' ' +
GEO_CACHE[location].long
);
curr.value.Latitude = GEO_CACHE[location].lat;
curr.value.Longitude = GEO_CACHE[location].long;
getGeo(itr, cb);
return;
}
// Nothing found in cache; do a lookup and cache the result
getGoogleJson(location, (data, err) => {
if (err) {
gContext.log('Error on ' + location + ' :' + err);
} else {
if (data.results.length > 0) {
let info = {
lat: data.results[0].geometry.location.lat,
long: data.results[0].geometry.location.lng
};
GEO_CACHE[location] = info;
curr.value.Latitude = info.lat;
curr.value.Longitude = info.long;
gContext.log(location + ' -> ' + info.lat + ' ' + info.long);
} else {
gContext.log(
"Didn't find anything for " + location + ' ::' + JSON.stringify(data)
);
}
}
setTimeout(() => getGeo(itr, cb), 1000);
});
}
function makeIterator(data) {
var i = 0;
return {
next: function() {
return i < data.length
? {
value: data[i++],
done: false
}
: {
data: data,
done: true
};
}
};
}
function getGoogleJson(location, cb) {
makeJsonReq(
{
hostname: 'maps.googleapis.com',
path: '/maps/api/geocode/json?address=' + encodeURIComponent(location),
method: 'GET'
},
undefined,
cb
);
}
function getGithubJson(path, cb) {
makeJsonReq(
{
hostname: 'api.github.com',
path: path,
method: 'GET',
auth: GH_USER + ':' + GH_KEY,
headers: {
'User-Agent': +GH_USER
}
},
undefined,
cb
);
}
function putGithubJson(path, putObj, cb) {
makeJsonReq(
{
hostname: 'api.github.com',
path: path,
method: 'PUT',
auth: GH_USER + ':' + GH_KEY,
headers: {
'User-Agent': +GH_USER
}
},
JSON.stringify(putObj),
cb
);
}
function makeJsonReq(opts, body, cb) {
let req = https.request(opts, res => {
if (res.statusCode !== 200) {
let err = new Error(
'Request ' +
opts.hostname +
' ' +
opts.path +
' failed:' +
res.statusCode +
'\n\t:' +
JSON.stringify(res.headers)
);
res.resume();
cb(undefined, err);
return;
}
res.setEncoding('utf8');
let data = '';
res.on('data', chunk => (data += chunk));
res.on('end', () => {
cb(JSON.parse(data));
});
});
req.on('error', e => {
gContext.log('Error in makeJsonReq: ' + e);
});
// If a body is provided, include it
if (body != undefined) {
req.write(body);
}
req.end();
}
function githubFilename() {
return '/repos/' + GH_USER + '/' + GH_REPO + '/contents/' + GH_FILE;
}