-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
183 lines (166 loc) · 6.69 KB
/
build.ts
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
/**
* Build script for @data-sets/countries
* @link https://download.geonames.org/export/dump/
*/
import assert from 'http-assert-plus';
import fs from 'fs';
import path from 'path';
import logger from '@/lib/logger';
import sparql, { parseProperty } from '@/lib/wikidata';
import { buildFile, DataSetMaps } from '@/lib/templates';
import { compareValues } from '@/lib/utils';
import type { CountryRecord } from './index';
async function fetchData(): Promise<CountryRecord[]> {
const rows = await sparql(`
SELECT DISTINCT (?country AS ?qid)
(SAMPLE(?countryLabel) AS ?name)
(SAMPLE(?countryIso2) AS ?iso2)
(SAMPLE(?countryIso3) AS ?iso3)
(SAMPLE(?countryCallPrefix) AS ?phonePrefix)
(SAMPLE(?currency) AS ?currencyQid)
(SAMPLE(?currencyCodeLabel) AS ?currencyCode)
(SAMPLE(?population) AS ?population)
(SAMPLE(?location) AS ?latlon)
(SAMPLE(?emergencyCallLabel) AS ?emergencyPhone)
(SAMPLE(?countryGeonameLabel) AS ?geonameId)
(GROUP_CONCAT(DISTINCT ?internetTldLabel; SEPARATOR=",") as ?internetTlds)
(GROUP_CONCAT(DISTINCT ?timezoneLabel; SEPARATOR=",") as ?timezones)
(GROUP_CONCAT(DISTINCT ?languageLabel; SEPARATOR=",") as ?languages)
(SAMPLE(?continent) AS ?continentQid)
(SAMPLE(?continentLabel) AS ?continentName)
(SAMPLE(?capital) AS ?capitalQid)
(SAMPLE(?capitalLabel) AS ?capitalName)
WHERE {
?country wdt:P31 wd:Q3624078 .
?country wdt:P297 ?countryIso2 .
?country wdt:P298 ?countryIso3 .
?country wdt:P474 ?countryCallPrefix .
?country wdt:P1082 ?population .
?country wdt:P625 ?location .
?country wdt:P2852 ?emergencyCall .
?country wdt:P1566 ?countryGeoname .
?country wdt:P78 ?internetTld .
?country wdt:P38 ?currency .
?country wdt:P421 ?timezone .
?country wdt:P37 ?language .
?country wdt:P30 ?continent .
?country wdt:P36 ?capital .
?currency wdt:P498 ?currencyCode .
#not a former country
FILTER NOT EXISTS {?country wdt:P31 wd:Q3024240}
#and no an ancient civilisation (needed to exclude ancient Egypt)
FILTER NOT EXISTS {?country wdt:P31 wd:Q28171280}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?country rdfs:label ?countryLabel .
?currencyCode rdfs:label ?currencyCodeLabel .
?emergencyCall rdfs:label ?emergencyCallLabel .
?countryGeoname rdfs:label ?countryGeonameLabel .
?internetTld rdfs:label ?internetTldLabel .
?timezone rdfs:label ?timezoneLabel .
?capital rdfs:label ?capitalLabel .
?continent rdfs:label ?continentLabel .
?language rdfs:label ?languageLabel .
}
}
GROUP BY ?country
ORDER BY ?iso2
`);
// logger.info({ rows });
assert(Array.isArray(rows.results?.bindings), 'Expected SPARQL to return an array of results');
return rows.results!.bindings!.map((row: any) => ({
id: parseProperty(row, 'qid.value', 'QID')!,
name: parseProperty(row, 'name.value')!,
iso2: parseProperty(row, 'iso2.value')!,
iso3: parseProperty(row, 'iso3.value'),
phonePrefix: parseProperty(row, 'phonePrefix.value'),
currencyId: parseProperty(row, 'currencyQid.value', 'QID'),
currencyCode: parseProperty(row, 'currencyCode.value'),
population: parseProperty(row, 'population.value'),
latlon: parseProperty(row, 'latlon.value', 'LATLON'),
emergencyPhone: parseProperty(row, 'emergencyPhone.value'),
geonameId: parseProperty(row, 'geonameId.value'),
internetTlds: parseProperty(row, 'internetTlds.value', 'GROUP_CONCAT(STRING)'),
// timezones: (parseProperty(row, 'timezones.value', 'GROUP_CONCAT(STRING)') ?? [])
// .filter(tz => tz.includes('/')),
// languages: parseProperty(row, 'languages.value', 'GROUP_CONCAT(STRING)'),
continent: parseProperty(row, 'continentName.value'),
capitalCity: parseProperty(row, 'capitalName.value'),
}));
}
/* eslint-disable no-unused-vars, no-redeclare */
function pushToIndex(
index: Record<string, Record<string, any>> | undefined,
key: string,
value: Record<string, any>
): Record<string, Record<string, any>>;
function pushToIndex(
index: Record<string, string | Record<string, any>> | undefined,
key: string | undefined,
value: string
): Record<string, string | Record<string, any>>;
function pushToIndex(
index: Record<string, string | Record<string, any>> | undefined,
key: string | undefined,
value: Record<string, any> | string,
): Record<string, string | Record<string, any>> | undefined {
if (typeof key === 'string') {
assert(index === undefined || !index[key], new Error(`Duplicate entry: ${key}`));
return Object.assign(index ?? {}, { [key]: value });
} else {
return index;
}
}
/* eslint-enable no-unused-vars, no-redeclare */
(async () => {
/* eslint-disable no-process-exit */
try {
const rows = await fetchData();
compareValues(rows);
logger.info({
length: rows.length,
countryCodes: rows.map(({ iso2 }) => iso2),
sample: rows.filter(({ iso2 }) => [ 'US', 'GB' ].includes(iso2)),
}, 'Loaded country info');
fs.writeFileSync(path.resolve(__dirname, './data.json'), JSON.stringify(rows, null, 2));
const data = rows.reduce((maps: DataSetMaps, record) => {
/* eslint-disable no-param-reassign */
maps.primaryIndex = pushToIndex(maps.primaryIndex, record.id, record);
maps.indexByIso2 = pushToIndex(maps.indexByIso2, record.iso2, record.id);
maps.indexByIso3 = pushToIndex(maps.indexByIso3, record.iso3, record.id);
return maps;
}, {
primaryIndex: {},
});
fs.writeFileSync(path.resolve(__dirname, './data.cjs'), buildFile([
{ type: 'header', data: { source: 'packages/countries/build.ts' } },
{ type: 'maps.cjs', data },
]));
fs.writeFileSync(path.resolve(__dirname, './data.d.ts'), buildFile([
{ type: 'header', data: { source: 'packages/countries/build.ts' } },
{ type: 'union-types.d.ts', data: { CountryCodes: rows.map(({ iso2 }) => iso2) } },
]));
fs.writeFileSync(path.resolve(__dirname, './data.md'), buildFile([
{
type: 'data.md',
data: {
source: 'packages/countries/build.ts',
head: Object.keys(rows[0]).map(key => `\`${key}\``),
rows: rows.map(row => Object.entries(row).map(([ key, value ]) => {
if (key === 'latlon') {
return (Array.isArray(value) ? value : [ value ]).join(', ');
} else if (typeof value === 'string') {
return value;
} else {
return JSON.stringify(value);
}
})),
},
},
]));
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
})();