-
Notifications
You must be signed in to change notification settings - Fork 1
/
conv.js
316 lines (285 loc) · 9.91 KB
/
conv.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var fs = require('fs');
var path = require('path');
var sqlite3 = require('sqlite3').verbose();
var CHECK_TABLE_NAME =
'SELECT name FROM sqlite_master WHERE type=\'table\' AND name=?;';
var CREATE_TABLE_METDATA = 'CREATE TABLE metadata (\
_id INTEGER PRIMARY KEY AUTOINCREMENT, \
key TEXT NOT NULL, \
data TEXT\
);';
var CREATE_TABLE_METDATA_INDEX =
'CREATE UNIQUE INDEX IF NOT EXISTS metadata_key ON metadata(key);';
var INSERT_METADATA ='INSERT INTO metadata(key, data) VALUES(?, ?);';
var QUERY_METADATA = 'SELECT key FROM metadata WHERE key = ?;';
var UPDATE_METADATA = 'UPDATE metadata SET data = ? WHERE key = ?';
var CREATE_TABLE = 'CREATE TABLE words (\
_id INTEGER PRIMARY KEY AUTOINCREMENT,\
label TEXT NOT NULL,\
languageCode TEXT NOT NULL,\
serverID TEXT NOT NULL,\
url TEXT NOT NULL,\
latitude TEXT,\
longitude TEXT,\
imageURL TEXT,\
shortDesc TEXT,\
category INTEGER DEFAULT ( 0 )\
);';
var CREATE_INDEX =
'CREATE UNIQUE INDEX IF NOT EXISTS serverIDIndex ON words(serverID);';
var INSERT_RECORD ='INSERT INTO words(label, languageCode, serverID, url,\
latitude, longitude, imageURL,\
shortDesc, category)\
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?);';
var QUERY_WORD = 'SELECT label FROM words WHERE serverID = ?;';
var UPDATE_RECORD = 'UPDATE words SET label = ?, languageCode = ?,\
url = ?, latitude = ?, longitude = ?,\
imageURL = ?, shortDesc = ?, category = ?\
WHERE serverID = ?';
// argument list: [check folder] [output folder] [category id] [category folder]
// [db version]
if (process.argv.length < 7) {
console.log('usage: node conv.js {check folder} {output folder} ' +
'{category id} {category folder name} {db version}');
process.exit(-1);
return;
}
function prepareFolder(folder) {
if (folder.substr(-1, 1) !== path.sep) {
return folder + path.sep;
} else {
return folder;
}
}
var checkPath = prepareFolder(process.argv[2]);
if (!checkPath.substr(0, 2) !== './') {
checkPath = './' + checkPath;
}
var rowJSONOutputFolder = prepareFolder(process.argv[3]);
var categoryFolder = prepareFolder(process.argv[5]);
var categoryID = parseInt(process.argv[4], 10);
var dbVersion = parseInt(process.argv[6], 10);
var languageCount = {};
var outputCount = {};
function listJSONFilesAndConvertThem(error, files) {
var validFiles = [];
files.forEach(function(file) {
if (file.substr(-5, 5).toLowerCase() !== '.json') {
return;
}
if (fs.lstatSync(checkPath + file, file).isFile()) {
validFiles[validFiles.length] = {
'path': checkPath + file,
'filename': file
};
}
});
// to have lower memory consumption, we run it as single thread mode
function runNext() {
var fileObj = validFiles.pop();
if (fileObj) {
parseSingleJSONFile(fileObj.path, fileObj.filename, function() {
runNext();
});
} else {
var langCount = 0;
console.log('================= parsed language count ==================');
for (var key in languageCount) {
console.log(key + ',' + languageCount[key]);
langCount++;
}
console.log('================= output language count ==================');
for (var key in outputCount) {
console.log(key + ',' + outputCount[key])
}
console.log('all converted, total-language: ' + langCount);
}
}
runNext();
}
function getSQLiteDB(lang, callback) {
// path will be ensured before this function calls.
var dbFile = rowJSONOutputFolder + lang + path.sep + categoryFolder + lang +
'.sqlite3';
var db = new sqlite3.Database(dbFile,
sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
function cb(err) {
if (err) {
console.error(err);
process.exit(-1);
} else {
callback(db);
}
});
}
function parseSingleJSONFile(file, filename, done) {
console.log('process file: ' + file);
var json = require(file);
var key = filename.substr(0, filename.indexOf('.'));
convertDataObject(json, key, function() {
var otherLangs = json.otherLanguages;
function runNext() {
var otherLang = otherLangs.pop();
if (!otherLang) {
done();
} else {
otherLang['latitude'] = json['latitude'];
otherLang['longitude'] = json['longitude'];
otherLang['imageURL'] = json['flagImageURL'] ?
json['flagImageURL'] : json['imageURL'];
convertDataObject(otherLang, key, runNext);
}
}
runNext();
});
}
function updateRecord(outputJSON, key, db, done) {
db.run(UPDATE_RECORD, [outputJSON.label, outputJSON.languageCode,
outputJSON.url, outputJSON.latitude,
outputJSON.longitude, outputJSON.imageURL, outputJSON.shortDesc,
outputJSON.category, outputJSON.serverID],
done);
}
function insertRecord(outputJSON, key, db, done) {
db.run(INSERT_RECORD, [outputJSON.label, outputJSON.languageCode,
outputJSON.serverID, outputJSON.url,
outputJSON.latitude, outputJSON.longitude,
outputJSON.imageURL, outputJSON.shortDesc,
outputJSON.category],
done);
}
function putRecord(outputJSON, key, db, done) {
db.get(QUERY_WORD, [outputJSON.serverID], function(err, row) {
if (row) {
updateRecord(outputJSON, key, db, done);
} else {
insertRecord(outputJSON, key, db, done);
}
});
}
function updateMetadata(db, key, value, done) {
db.run(UPDATE_METADATA, [value, key], done);
}
function insertMetadata(db, key, value, done) {
db.run(INSERT_METADATA, [key, value], done);
}
function putMetadata(db, key, value, done) {
db.get(QUERY_METADATA, [key], function(err, row) {
if (row) {
updateMetadata(db, key, value, done);
} else {
insertMetadata(db, key, value, done);
}
});
}
function constructOutputJSON(json, key) {
var name = json.countryName ?
json.name + ' (' + json.countryName + ')' : json.name;
var shortDesc = json.shortDesc ? json.shortDesc.join('\n') : '';
if (!name) {
console.log('WikiDataError, name: ' + JSON.stringify(json));
return;
} else if (!json.wikiUrl) {
console.log('WikiDataError, wikiUrl: ' + JSON.stringify(json));
return;
} else {
shortDesc = shortDesc.substr(0, 200);
return {
'label': name,
'languageCode': json.lang,
'serverID': process.argv[5] + '/' + key,
'url': json.wikiUrl,
'latitude': json.latitude,
'longitude': json.longitude,
'imageURL': json.imageURL ? json.imageURL : '',
'shortDesc': shortDesc,
'category': categoryID
};
}
}
function ensureMetadata(db, version, lang, category, done) {
db.get(CHECK_TABLE_NAME, ['metadata'], function(err, row) {
if (err) {
console.error('hulk, putMetadata: ' + err);
process.exit(-1);
return;
}
function putData() {
putMetadata(db, 'version', version, function() {
putMetadata(db, 'lang', lang, function() {
putMetadata(db, 'category', category, done);
});
});
}
if (!row) {
db.run(CREATE_TABLE_METDATA, function() {
db.run(CREATE_TABLE_METDATA_INDEX, function() {
putData();
});
});
} else {
putData();
}
});
}
function putConvertedDataObject(db, json, key, outputFolder, done) {
db.get(CHECK_TABLE_NAME, ['words'], function(err, row) {
if (err) {
console.error('hulk: ' + err);
process.exit(-1);
return;
}
var outputJSON = constructOutputJSON(json, key);
if (!outputJSON) {
db.close(done());
return;
}
function outputData() {
putRecord(outputJSON, key, db, function() {
db.close(done());
});
fs.writeFile(outputFolder + key + '.json',
JSON.stringify(outputJSON) + '\n'
);
if (outputCount[json.lang]) {
outputCount[json.lang]++;
} else {
outputCount[json.lang] = 1;
}
}
if (!row) {
db.run(CREATE_TABLE, function() {
db.run(CREATE_INDEX, function() {
outputData();
});
});
} else {
outputData();
}
});
}
function convertDataObject(json, key, done) {
if (languageCount[json.lang]) {
languageCount[json.lang]++;
} else {
languageCount[json.lang] = 1;
}
// ensure the lang folder which hosts category data and database.
if (!fs.existsSync(rowJSONOutputFolder + json.lang)) {
fs.mkdirSync(rowJSONOutputFolder + json.lang);
}
// ensure the lang + category folder which hosts all json data files.
var jsonOutputFolder = rowJSONOutputFolder + json.lang + path.sep +
categoryFolder;
if (!fs.existsSync(jsonOutputFolder)) {
fs.mkdirSync(jsonOutputFolder);
}
getSQLiteDB(json.lang, function(db) {
db.serialize(function() {
ensureMetadata(db, dbVersion, json.lang, categoryFolder, function() {
putConvertedDataObject(db, json, key, jsonOutputFolder, done);
});
});
});
}
var dirs = fs.readdir(checkPath, listJSONFilesAndConvertThem);