-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcivparser.js
75 lines (66 loc) · 1.89 KB
/
civparser.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
var cheerio = require('cheerio');
var request = require('request');
function trim(string) {
return string.replace(/^\s+|\s+$/g, '');
};
request('http://civcraft.slimecraft.eu/map.php', function(error, response, html) {
var $ = cheerio.load(html);
var cities = Array.prototype.map.call($('.btn'), function(el) {
el = $(el);
return el.attr('data-content').split('<br>').map(function(line) {
var html = cheerio.load(line);
var key = html('b').remove().text().replace(/\:$/, '').toLowerCase();
var value = trim(html.html());
if (key === 'coordinates') {
value = value.split(',').map(function(number, index) {
return (index ? -1 : 1) * parseInt(number) * (9190000 / 15000); // magic!
});
} else if (typeof key !== 'undefined' && typeof value !== 'undefined') {
key = trim(key);
value = trim(value);
}
return {
key: key,
value: value
};
}).map(function(item) {
if (item.key === 'coordinates') {
item = {
key: 'geometry',
value: {
type: "Point",
coordinates: item.value
}
};
}
return item;
}).reduce(function(result, item) {
if (item.key === 'geometry') {
result[item.key] = item.value;
} else if (item.key !== '') {
result.properties[item.key] = item.value;
}
return result;
}, {
type: 'Feature',
properties: {
name: $(el.attr('data-original-title')).text(),
code: el.text()
}
});
});
var codes = [];
cities = cities.filter(function(city) {
if (!city.properties.code || codes.indexOf(city.properties.code) === -1) {
codes.push(city.properties.code);
return city.properties.name !== 'Cooks Laboratories';
}
return false;
}).sort(function(a, b) {
return (a.properties.name < b.properties.name) ? -1 : (a.properties.name > b.properties.name) ? 1 : 0;
})
process.stdout.write(JSON.stringify({
type: 'FeatureCollection',
features: cities
}, null, '\t'));
});