-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplaceholderLookup.js
55 lines (49 loc) · 2.02 KB
/
placeholderLookup.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
const request = require('superagent');
const keepaliveAgent = require('./httpAgent')();
const selectLocality = require('./selectLocality');
const PLACEHOLDER_DEFAULT_QPS = 30;
const PLACEHOLDER_DEFAULT_PLACETYPES = 'locality,borough';
const PLACEHOLDER_HOST = process.env.PLACEHOLDER_HOST || 'localhost';
const PLACEHOLDER_PORT = process.env.PLACEHOLDER_PORT || '3000';
const PLACEHOLDER_QPS = parseInt(process.env.PLACEHOLDER_QPS, 10);
const PLACEHOLDER_PLACETYPES = process.env.PLACEHOLDER_PLACETYPES || PLACEHOLDER_DEFAULT_PLACETYPES;
const PLACEHOLDER_HTTP_URL = `http://${PLACEHOLDER_HOST}:${PLACEHOLDER_PORT}/parser/search`;
const PLACEHOLDER_QPS_LIMIT = PLACEHOLDER_QPS || PLACEHOLDER_DEFAULT_QPS;
// query placeholder with the data from a single row
function placeholder(row){
request.get(PLACEHOLDER_HTTP_URL)
.set('Connection', 'keep-alive')
.agent(keepaliveAgent)
.query({
text: row.city,
lang: 'eng',
placetype: PLACEHOLDER_PLACETYPES
})
.end((err, res) => {
var locality = selectLocality(row, err, res);
if( locality.err ){ return console.error(
JSON.stringify( locality ),
JSON.stringify( row )
)}
console.log([
row.count, row.postcode, row.city,
locality.candidates[0].wofid,
locality.candidates[0].name_english,
locality.candidates[0].name_abbr,
locality.candidates[0].placetype,
locality.candidates[0].country_name,
locality.candidates[0].country_abbr
].join('\t'));
});
}
function lookup(db){
const stmt = db.prepare(`SELECT * FROM aggregate`);
const iterator = stmt.iterate();
// apply some basic rate-limiting
const i = setInterval(() => {
row = iterator.next();
if( !row || row.done ){ clearInterval(i); }
else { placeholder( row.value ); }
}, (1000 / PLACEHOLDER_QPS_LIMIT));
}
module.exports = lookup;