forked from pbakondy/mcc-mnc-list
-
Notifications
You must be signed in to change notification settings - Fork 9
/
fetch.js
217 lines (178 loc) · 6.71 KB
/
fetch.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const plmnutils = require('./plmnutils.js');
const centroids = require('./centroids.json'); // Converted from http://gothos.info/resources/ country centroids csv file
const { JSDOM } = jsdom;
const WIKI_URLS = [
'https://en.wikipedia.org/wiki/Mobile_country_code',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_2xx_(Europe)',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_3xx_(North_America)',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_4xx_(Asia)',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_5xx_(Oceania)',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_6xx_(Africa)',
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_7xx_(South_America)'
];
const MCC_MNC_OUTPUT_FILE = path.join( __dirname, 'mcc-mnc-list.json');
const STATUS_CODES_OUTPUT_FILE = path.join( __dirname, 'status-codes.json');
fs.writeFileSync(MCC_MNC_OUTPUT_FILE, "");
var statusCodes = [];
var records = [];
async function fetch (wiki_url) {
return new Promise((resolve, reject) => {
JSDOM.fromURL(wiki_url).then(dom =>
{
const { window } = dom;
var content = window.document.querySelector('#mw-content-text > .mw-parser-output');
if (!content.hasChildNodes()) {
console.log('ERROR - empty content');
return;
}
content = removeCiteReferences(content);
const children = content.childNodes;
let recordType, sectionName, countryName = null, countryCode = null;
nodeList: for (let i = 0; i < children.length; i++) {
let node = children[i];
if (!node.textContent.trim().length) {
// skip empty lines
continue;
}
if (node.nodeName === 'H2' || node.nodeName === 'H3' || node.nodeName === 'H4') {
recordType = 'other';
sectionName = node.querySelector('.mw-headline').textContent.trim();
if (sectionName === 'See also' || sectionName === 'External links' || sectionName === 'National MNC Authorities') {
break nodeList;
}
if (sectionName === 'National operators') {
continue;
}
if (sectionName.length === 1) {
continue;
}
if (sectionName === 'Test networks') {
countryName = null;
countryCode = null;
recordType = 'Test';
}
if (sectionName.indexOf(' - ') !== -1) {
let sectionParts = sectionName.split(' - ');
countryName = sectionParts[0];
countryCode = sectionParts[1];
recordType = 'National';
}
if (sectionName === 'International operators') {
countryName = null;
countryCode = null;
recordType = 'International';
}
if (recordType === 'other') {
console.log('WARN recordType is other', node.textContent);
}
}
if (node.nodeName === 'TABLE') {
let rows = node.querySelectorAll('tr');
for (let j = 1; j < rows.length; j++) {
let cols = rows[j].querySelectorAll('td');
if (cols.length < 7) {
console.log('WARN invalid table row:', rows[j], node.textContent);
continue;
}
let status = cleanup(cols[4].textContent);
if (status === 'Not Operational') {
status = 'Not operational';
}
if (status === 'operational') {
status = 'Operational';
}
if ( status && statusCodes.indexOf( status ) === -1 ) {
statusCodes.push( status );
}
var mcc = cleanup(cols[0].textContent);
var mnc = cleanup(cols[1].textContent);
var plmn = mcc ? (mnc ? mcc + mnc : null) : null;
var nibbledPlmn = plmn ? plmnutils.encPlmn(mcc, mnc) : null;
var region = plmnutils.getRegion(mcc);
// Get last 2 char of countryCode to ensure matching with a ISO 3166-1 alpha-2 code
var geo = countryCode ? getGeo(countryCode.toUpperCase().slice(-2)) : getGeo(null);
records.push({
plmn: plmn,
nibbledPlmn: nibbledPlmn,
mcc: mcc,
mnc: mnc,
region: region,
type: recordType,
countryName: countryName,
countryCode: countryCode,
lat: geo.lat,
long: geo.long,
brand: cleanup(cols[2].textContent),
operator: cleanup(cols[3].textContent),
status: status,
bands: cleanup(cols[5].textContent),
notes: cleanup(cols[6].textContent)
})
}
}
}
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE );
console.log( 'Total ' + records.length + ' records' );
resolve()
})
})
}
function removeCiteReferences(nodes) {
let links = nodes.querySelectorAll('a');
for (let i = 0; i < links.length; i++) {
let link = links[i];
let href = link.getAttribute('href');
if (href.substr(0, 10) === '#cite_note') {
link.remove();
}
}
return nodes;
}
function cleanup(str) {
str = str.trim();
str = removeCitationNeeded(str);
if (str.substr(0, 1) === '[' && str.substr(-1) === ']') {
// remove brackets-only like [7]
str = '';
}
if (str.substr(0, 1) != '[' && str.substr(-1) === ']') {
// remove postfix references like ...[7]
let index = str.lastIndexOf('[');
str = str.substr(0, index - 1).trim();
}
return str.length ? str : null;
}
function removeCitationNeeded(str) {
return str.replace(/\[citation needed\]/g, '');
}
function getGeo(countryCode) {
var centroid = centroids.filter(function(el) {return el.ISO3136.toUpperCase() == countryCode});
if (centroid.length) {
return { 'lat': String(centroid[0].LAT), 'long': String(centroid[0].LONG) };
} else {
return { 'lat': null, 'long': null };
}
}
async function run() {
for (const wiki_url of WIKI_URLS) {
await fetch(wiki_url);
}
fs.appendFile( MCC_MNC_OUTPUT_FILE, JSON.stringify( records, null, 2 ), err => {
if ( err ) {
throw err;
}
});
statusCodes.sort();
console.log("fin");
fs.writeFile( STATUS_CODES_OUTPUT_FILE, JSON.stringify( statusCodes, null, 2 ), err => {
if ( err ) {
throw err;
}
console.log( 'Status codes saved to ' + STATUS_CODES_OUTPUT_FILE );
});
}
run()