This repository has been archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikidata.js
56 lines (50 loc) · 1.72 KB
/
wikidata.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
var wdk = require('wikidata-sdk')
var request = require('request')
function extractStatementString(entity, claimId) {
var claims = entity.claims[claimId];
if(claims && claims.length > 0) {
return claims[0].mainsnak.datavalue.value;
}
return undefined
}
function extractStatementIntQuantity(entity, claimId) {
var claims = entity.claims[claimId];
if(claims && claims.length > 0) {
return parseInt(claims[0].mainsnak.datavalue.value.amount)
}
return undefined
}
// https://www.wikidata.org/wiki/Property:P281
exports.augmentPostal = function augmentPostal(entity, tags) {
var postal = extractStatementString(entity, "P281")
if (postal) return Object.assign({}, tags, {"wikidata:P281": postal});
return tags;
}
// https://www.wikidata.org/wiki/Property:P1082
exports.augmentPopulation = function augmentPopulation(entity, tags) {
var population = extractStatementIntQuantity(entity, "P1082")
if (population) return Object.assign({}, tags, {"wikidata:P1082": population});
return tags;
}
// https://www.wikidata.org/wiki/Property:P2044
exports.augmentElevation = function augmentElevation(entity, tags) {
var elevation = extractStatementIntQuantity(entity, "P2044")
if (elevation) return Object.assign({}, tags, {"wikidata:P2044": elevation});
return tags;
}
function queryWikidata(id, cb) {
if(!id) return null
var url = wdk.getEntities({
ids: id,
properties: ['info', 'labels', 'statements']
});
request.get(url, function(error, response, body) {
if (error || response.statusCode != 200) {
return null
}
var result = JSON.parse(body);
var entity = result.entities[id];
cb(entity);
});
}
exports.queryWikidata = queryWikidata;