-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-json.js
157 lines (116 loc) · 4.82 KB
/
parse-json.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
const fs = require('fs');
const data = require('./data/factbook.json');
// Determines if a country should be included
const validCountry = key => {
// The entire planet/EU is not a country
if (key === 'world' || key === 'european_union') {
return false;
}
// Some, e.g. oceans are included but have no population
if (Object.keys(data['countries'][key]['data']).indexOf('people') === -1) {
return false;
}
if (Object.keys(data['countries'][key]['data']['people']).indexOf('population') === -1) {
return false;
}
// Some have peak listed wrong
if (Object.keys(data['countries'][key]['data']['geography']).indexOf('elevation') === -1) {
return false;
}
if (Object.keys(data['countries'][key]['data']['geography']['elevation']).indexOf('highest_point') === -1) {
return false;
}
// Some territories that probably shouldn't be included
if (Object.keys(data['countries'][key]['data']['government']).indexOf('capital') === -1) {
return false;
}
// Excluding some very small nations
if (Number.parseInt(data['countries'][key]['data']['people']['population']['total']) < 10000) {
return false;
}
return true;
};
const countries = Object.keys(data['countries']);
const minData = {};
for (let c of countries) {
if (validCountry(c)) {
let countryData = data['countries'][c]['data'];
minData[c] = {};
// Get all names used
let names = [];
names.push(countryData['name']);
for (let nametype of Object.keys(countryData['government']['country_name'])) {
if (nametype === 'etymology' || nametype === 'note' || nametype === 'former') {
continue;
}
let name = countryData['government']['country_name'][nametype];
// Ignore invalid names
if (name === 'none') {
continue;
}
// Clean up names
if (name.indexOf(';') !== -1) {
name = name.substr(0, name.indexOf(';'));
}
if (name.indexOf('(') !== -1) {
name = name.replace(/\(.+\)/g, '');
}
// Separate multi-name strings
let tempNames;
if (name.indexOf('/') !== -1 || name.indexOf(' or ') !== -1) {
tempNames = name.split(/(\/| or )/g).filter(s => s !== ' or ');
}
else {
tempNames = [name];
}
for (let n of tempNames) {
if (names.indexOf(n) === -1) {
names.push(n.trim());
if (n.startsWith('The ')) {
names.push(n.substr(4).trim());
}
}
}
}
if (c === 'burma') {
names.push('Myanmar');
}
minData[c]['names'] = names;
minData[c]['population'] = countryData['people']['population']['total'];
minData[c]['region'] = countryData['geography']['map_references'];
// Fix incorrect formatting on Ukraine and France
if (minData[c]['region'] === 'AsiaEurope' || minData[c]['region'] === 'Europe;') {
minData[c]['region'] = 'Europe';
}
minData[c]['landlocked'] = countryData['geography']['coastline']['value'] == 0;
if (countryData['geography']['area']['land'] !== undefined) {
minData[c]['land_area'] = countryData['geography']['area']['land']['value'];
} else {
// Sudan/South Sudan had incorrect formatting, but it should be accurate enough
minData[c]['land_area'] = countryData['geography']['area']['total']['value'];
}
if (countryData['geography']['elevation']['highest_point'] instanceof Object) {
minData[c]['peak_elevation'] = countryData['geography']['elevation']['highest_point']['elevation']['value'];
} else {
// Data for Ecuador is formatted wrong
minData[c]['peak_elevation'] = Number.parseInt(countryData['geography']['elevation']['highest_point'].split(' ')[1].replace(',', ''));
}
let capital = countryData['government']['capital']['name'];
if (capital === undefined) {
capital = countryData['government']['capital']['capital'];
}
if (capital === undefined) {
delete minData[c];
continue;
}
// Clean up names
if (capital.indexOf(';') !== -1) {
capital = capital.substr(0, capital.indexOf(';'));
}
capital = capital.replace(/ \(.+?\)/g, '');
capital = capital.replace(',', '');
minData[c]['capital'] = capital;
}
}
fs.writeFileSync('./data/factbook-min.json', JSON.stringify(minData, null, 2));
console.log(`Wrote ${Object.keys(minData).length} countries to file.`);