forked from openzim/mwoffliner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpselectionsoffliner.js
executable file
·258 lines (232 loc) · 8 KB
/
wpselectionsoffliner.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
#!/usr/bin/env node
"use strict";
/************************************/
/* MODULE VARIABLE SECTION **********/
/************************************/
var fs = require( 'fs' );
var async = require( 'async' );
var http = require( 'follow-redirects' ).http;
var https = require( 'follow-redirects').https;
var pathParser = require( 'path' );
var homeDirExpander = require( 'expand-home-dir' );
var countryLanguage = require( 'country-language' );
var yargs = require( 'yargs' );
var exec = require( 'child_process' ).exec;
var spawn = require( 'child_process' ).spawn;
/************************************/
/* COMMAND LINE PARSING *************/
/************************************/
var argv = yargs.usage('Given a directory, create for each selection list file belonging to it, the corresponding ZIM file against Wikipedia: $0'
+ '\nExample: ./wpselectionsoffliner.js --directory=/tmp/wikiproject/ [--tmpDirectory=/tmp/] --outputDirectory=[/var/zim2index]' )
.require( [ 'directory' ] )
.describe( 'tmpDirectory', 'Directory where files are temporary stored')
.describe( 'outputDirectory', 'Directory to write the ZIM files')
.describe( 'verbose', 'Print debug information to the stdout' )
.describe( 'resume', 'Do not overwrite if ZIM file already created' )
.strict()
.argv;
/* Check if opt. binaries are available */
var optBinaries = [ 'mv --version' ];
optBinaries.forEach( function( cmd ) {
exec(cmd + ' 2>&1 > /dev/null', function( error, stdout, stderr ) {
if ( error ) {
console.error( 'Failed to find binary "' + cmd.split(' ')[0] + '": (' + error + ')' );
process.exit( 1 );
}
});
});
/************************************/
/* CUSTOM VARIABLE SECTION **********/
/************************************/
var date = new Date();
var directory = getAbsoluteDirectoryPath( homeDirExpander( argv.directory ) );
var outputDirectory = getAbsoluteDirectoryPath( argv.outputDirectory ? homeDirExpander( argv.outputDirectory ) + '/' : directory );
var tmpDirectory = argv.tmpDirectory;
var verbose = argv.verbose;
var resume = argv.resume;
var selections = new Array();
var wpBlackList = [];
var mediawikis = new Object();
/************************************/
/* MAIN *****************************/
/************************************/
async.series(
[
function( finished ) { init( finished ) },
function( finished ) { loadMatrix( finished ) },
function( finished ) { loadSelections( finished ) },
function( finished ) { dump( finished ) }
],
function( error ) {
if ( error ) {
console.error( 'Unable to dump correctly all mediawikis (' + error + ')' );
} else {
printLog( 'All mediawikis dump successfuly' );
}
}
);
/************************************/
/* FUNCTIONS ************************/
/************************************/
function init( finished ) {
finished();
}
function dump( finished ) {
async.eachSeries(
selections,
function ( language, finished ) {
if ( wpBlackList.indexOf( language ) == -1 ) {
var mwUrl = 'http://' + language + '.wikipedia.org/';
var articleList = directory + language;
var parsoidCode = mediawikis[ language ] && mediawikis[ language ].dbname ? mediawikis[ language ].dbname : undefined;
if ( !parsoidCode ) {
console.error( 'Unable to compute parsoid URL for :' + mwUrl );
process.exit( 1 );
}
var parsoidUrl = 'http://parsoid-lb.eqiad.wikimedia.org/' + parsoidCode + '/';
printLog( 'Dumping selection for language "' + language + '"' );
executeTransparently( './mwoffliner.js',
[ '--mwUrl=' + mwUrl, '--parsoidUrl=' + parsoidUrl,
'--outputDirectory=' + outputDirectory,
'--articleList=' + articleList,
'--filenamePrefix=' + zimFilenamePrefix,
tmpDirectory ? '--tmpDirectory=' + tmpDirectory : ' ',
resume ? '--resume' : ' ',
verbose ? '--verbose' : ' ',
],
function( executionError ) {
if ( executionError ) {
console.error( executionError );
process.exit( 1 );
} else {
finished();
}
});
} else {
printLog( language + '.wikipedia.org is blacklisted, dumping of this Wikipedia will be skiped' );
finished();
}
},
function( error ) {
finished( error );
});
}
function loadSelections( finished ) {
fs.readdir( directory, function( error, list ) {
selections = list;
setTimeout( finished, 0 );
});
}
function executeTransparently( command, args, callback, nostdout, nostderr ) {
printLog( 'Executing command: ' + command + ' ' + args.join( ' ' ) );
try {
var proc = spawn( command, args );
if ( !nostdout ) {
proc.stdout.on( 'data', function ( data ) {
printLog( String( data ).replace(/[\n\r]/g, '') );
});
}
if ( !nostderr ) {
proc.stderr.on( 'data', function ( data ) {
console.error( String( data ).replace(/[\n\r]/g, '') );
});
}
proc.on( 'close', function ( code ) {
callback( code !== 0 ? 'Error by executing ' + command : undefined );
});
} catch ( error ) {
callback( 'Error by executing ' + command );
}
}
function printLog( msg ) {
if ( verbose ) {
console.info( msg );
}
}
function loadMatrix( finished ) {
downloadContent( 'http://meta.wikimedia.org/w/api.php?action=sitematrix&format=json', function( json ) {
if ( !JSON.parse( json )['error'] ) {
var entries = JSON.parse( json );
var entryCount = entries['sitematrix']['count'];
for ( var i=0; i<entryCount; i++ ) {
var entry = entries['sitematrix'][i];
if ( entry ) {
var language = entry['code'];
var sites = entry['site'];
for ( var j=0; j<sites.length; j++) {
if ( sites[ j ].code == 'wiki' && sites[ j ].closed === undefined ) {
mediawikis[ language ] = sites[ j ];
}
}
}
}
console.log( 'Matrix loaded successfuly' );
finished();
} else {
console.error( 'Unable to parse the matrix JSON from ' + matrixUrl );
process.exit( 1 );
}
});
}
function getAbsoluteDirectoryPath( directoryPath ) {
return directoryPath[0] === '/' ? directoryPath : pathParser.resolve( process.cwd(), directoryPath ) + '/';
}
function downloadContent( url, callback, var1, var2, var3 ) {
var retryCount = 1;
async.retry(
5,
function( finished ) {
var calledFinished = false;
function callFinished( timeout, message, data ) {
if ( !calledFinished ) {
calledFinished = true;
setTimeout( finished, timeout, message, data );
}
}
http.get( url, function( response ) {
if ( response.statusCode == 200 ) {
var data = '';
response.on( 'data', function ( chunk ) {
data += chunk;
});
response.on( 'end', function() {
callFinished( 0, null, data );
});
} else {
var message = 'Unable to donwload content [' + retryCount + '] ' + decodeURI( url ) + ' (statusCode=' + response.statusCode + ').';
console.error( message );
callFinished( 0, message );
}
})
.on( 'error', function( error ) {
var message = 'Unable to download content [' + retryCount + '] ' + decodeURI( url ) + ' ( ' + error + ' ).';
console.error( message );
callFinished( 0, message );
})
.on( 'socket', function ( socket ) {
var req = this;
socket.setTimeout( 50000 * ++retryCount );
if ( !socket.custom ) {
socket.custom = true;
socket.addListener( 'timeout', function() {
var message = 'Unable to download content [' + retryCount + '] ' + decodeURI( url ) + ' (socket timeout)';
console.error( message );
callFinished( 2000, message );
});
socket.addListener( 'error', function( error ) {
var message = 'Unable to download content [' + retryCount + '] ' + decodeURI( url ) + ' (socket error)';
console.error( message );
callFinished( 2000, message );
});
}
});
},
function ( error, data ) {
if ( error ) {
console.error( "Absolutly unable to retrieve async. URL. " + error );
}
if ( callback ) {
setTimeout( callback, 0, data, var1, var2, var3 );
}
});
}